Browse Source

tensor_util.to_numpy_string

tags/v0.20
Oceania2018 5 years ago
parent
commit
73a4dbe6d4
4 changed files with 31 additions and 33 deletions
  1. +1
    -31
      src/TensorFlowNET.Core/Eager/EagerTensor.ToString.cs
  2. +29
    -0
      src/TensorFlowNET.Core/Tensors/tensor_util.cs
  3. +1
    -1
      src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs
  4. +0
    -1
      test/TensorFlowNET.UnitTest/TF_API/StringsApiTest.cs

+ 1
- 31
src/TensorFlowNET.Core/Eager/EagerTensor.ToString.cs View File

@@ -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)}";
} }
} }

+ 29
- 0
src/TensorFlowNET.Core/Tensors/tensor_util.cs View File

@@ -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();
}
}
} }
} }

+ 1
- 1
src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs View File

@@ -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()}";
} }


+ 0
- 1
test/TensorFlowNET.UnitTest/TF_API/StringsApiTest.cs View File

@@ -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]);


Loading…
Cancel
Save