Skip to content

Commit

Permalink
[numpy] Fix users of NumPy APIs that are removed in NumPy 2.0.
Browse files Browse the repository at this point in the history
This change migrates users of APIs removed in NumPy 2.0 to their recommended replacements (https://numpy.org/devdocs/numpy_2_0_migration_guide.html).

PiperOrigin-RevId: 655772286
  • Loading branch information
hawkinsp authored and tensorflower-gardener committed Aug 2, 2024
1 parent 1b08597 commit a6a7c5f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions tf_keras/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1423,20 +1423,20 @@ def __init__(
if mode == "min":
self.monitor_op = np.less
if self.best is None:
self.best = np.Inf
self.best = np.inf
elif mode == "max":
self.monitor_op = np.greater
if self.best is None:
self.best = -np.Inf
self.best = -np.inf
else:
if "acc" in self.monitor or self.monitor.startswith("fmeasure"):
self.monitor_op = np.greater
if self.best is None:
self.best = -np.Inf
self.best = -np.inf
else:
self.monitor_op = np.less
if self.best is None:
self.best = np.Inf
self.best = np.inf

if self.save_freq != "epoch" and not isinstance(self.save_freq, int):
raise ValueError(
Expand Down Expand Up @@ -2112,7 +2112,7 @@ def on_train_begin(self, logs=None):
# Allow instances to be re-used
self.wait = 0
self.stopped_epoch = 0
self.best = np.Inf if self.monitor_op == np.less else -np.Inf
self.best = np.inf if self.monitor_op == np.less else -np.inf
self.best_weights = None
self.best_epoch = 0

Expand Down Expand Up @@ -3115,10 +3115,10 @@ def _reset(self):
self.mode == "auto" and "acc" not in self.monitor
):
self.monitor_op = lambda a, b: np.less(a, b - self.min_delta)
self.best = np.Inf
self.best = np.inf
else:
self.monitor_op = lambda a, b: np.greater(a, b + self.min_delta)
self.best = -np.Inf
self.best = -np.inf
self.cooldown_counter = 0
self.wait = 0

Expand Down
4 changes: 2 additions & 2 deletions tf_keras/mixed_precision/autocast_variable_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,13 @@ def test_assign_stays_in_true_dtype(self, distribution):
# same float16 value.
self.evaluate(x.assign(1.0 + small_tensor))
self.assertEqual(1.0, self.evaluate(x.value()))
self.assertEqual(1.0 + small_val, self.evaluate(x))
self.assertEqual(1.0 + np.float64(small_val), self.evaluate(x))

self.evaluate(x.assign(1.0))
with autocast_variable.enable_auto_cast_variables(tf.float16):
self.evaluate(x.assign_add(small_tensor))
self.assertEqual(1.0, self.evaluate(x.value()))
self.assertEqual(1.0 + small_val, self.evaluate(x))
self.assertEqual(1.0 + np.float64(small_val), self.evaluate(x))

def test_thread_local_autocast_dtype(self):
x = get_var(1.0, tf.float32)
Expand Down
2 changes: 1 addition & 1 deletion tf_keras/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ def pad_sequences(
maxlen = np.max(lengths)

is_dtype_str = np.issubdtype(dtype, np.str_) or np.issubdtype(
dtype, np.unicode_
dtype, np.str_
)
if isinstance(value, str) and dtype != object and not is_dtype_str:
raise ValueError(
Expand Down

0 comments on commit a6a7c5f

Please sign in to comment.