From 76a08f796c560a109d8d58d75c55d38cdb8b8cd6 Mon Sep 17 00:00:00 2001 From: Tomer Kaftan Date: Mon, 26 Oct 2020 13:12:13 -0700 Subject: [PATCH] Remove usage of internal ops.numpy_text by forking it into Keras. PiperOrigin-RevId: 339102563 Change-Id: Ic21e266529dcd44e2a2199bbd0391beef4444a79 --- .../keras/mixed_precision/autocast_variable.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tensorflow/python/keras/mixed_precision/autocast_variable.py b/tensorflow/python/keras/mixed_precision/autocast_variable.py index 3cacee0cb82..6882a055a68 100644 --- a/tensorflow/python/keras/mixed_precision/autocast_variable.py +++ b/tensorflow/python/keras/mixed_precision/autocast_variable.py @@ -34,6 +34,19 @@ from tensorflow.python.types import core _autocast_dtype = threading.local() +def numpy_text(tensor, is_repr=False): + """Human readable representation of a tensor's numpy value.""" + if tensor.dtype.is_numpy_compatible: + # pylint: disable=protected-access + text = repr(tensor._numpy()) if is_repr else str(tensor._numpy()) + # pylint: enable=protected-access + else: + text = '' + if '\n' in text: + text = '\n' + text + return text + + class AutoCastVariable(variables.Variable, core.Tensor): """Variable that will cast itself to a different dtype in applicable contexts. @@ -144,7 +157,7 @@ class AutoCastVariable(variables.Variable, core.Tensor): 'dtype={v.dtype.name} dtype_to_cast_to={v._cast_dtype.name}, ' 'numpy={np_repr}>') return repr_str.format( - v=self, np_repr=ops.numpy_text(self.read_value(), is_repr=True)) + v=self, np_repr=numpy_text(self.read_value(), is_repr=True)) else: repr_str = ("') @@ -534,5 +547,3 @@ class enable_auto_cast_variables(object): # pylint:disable=invalid-name def __exit__(self, type_arg, value_arg, traceback_arg): _autocast_dtype.dtype = self._prev_dtype - -