From 73a4dbe6d492431758466203a4ee8e78a1b5212f Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Fri, 24 Jul 2020 15:25:40 -0500 Subject: [PATCH] tensor_util.to_numpy_string --- .../Eager/EagerTensor.ToString.cs | 32 +------------------ src/TensorFlowNET.Core/Tensors/tensor_util.cs | 29 +++++++++++++++++ .../Variables/BaseResourceVariable.cs | 2 +- .../TF_API/StringsApiTest.cs | 1 - 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/TensorFlowNET.Core/Eager/EagerTensor.ToString.cs b/src/TensorFlowNET.Core/Eager/EagerTensor.ToString.cs index 72fac71b..adf44007 100644 --- a/src/TensorFlowNET.Core/Eager/EagerTensor.ToString.cs +++ b/src/TensorFlowNET.Core/Eager/EagerTensor.ToString.cs @@ -10,36 +10,6 @@ namespace Tensorflow.Eager public partial class EagerTensor { public override string ToString() - { - switch (rank) - { - case -1: - return $"tf.Tensor: shape={TensorShape}, dtype={dtype.as_numpy_name()}, numpy={GetFormattedString(dtype, numpy())}"; - case 0: - return $"tf.Tensor: shape={TensorShape}, dtype={dtype.as_numpy_name()}, numpy={GetFormattedString(dtype, numpy())}"; - default: - return $"tf.Tensor: shape={TensorShape}, dtype={dtype.as_numpy_name()}, numpy={GetFormattedString(dtype, numpy())}"; - } - } - - public static string GetFormattedString(TF_DataType dtype, NDArray nd) - { - if (nd.size == 0) - return "[]"; - - switch (dtype) - { - case TF_DataType.TF_STRING: - return string.Join(string.Empty, nd.ToArray() - .Select(x => x < 32 || x > 127 ? "\\x" + x.ToString("x") : Convert.ToChar(x).ToString())); - case TF_DataType.TF_BOOL: - return (nd.GetByte(0) > 0).ToString(); - case TF_DataType.TF_VARIANT: - case TF_DataType.TF_RESOURCE: - return ""; - default: - return nd.ToString(); - } - } + => $"tf.Tensor: shape={TensorShape}, dtype={dtype.as_numpy_name()}, numpy={tensor_util.to_numpy_string(this)}"; } } diff --git a/src/TensorFlowNET.Core/Tensors/tensor_util.cs b/src/TensorFlowNET.Core/Tensors/tensor_util.cs index 430d358b..16aaea44 100644 --- a/src/TensorFlowNET.Core/Tensors/tensor_util.cs +++ b/src/TensorFlowNET.Core/Tensors/tensor_util.cs @@ -349,5 +349,34 @@ namespace Tensorflow { return ops.convert_to_tensor(shape, dtype: TF_DataType.TF_INT32, name: "shape"); } + + public static string to_numpy_string(Tensor tensor) + { + var dtype = tensor.dtype; + + if(dtype == TF_DataType.TF_STRING && tensor.NDims > 0) + { + return $"['{string.Join("', '", tensor.StringData())}']"; + } + + var nd = tensor.numpy(); + + if (nd.size == 0) + return "[]"; + + switch (dtype) + { + case TF_DataType.TF_STRING: + return string.Join(string.Empty, nd.ToArray() + .Select(x => x < 32 || x > 127 ? "\\x" + x.ToString("x") : Convert.ToChar(x).ToString())); + case TF_DataType.TF_BOOL: + return (nd.GetByte(0) > 0).ToString(); + case TF_DataType.TF_VARIANT: + case TF_DataType.TF_RESOURCE: + return ""; + default: + return nd.ToString(); + } + } } } diff --git a/src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs b/src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs index 9adc5e4f..1904c715 100644 --- a/src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs +++ b/src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs @@ -137,7 +137,7 @@ namespace Tensorflow public override string ToString() { if (tf.context.executing_eagerly()) - return $"tf.Variable: '{Name}' shape={string.Join(",", shape)}, dtype={dtype.as_numpy_name()}, numpy={EagerTensor.GetFormattedString(dtype, numpy())}"; + return $"tf.Variable: '{Name}' shape={string.Join(",", shape)}, dtype={dtype.as_numpy_name()}, numpy={tensor_util.to_numpy_string(read_value())}"; else return $"tf.Variable: '{Name}' shape={string.Join(",", shape)}, dtype={dtype.as_numpy_name()}"; } diff --git a/test/TensorFlowNET.UnitTest/TF_API/StringsApiTest.cs b/test/TensorFlowNET.UnitTest/TF_API/StringsApiTest.cs index 538936b3..f021e581 100644 --- a/test/TensorFlowNET.UnitTest/TF_API/StringsApiTest.cs +++ b/test/TensorFlowNET.UnitTest/TF_API/StringsApiTest.cs @@ -53,7 +53,6 @@ namespace Tensorflow.UnitTest.TF_API { var strings = new[] { "map_and_batch_fusion", "noop_elimination", "shuffle_and_repeat_fusion" }; var tensor = tf.constant(strings, dtype: tf.@string, name: "optimizations"); - tensor.ToString(); var stringData = tensor.StringData(); Assert.AreEqual(3, tensor.shape[0]);