From 3f4b5b3a74f03e71088a7ec00e9060f4aa14eeff Mon Sep 17 00:00:00 2001 From: pepure Date: Tue, 30 Jun 2020 22:41:40 +0800 Subject: [PATCH] add transpose API eagerly --- .../Operations/gen_array_ops.cs | 9 ++++++ .../TF_API/TensorOperate.cs | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 test/TensorFlowNET.UnitTest/TF_API/TensorOperate.cs diff --git a/src/TensorFlowNET.Core/Operations/gen_array_ops.cs b/src/TensorFlowNET.Core/Operations/gen_array_ops.cs index e718c4a3..2111564c 100644 --- a/src/TensorFlowNET.Core/Operations/gen_array_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_array_ops.cs @@ -487,6 +487,15 @@ namespace Tensorflow public static Tensor transpose(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]; } diff --git a/test/TensorFlowNET.UnitTest/TF_API/TensorOperate.cs b/test/TensorFlowNET.UnitTest/TF_API/TensorOperate.cs new file mode 100644 index 00000000..b3ce7a4a --- /dev/null +++ b/test/TensorFlowNET.UnitTest/TF_API/TensorOperate.cs @@ -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(), b.numpy().ToArray())); + } + + + } +}