| @@ -10,36 +10,6 @@ namespace Tensorflow.Eager | |||||
| public partial class EagerTensor | public partial class EagerTensor | ||||
| { | { | ||||
| public override string ToString() | 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<byte>() | |||||
| .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 "<unprintable>"; | |||||
| default: | |||||
| return nd.ToString(); | |||||
| } | |||||
| } | |||||
| => $"tf.Tensor: shape={TensorShape}, dtype={dtype.as_numpy_name()}, numpy={tensor_util.to_numpy_string(this)}"; | |||||
| } | } | ||||
| } | } | ||||
| @@ -349,5 +349,34 @@ namespace Tensorflow | |||||
| { | { | ||||
| return ops.convert_to_tensor(shape, dtype: TF_DataType.TF_INT32, name: "shape"); | 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<byte>() | |||||
| .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 "<unprintable>"; | |||||
| default: | |||||
| return nd.ToString(); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -137,7 +137,7 @@ namespace Tensorflow | |||||
| public override string ToString() | public override string ToString() | ||||
| { | { | ||||
| if (tf.context.executing_eagerly()) | 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 | else | ||||
| return $"tf.Variable: '{Name}' shape={string.Join(",", shape)}, dtype={dtype.as_numpy_name()}"; | return $"tf.Variable: '{Name}' shape={string.Join(",", shape)}, dtype={dtype.as_numpy_name()}"; | ||||
| } | } | ||||
| @@ -53,7 +53,6 @@ namespace Tensorflow.UnitTest.TF_API | |||||
| { | { | ||||
| var strings = new[] { "map_and_batch_fusion", "noop_elimination", "shuffle_and_repeat_fusion" }; | var strings = new[] { "map_and_batch_fusion", "noop_elimination", "shuffle_and_repeat_fusion" }; | ||||
| var tensor = tf.constant(strings, dtype: tf.@string, name: "optimizations"); | var tensor = tf.constant(strings, dtype: tf.@string, name: "optimizations"); | ||||
| tensor.ToString(); | |||||
| var stringData = tensor.StringData(); | var stringData = tensor.StringData(); | ||||
| Assert.AreEqual(3, tensor.shape[0]); | Assert.AreEqual(3, tensor.shape[0]); | ||||