Browse Source

add transpose API eagerly

tags/v0.20
pepure 5 years ago
parent
commit
3f4b5b3a74
2 changed files with 37 additions and 0 deletions
  1. +9
    -0
      src/TensorFlowNET.Core/Operations/gen_array_ops.cs
  2. +28
    -0
      test/TensorFlowNET.UnitTest/TF_API/TensorOperate.cs

+ 9
- 0
src/TensorFlowNET.Core/Operations/gen_array_ops.cs View File

@@ -487,6 +487,15 @@ namespace Tensorflow

public static Tensor transpose<T1, T2>(T1 x, T2 perm, string name = null)
{
if (tf.context.executing_eagerly())
{
var results = tf.Runner.TFE_FastPathExecute(tf.context, tf.context.device_name,
"Transpose", name,
null,
x, perm);

return results[0];
}
var _op = tf._op_def_lib._apply_op_helper("Transpose", name, new { x, perm });
return _op.outputs[0];
}


+ 28
- 0
test/TensorFlowNET.UnitTest/TF_API/TensorOperate.cs View File

@@ -0,0 +1,28 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NumSharp;
using System.Linq;
using Tensorflow;
using static Tensorflow.Binding;

namespace Tensorflow.UnitTest.TF_API
{
[TestClass]
public class TensorOperate
{
[TestMethod]
public void TransposeTest()
{
var a = tf.constant(np.array(new[, , ,] { { { { 1, 11, 2, 22 } }, { { 3, 33, 4, 44 } } },
{ { { 5, 55, 6, 66 } }, { { 7, 77, 8, 88 } } } }));
var b = tf.transpose(a, new[] { 3, 1, 2, 0 });
var transpose_a = tf.constant(np.array(new[, , ,] { { { { 1, 5 } }, { { 3, 7 } } },
{ { { 11, 55 } }, { { 33, 77 } } }, { { { 2, 6 } }, { { 4, 8 } } },
{ { { 22, 66 } }, { { 44, 88 } } } }));
Assert.IsTrue(Enumerable.SequenceEqual(new[] { 4, 2, 1, 2 }, b.shape));
Assert.IsTrue(Enumerable.SequenceEqual(transpose_a.numpy().ToArray<int>(), b.numpy().ToArray<int>()));
}


}
}

Loading…
Cancel
Save