From 9b07a2e19bcc8427cdd61d4b890c31dc4304136f Mon Sep 17 00:00:00 2001 From: phawkins Date: Fri, 26 Jul 2024 08:59:59 -0700 Subject: [PATCH] [numpy] Fix users of NumPy APIs that are removed in NumPy 2.0. 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: 656419848 --- .../python/bijectors/bijector_test_util.py | 8 ++++++-- .../python/distributions/lambertw_f_test.py | 2 +- tensorflow_probability/python/internal/dtype_util.py | 11 ++++++++++- .../python/internal/dtype_util_test.py | 6 ++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/tensorflow_probability/python/bijectors/bijector_test_util.py b/tensorflow_probability/python/bijectors/bijector_test_util.py index 7243d64c62..35732b2c5e 100644 --- a/tensorflow_probability/python/bijectors/bijector_test_util.py +++ b/tensorflow_probability/python/bijectors/bijector_test_util.py @@ -89,8 +89,12 @@ def assert_scalar_congruency(bijector, ten_x_pts = np.linspace(lower_x, upper_x, num=10).astype(np.float32) if bijector.dtype is not None: ten_x_pts = ten_x_pts.astype(dtype_util.as_numpy_dtype(bijector.dtype)) - lower_x = np.cast[dtype_util.as_numpy_dtype(bijector.dtype)](lower_x) - upper_x = np.cast[dtype_util.as_numpy_dtype(bijector.dtype)](upper_x) + lower_x = np.asarray( + lower_x, dtype=dtype_util.as_numpy_dtype(bijector.dtype) + ) + upper_x = np.asarray( + upper_x, dtype=dtype_util.as_numpy_dtype(bijector.dtype) + ) forward_on_10_pts = bijector.forward(ten_x_pts) # Set the lower/upper limits in the range of the bijector. diff --git a/tensorflow_probability/python/distributions/lambertw_f_test.py b/tensorflow_probability/python/distributions/lambertw_f_test.py index 95d8da2918..25a90ea6ec 100644 --- a/tensorflow_probability/python/distributions/lambertw_f_test.py +++ b/tensorflow_probability/python/distributions/lambertw_f_test.py @@ -205,7 +205,7 @@ def dist_lambda(t): model.compile(optimizer=optimizer, loss=negloglik) model.fit(x, y, epochs=1, verbose=True, batch_size=32, validation_split=0.2) - self.assertGreater(model.history.history["val_loss"][0], -np.Inf) + self.assertGreater(model.history.history["val_loss"][0], -np.inf) if __name__ == "__main__": diff --git a/tensorflow_probability/python/internal/dtype_util.py b/tensorflow_probability/python/internal/dtype_util.py index ca75d1a893..3fc3f647a0 100644 --- a/tensorflow_probability/python/internal/dtype_util.py +++ b/tensorflow_probability/python/internal/dtype_util.py @@ -231,6 +231,15 @@ def _unify_dtype(current, new): lambda dt, h: base_dtype(h if dt is None else dt), dtype, dtype_hint) +def _issctype(x): + if not isinstance(x, (type, np.dtype)): + return False + try: + return np.dtype(x) != np.object_ + except: # pylint: disable=bare-except + return False + + def convert_to_dtype(tensor_or_dtype, dtype=None, dtype_hint=None): """Get a dtype from a list/tensor/dtype using convert_to_tensor semantics.""" if tensor_or_dtype is None: @@ -244,7 +253,7 @@ def convert_to_dtype(tensor_or_dtype, dtype=None, dtype_hint=None): # Numpy dtypes defer to dtype/dtype_hint elif isinstance(tensor_or_dtype, np.ndarray): dt = base_dtype(dtype or dtype_hint or tensor_or_dtype.dtype) - elif np.issctype(tensor_or_dtype): + elif _issctype(tensor_or_dtype): dt = base_dtype(dtype or dtype_hint or tensor_or_dtype) else: # If this is a Python object, call `convert_to_tensor` and grab the dtype. diff --git a/tensorflow_probability/python/internal/dtype_util_test.py b/tensorflow_probability/python/internal/dtype_util_test.py index 861b99bebc..6b0e5a0774 100644 --- a/tensorflow_probability/python/internal/dtype_util_test.py +++ b/tensorflow_probability/python/internal/dtype_util_test.py @@ -179,7 +179,9 @@ def testMax(self, dtype, expected_maxval): disable_numpy=True, reason='`convert_to_tensor` respects array dtypes in numpy backend.') def testConvertToDtype(self, tensor_or_dtype, dtype, dtype_hint): - if np.issctype(tensor_or_dtype): + if isinstance(tensor_or_dtype, np.generic) or hasattr( + tensor_or_dtype, 'dtype' + ): example_tensor = np.zeros([], tensor_or_dtype) elif isinstance(tensor_or_dtype, tf.DType): example_tensor = tf.zeros([], tensor_or_dtype) @@ -203,7 +205,7 @@ def testConvertToDtype(self, tensor_or_dtype, dtype, dtype_hint): disable_jax=True, reason='`convert_to_tensor` only raises in TF mode.') def testConvertToDTypeRaises(self, tensor_or_dtype, dtype, dtype_hint): - if np.issctype(tensor_or_dtype): + if isinstance(tensor_or_dtype, np.generic): example_tensor = np.zeros([], tensor_or_dtype) elif isinstance(tensor_or_dtype, tf.DType): example_tensor = tf.zeros([], tensor_or_dtype)