Skip to content

Commit 4c66cf6

Browse files
committed
Fix code formatting
1. Fix line length issues in tensorflow/numpy.py 2. Remove extra blank lines 3. Use specific exception types instead of bare except 4. Improve docstring formatting
1 parent d55940d commit 4c66cf6

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

keras/src/backend/tensorflow/numpy.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,7 +2742,7 @@ def round(x, decimals=0):
27422742

27432743
def tile(x, repeats):
27442744
x = convert_to_tensor(x)
2745-
2745+
27462746
# Check if repeats contains only concrete integers
27472747
# If so, keep it as a Python list/tuple for better shape inference
27482748
try:
@@ -2752,14 +2752,14 @@ def tile(x, repeats):
27522752
for r in repeats:
27532753
if isinstance(r, int):
27542754
concrete_repeats.append(r)
2755-
elif hasattr(r, 'numpy') and r.shape == ():
2755+
elif hasattr(r, "numpy") and r.shape == ():
27562756
# Scalar tensor with concrete value
27572757
concrete_repeats.append(int(r.numpy()))
27582758
else:
27592759
# Not a concrete value, fall back to tensor path
27602760
concrete_repeats = None
27612761
break
2762-
2762+
27632763
if concrete_repeats is not None:
27642764
# Use concrete repeats directly for better shape inference
27652765
repeats = concrete_repeats
@@ -2770,13 +2770,18 @@ def tile(x, repeats):
27702770
repeats = [1] * (x_rank - len(repeats)) + repeats
27712771
elif len(repeats) > x_rank:
27722772
# Need to reshape x to match repeats length
2773-
x_shape_list = [1] * (len(repeats) - x_rank) + [d if d is not None else -1 for d in x.shape.as_list()]
2773+
x_shape_list = [1] * (
2774+
len(repeats) - x_rank
2775+
) + [
2776+
d if d is not None else -1
2777+
for d in x.shape.as_list()
2778+
]
27742779
x = tf.reshape(x, x_shape_list)
27752780
return tf.tile(x, repeats)
2776-
except Exception:
2781+
except (AttributeError, TypeError, ValueError):
27772782
# If anything goes wrong, fall back to original implementation
27782783
pass
2779-
2784+
27802785
# Original dynamic implementation for non-concrete repeats
27812786
repeats = tf.reshape(convert_to_tensor(repeats, dtype="int32"), [-1])
27822787
repeats_size = tf.size(repeats)

keras/src/ops/numpy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6411,15 +6411,15 @@ def compute_output_spec(self, x):
64116411
repeats = self.repeats
64126412
if isinstance(repeats, int):
64136413
repeats = [repeats]
6414-
6414+
64156415
# Convert repeats to list if it's a tuple or other iterable
64166416
# and extract concrete integer values
64176417
if not isinstance(repeats, list):
64186418
try:
64196419
repeats = list(repeats)
64206420
except TypeError:
64216421
repeats = [repeats]
6422-
6422+
64236423
if len(x_shape) > len(repeats):
64246424
repeats = [1] * (len(x_shape) - len(repeats)) + repeats
64256425
else:

keras/src/ops/numpy_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,7 @@ def test_tile(self):
18201820
self.assertEqual(knp.tile(x, [2]).shape, (None, 6))
18211821
self.assertEqual(knp.tile(x, [1, 2]).shape, (None, 6))
18221822
self.assertEqual(knp.tile(x, [2, 1, 2]).shape, (2, None, 6))
1823-
1823+
18241824
# Test with multi-dimensional input
18251825
x = KerasTensor((None, 3, 2, 2))
18261826
self.assertEqual(knp.tile(x, [1, 2, 1, 1]).shape, (None, 6, 2, 2))
@@ -9513,12 +9513,13 @@ def call(self, x):
95139513
model.predict(np.random.randn(1, 8))
95149514

95159515
def test_tile_shape_inference_in_layer(self):
9516-
"""Test that ops.tile properly infers output shape when used in a Layer.
9517-
9516+
"""Test that ops.tile properly infers output shape in a Layer.
9517+
95189518
This is a regression test for issue #20914 where TensorFlow backend
95199519
would return all-None shapes when tile was called inside a Layer's
95209520
call method with concrete integer repeats.
95219521
"""
9522+
95229523
class TileLayer(keras.layers.Layer):
95239524
def call(self, x):
95249525
# Use concrete integer repeats
@@ -9527,7 +9528,7 @@ def call(self, x):
95279528

95289529
inputs = keras.Input(shape=(3, 2, 2))
95299530
output = TileLayer()(inputs)
9530-
9531+
95319532
# With the fix, output shape should be (None, 6, 2, 2)
95329533
# Before the fix, it was (None, None, None, None)
95339534
self.assertEqual(output.shape, (None, 6, 2, 2))

0 commit comments

Comments
 (0)