From 4d8ae9a396c83ae09a1c525e267642a5935a7c19 Mon Sep 17 00:00:00 2001 From: Eli Belash Date: Sun, 1 Sep 2019 23:32:38 +0300 Subject: [PATCH] Added unit-tests for autocasting mechanism. --- test/TensorFlowNET.UnitTest/SessionTest.cs | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/test/TensorFlowNET.UnitTest/SessionTest.cs b/test/TensorFlowNET.UnitTest/SessionTest.cs index d2295166..f1453c0e 100644 --- a/test/TensorFlowNET.UnitTest/SessionTest.cs +++ b/test/TensorFlowNET.UnitTest/SessionTest.cs @@ -7,6 +7,7 @@ using System.Runtime.CompilerServices; using System.Text; using FluentAssertions; using Google.Protobuf; +using NumSharp.Backends; using Tensorflow; using Tensorflow.Util; using static Tensorflow.Binding; @@ -131,5 +132,61 @@ namespace TensorFlowNET.UnitTest } } } + + [TestMethod] + public void Autocast_Case1() + { + var sess = tf.Session().as_default(); + var input = tf.placeholder(tf.float64, shape: new TensorShape(6)); + var op = tf.reshape(input, new int[] {2, 3}); + sess.run(tf.global_variables_initializer()); + var ret = sess.run(op, feed_dict: (input, np.array(1, 2, 3, 4, 5, 6))); + + ret.Should().BeOfType().And.BeShaped(2, 3).And.BeOfValues(1, 2, 3, 4, 5, 6); + print(ret.dtype); + print(ret); + } + + [TestMethod] + public void Autocast_Case2() + { + var sess = tf.Session().as_default(); + var input = tf.placeholder(tf.float64, shape: new TensorShape(6)); + var op = tf.reshape(input, new int[] {2, 3}); + sess.run(tf.global_variables_initializer()); + var ret = sess.run(op, feed_dict: (input, np.array(1, 2, 3, 4, 5, 6).astype(NPTypeCode.Single) + 0.1f)); + + ret.Should().BeOfType().And.BeShaped(2, 3).And.BeOfValuesApproximately(0.001d, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1); + print(ret.dtype); + print(ret); + } + + [TestMethod] + public void Autocast_Case3() + { + var sess = tf.Session().as_default(); + var input = tf.placeholder(tf.int16, shape: new TensorShape(6)); + var op = tf.reshape(input, new int[] {2, 3}); + sess.run(tf.global_variables_initializer()); + var ret = sess.run(op, feed_dict: (input, np.array(1, 2, 3, 4, 5, 6).astype(NPTypeCode.Single) + 0.1f)); + + ret.Should().BeOfType().And.BeShaped(2, 3).And.BeOfValues(1, 2, 3, 4, 5, 6); + print(ret.dtype); + print(ret); + } + + [TestMethod] + public void Autocast_Case4() + { + var sess = tf.Session().as_default(); + var input = tf.placeholder(tf.@byte, shape: new TensorShape(6)); + var op = tf.reshape(input, new int[] {2, 3}); + sess.run(tf.global_variables_initializer()); + var ret = sess.run(op, feed_dict: (input, np.array(1, 2, 3, 4, 5, 6).astype(NPTypeCode.Single) + 0.1f)); + + ret.Should().BeOfType().And.BeShaped(2, 3).And.BeOfValues(1, 2, 3, 4, 5, 6); + print(ret.dtype); + print(ret); + } } } \ No newline at end of file