From a3672abdbedb39c963642ed2f8a1ea52fc9724ca Mon Sep 17 00:00:00 2001 From: pepure Date: Tue, 7 Jul 2020 13:09:19 +0800 Subject: [PATCH] Gradient Variable Test --- .../TF_API/GradientTest.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 test/TensorFlowNET.UnitTest/TF_API/GradientTest.cs diff --git a/test/TensorFlowNET.UnitTest/TF_API/GradientTest.cs b/test/TensorFlowNET.UnitTest/TF_API/GradientTest.cs new file mode 100644 index 00000000..0f91f93f --- /dev/null +++ b/test/TensorFlowNET.UnitTest/TF_API/GradientTest.cs @@ -0,0 +1,47 @@ +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 GradientTest + { + [TestMethod] + public void GradientFloatTest() + { + var x = tf.Variable(3.0, dtype: TF_DataType.TF_FLOAT); + using var tape = tf.GradientTape(); + var y = tf.square(x); + var y_grad = tape.gradient(y, x); + Assert.AreEqual(9.0f, (float)y); + } + + [TestMethod] + public void GradientDefaultTest() + {//error 1#: Variable default type + var x = tf.Variable(3.0); + using var tape = tf.GradientTape(); + var y = tf.square(x); + var y_grad = tape.gradient(y, x); + Assert.AreEqual(9.0, (double)y); + } + [TestMethod] + public void GradientDoubleTest() + {//error 2#: Variable double type + var x = tf.Variable(3.0, dtype: TF_DataType.TF_DOUBLE); + using var tape = tf.GradientTape(); + var y = tf.square(x); + var y_grad = tape.gradient(y, x); + Assert.AreEqual(9.0, (double)y); + } + + + + + + } +}