Skip to content

Modified compute_output_shape function to handle broadcasting behavior in layers.Rescaling #21351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 10, 2025

Conversation

sonali-kumari1
Copy link
Contributor

  • Fixed compute_output_shape() in Rescaling to correctly handle the broadcasting and ensure consistent shapes in model.summary() and model.output_shape.
  • Improved Rescaling layer docstring with detailed description of scale and offset parameters.

Fixes #21319

@codecov-commenter
Copy link

codecov-commenter commented Jun 4, 2025

Codecov Report

Attention: Patch coverage is 77.14286% with 8 lines in your changes missing coverage. Please review.

Project coverage is 82.74%. Comparing base (de9cf25) to head (2544af9).
Report is 50 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/layers/preprocessing/rescaling.py 77.14% 4 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21351      +/-   ##
==========================================
+ Coverage   82.65%   82.74%   +0.09%     
==========================================
  Files         565      565              
  Lines       54823    55339     +516     
  Branches     8514     8639     +125     
==========================================
+ Hits        45315    45793     +478     
- Misses       7414     7440      +26     
- Partials     2094     2106      +12     
Flag Coverage Δ
keras 82.56% <74.28%> (+0.09%) ⬆️
keras-jax 63.36% <71.42%> (-0.23%) ⬇️
keras-numpy 58.53% <25.71%> (-0.22%) ⬇️
keras-openvino 33.76% <2.85%> (+0.62%) ⬆️
keras-tensorflow 63.78% <71.42%> (-0.23%) ⬇️
keras-torch 63.39% <71.42%> (-0.27%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! The change should be accompanied by a unit test.

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Jun 13, 2025
@github-project-automation github-project-automation bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Jun 13, 2025
@fchollet
Copy link
Collaborator

Please fix the failing torch test

FAILED keras/src/layers/preprocessing/rescaling_test.py::RescalingTest::test_rescaling_broadcast_output_shape - AssertionError: False is not true : Expected output shapes (2, 2) but received torch.Size([2, 2, 2])

@sonali-kumari1
Copy link
Contributor Author

Please fix the failing torch test

FAILED keras/src/layers/preprocessing/rescaling_test.py::RescalingTest::test_rescaling_broadcast_output_shape - AssertionError: False is not true : Expected output shapes (2, 2) but received torch.Size([2, 2, 2])

Hi @fchollet,
I tested the output shapes locally across all three backends — TensorFlow, JAX, and Torch — and observed consistent shapes: (2, 2) for TensorFlow and JAX, and torch.Size([2, 2]) for Torch. Here's the code snippet I used:

import numpy as np
import keras
from keras import layers

x = np.ones((2, 1))
y_target = np.ones((2, 1))


model = keras.Sequential(
    [
        layers.Input(shape=(1,), name="Feature"),
        layers.Rescaling([1.0, 2.0], [0.2, 0.4]),
    ]
)
model.summary()

y_pred = model(x)

print(f"Expected model output shape: {model.output_shape}")
print(f"True model output shape: {y_pred.shape}")
print(f"Target shape: {y_target.shape}")

model.compile(
    optimizer="adam",
    loss="mse",
)
model.fit(x, y_target, epochs=1, batch_size=8)

print("Result:", y_pred)

print("Actual input: ", x)

Output shapes :

  • TensorFlow: (2, 2)
  • JAX: (2, 2)
  • Torch: torch.Size([2, 2])

However, in CI environment, and specifically with the Torch backend, the output shape has an extra dimension.

@fchollet
Copy link
Collaborator

However, in CI environment, and specifically with the Torch backend, the output shape has an extra dimension.

The key bit is that torch tests run with image_data_format set to channels_first. Please make sure the test takes this into account.

@google-ml-butler google-ml-butler bot removed the ready to pull Ready to be merged into the codebase label Jul 7, 2025
Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Jul 7, 2025
@fchollet fchollet merged commit e233825 into keras-team:master Jul 10, 2025
10 checks passed
@github-project-automation github-project-automation bot moved this from Approved by Reviewer to Merged in PR Queue Jul 10, 2025
@sonali-kumari1 sonali-kumari1 deleted the rescalingTest branch July 10, 2025 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready to pull Ready to be merged into the codebase size:M
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

Inconsistent broadcasting behaviour in layers.Rescaling
5 participants