From e40be9338012df6f84ad923967df4b2ea7a7ab9b Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Thu, 2 Dec 2021 20:49:41 -0600 Subject: [PATCH] VariableTest #888 --- src/TensorFlowNET.Core/APIs/tf.compat.v1.cs | 9 +++++++ src/TensorFlowNET.Core/APIs/tf.variable.cs | 5 +--- .../Tensorflow.Binding.csproj | 6 ++--- .../Variables/ResourceVariable.cs | 6 +++++ src/TensorFlowNET.Core/tensorflow.cs | 4 +-- .../Tensorflow.Keras.csproj | 4 +-- .../Basics/VariableTest.cs | 26 +++++++++++++++++++ 7 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 test/TensorFlowNET.Graph.UnitTest/Basics/VariableTest.cs diff --git a/src/TensorFlowNET.Core/APIs/tf.compat.v1.cs b/src/TensorFlowNET.Core/APIs/tf.compat.v1.cs index bef37843..982e7ccc 100644 --- a/src/TensorFlowNET.Core/APIs/tf.compat.v1.cs +++ b/src/TensorFlowNET.Core/APIs/tf.compat.v1.cs @@ -47,5 +47,14 @@ namespace Tensorflow trainable: trainable, collections: collections); } + + public Operation global_variables_initializer() + { + var g = variables.global_variables(); + return variables.variables_initializer(g.ToArray()); + } + + public Session Session() + => new Session().as_default(); } } diff --git a/src/TensorFlowNET.Core/APIs/tf.variable.cs b/src/TensorFlowNET.Core/APIs/tf.variable.cs index c730e805..9ce864bd 100644 --- a/src/TensorFlowNET.Core/APIs/tf.variable.cs +++ b/src/TensorFlowNET.Core/APIs/tf.variable.cs @@ -37,10 +37,7 @@ namespace Tensorflow => variables.variables_initializer(var_list, name: name); public Operation global_variables_initializer() - { - var g = variables.global_variables(); - return variables.variables_initializer(g.ToArray()); - } + => tf.compat.v1.global_variables_initializer(); /// /// Returns all variables created with `trainable=True`. diff --git a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj index e68bf2f1..a471b877 100644 --- a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj +++ b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj @@ -5,7 +5,7 @@ TensorFlow.NET Tensorflow 2.2.0 - 0.60.5 + 0.60.6 9.0 enable Haiping Chen, Meinrad Recheis, Eli Belash @@ -20,7 +20,7 @@ Google's TensorFlow full binding in .NET Standard. Building, training and infering deep learning models. https://tensorflownet.readthedocs.io - 0.60.5.0 + 0.60.6.0 tf.net 0.60.x and above are based on tensorflow native 2.6.0 * Eager Mode is added finally. @@ -35,7 +35,7 @@ Keras API is a separate package released as TensorFlow.Keras. tf.net 0.4x.x aligns with TensorFlow v2.4.1 native library. tf.net 0.5x.x aligns with TensorFlow v2.5.x native library. tf.net 0.6x.x aligns with TensorFlow v2.6.x native library. - 0.60.5.0 + 0.60.6.0 LICENSE true true diff --git a/src/TensorFlowNET.Core/Variables/ResourceVariable.cs b/src/TensorFlowNET.Core/Variables/ResourceVariable.cs index f4a8eb1f..b31960c7 100644 --- a/src/TensorFlowNET.Core/Variables/ResourceVariable.cs +++ b/src/TensorFlowNET.Core/Variables/ResourceVariable.cs @@ -17,6 +17,7 @@ using Google.Protobuf; using System; using System.Collections.Generic; +using Tensorflow.NumPy; using static Tensorflow.Binding; namespace Tensorflow @@ -229,5 +230,10 @@ namespace Tensorflow throw new NotImplementedException("to_proto RefVariable"); } + + public NDArray eval(Session session = null) + { + return _graph_element.eval(session); + } } } diff --git a/src/TensorFlowNET.Core/tensorflow.cs b/src/TensorFlowNET.Core/tensorflow.cs index bea1e35b..a80b2007 100644 --- a/src/TensorFlowNET.Core/tensorflow.cs +++ b/src/TensorFlowNET.Core/tensorflow.cs @@ -93,9 +93,7 @@ namespace Tensorflow => ops.get_default_session(); public Session Session() - { - return new Session().as_default(); - } + => compat.v1.Session(); public Session Session(Graph graph, ConfigProto config = null) { diff --git a/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj b/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj index 8f718041..db68b385 100644 --- a/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj +++ b/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj @@ -37,8 +37,8 @@ Keras is an API designed for human beings, not machines. Keras follows best prac Git true Open.snk - 0.6.5.0 - 0.6.5.0 + 0.6.6.0 + 0.6.6.0 LICENSE diff --git a/test/TensorFlowNET.Graph.UnitTest/Basics/VariableTest.cs b/test/TensorFlowNET.Graph.UnitTest/Basics/VariableTest.cs new file mode 100644 index 00000000..35525a1a --- /dev/null +++ b/test/TensorFlowNET.Graph.UnitTest/Basics/VariableTest.cs @@ -0,0 +1,26 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using Tensorflow; +using static Tensorflow.Binding; + +namespace TensorFlowNET.UnitTest.Basics +{ + [TestClass] + public class VariableTest : GraphModeTestBase + { + [TestMethod] + public void InitVariable() + { + var v = tf.Variable(new[] { 1, 2 }); + var init = tf.compat.v1.global_variables_initializer(); + + using var sess = tf.compat.v1.Session(); + sess.run(init); + // Usage passing the session explicitly. + print(v.eval(sess)); + // Usage with the default session. The 'with' block + // above makes 'sess' the default session. + print(v.eval()); + } + } +}