Skip to content

Commit 004b483

Browse files
Clean PR: remove accidental keras initializer test from mixed-dict-keys branch
1 parent ab52378 commit 004b483

3 files changed

Lines changed: 28 additions & 164 deletions

File tree

tensorflow/python/keras/initializers/initializers_v2.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
from tensorflow.python.framework import constant_op
2121
from tensorflow.python.framework import dtypes
22-
from tensorflow.python.framework import tensor_util
2322
from tensorflow.python.keras import backend
2423
from tensorflow.python.ops import array_ops
2524
from tensorflow.python.ops import gen_linalg_ops
@@ -946,41 +945,30 @@ def truncated_normal(self, shape, mean, stddev, dtype):
946945
shape=shape, mean=mean, stddev=stddev, dtype=dtype, seed=self.seed)
947946

948947

949-
950948
def _compute_fans(shape):
951-
"""Returns (fan_in, fan_out) for layers.
949+
"""Computes the number of input and output units for a weight shape.
952950
953-
Handles dynamic/symbolic dimensions safely by attempting to extract a
954-
constant value and otherwise raising an informative TypeError.
955-
"""
956-
def _to_int(value):
957-
"""Convert value to int, handling symbolic tensors from XLA."""
958-
const_value = tensor_util.constant_value(value)
959-
if const_value is not None:
960-
return int(const_value)
961-
try:
962-
return int(value)
963-
except (TypeError, ValueError):
964-
raise TypeError(
965-
"Cannot compute fan_in/fan_out with dynamic shape dimensions. "
966-
f"Shape dimension {value!r} is symbolic/dynamic. "
967-
"Use concrete shapes or compute weights outside tf.function."
968-
)
951+
Args:
952+
shape: Integer shape tuple or TF tensor shape.
969953
954+
Returns:
955+
A tuple of integer scalars (fan_in, fan_out).
956+
"""
970957
if len(shape) < 1: # Just to avoid errors for constants.
971958
fan_in = fan_out = 1
972959
elif len(shape) == 1:
973-
fan_in = fan_out = _to_int(shape[0])
960+
fan_in = fan_out = shape[0]
974961
elif len(shape) == 2:
975-
fan_in = _to_int(shape[0])
976-
fan_out = _to_int(shape[1])
962+
fan_in = shape[0]
963+
fan_out = shape[1]
977964
else:
978-
# Assuming convolution kernels (kernel spatial dims..., in_depth, out_depth)
965+
# Assuming convolution kernels (2D, 3D, or more).
966+
# kernel shape: (..., input_depth, depth)
979967
receptive_field_size = 1
980968
for dim in shape[:-2]:
981-
receptive_field_size *= _to_int(dim)
982-
fan_in = _to_int(shape[-2]) * receptive_field_size
983-
fan_out = _to_int(shape[-1]) * receptive_field_size
969+
receptive_field_size *= dim
970+
fan_in = shape[-2] * receptive_field_size
971+
fan_out = shape[-1] * receptive_field_size
984972
return int(fan_in), int(fan_out)
985973

986974

tensorflow/python/keras/initializers/keras_initializers_dynamic_shapes_test.py

Lines changed: 0 additions & 112 deletions
This file was deleted.

tensorflow/python/ops/init_ops.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def _initializer(shape, dtype=dtypes.float32, partition_info=None):
3636
from tensorflow.python.framework import constant_op
3737
from tensorflow.python.framework import dtypes
3838
from tensorflow.python.framework import tensor_shape
39-
from tensorflow.python.framework import tensor_util
4039
from tensorflow.python.ops import array_ops
4140
from tensorflow.python.ops import array_ops_stack
4241
from tensorflow.python.ops import gen_linalg_ops
@@ -1787,41 +1786,30 @@ def he_uniform(seed=None):
17871786
# Utility functions.
17881787

17891788

1790-
17911789
def _compute_fans(shape):
1792-
"""Returns (fan_in, fan_out) for layers.
1790+
"""Computes the number of input and output units for a weight shape.
17931791
1794-
Handles dynamic/symbolic dimensions safely by attempting to extract a
1795-
constant value and otherwise raising an informative TypeError.
1796-
"""
1797-
def _to_int(value):
1798-
"""Convert value to int, handling symbolic tensors from XLA."""
1799-
const_value = tensor_util.constant_value(value)
1800-
if const_value is not None:
1801-
return int(const_value)
1802-
try:
1803-
return int(value)
1804-
except (TypeError, ValueError):
1805-
raise TypeError(
1806-
"Cannot compute fan_in/fan_out with dynamic shape dimensions. "
1807-
f"Shape dimension {value!r} is symbolic/dynamic. "
1808-
"Use concrete shapes or compute weights outside tf.function."
1809-
)
1792+
Args:
1793+
shape: Integer shape tuple or TF tensor shape.
18101794
1795+
Returns:
1796+
A tuple of integer scalars (fan_in, fan_out).
1797+
"""
18111798
if len(shape) < 1: # Just to avoid errors for constants.
18121799
fan_in = fan_out = 1
18131800
elif len(shape) == 1:
1814-
fan_in = fan_out = _to_int(shape[0])
1801+
fan_in = fan_out = shape[0]
18151802
elif len(shape) == 2:
1816-
fan_in = _to_int(shape[0])
1817-
fan_out = _to_int(shape[1])
1803+
fan_in = shape[0]
1804+
fan_out = shape[1]
18181805
else:
1819-
# Assuming convolution kernels (kernel spatial dims..., in_depth, out_depth)
1806+
# Assuming convolution kernels (2D, 3D, or more).
1807+
# kernel shape: (..., input_depth, depth)
18201808
receptive_field_size = 1
18211809
for dim in shape[:-2]:
1822-
receptive_field_size *= _to_int(dim)
1823-
fan_in = _to_int(shape[-2]) * receptive_field_size
1824-
fan_out = _to_int(shape[-1]) * receptive_field_size
1810+
receptive_field_size *= dim
1811+
fan_in = shape[-2] * receptive_field_size
1812+
fan_out = shape[-1] * receptive_field_size
18251813
return int(fan_in), int(fan_out)
18261814

18271815

0 commit comments

Comments
 (0)