From 9665bf5262867368d356dfd884b6eb41a592bbb9 Mon Sep 17 00:00:00 2001 From: haiping008 Date: Wed, 23 Jan 2019 14:35:08 -0600 Subject: [PATCH] Aadded ContextOptions --- src/TensorFlowNET.Core/Eager/Context.cs | 2 +- .../Eager/ContextOptions.cs | 26 ++++++++++++++ .../Gradients/gradients_impl.py.cs | 17 ++++++---- .../Variables/RefVariable.cs | 2 ++ tensorflowlib/README.md | 6 +++- .../LinearRegression.cs | 2 ++ test/TensorFlowNET.UnitTest/ConsumersTest.cs | 34 +++++++++++++++++++ .../Eager/CApiVariableTest.cs | 28 +++++++++++++++ 8 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 src/TensorFlowNET.Core/Eager/ContextOptions.cs create mode 100644 test/TensorFlowNET.UnitTest/ConsumersTest.cs create mode 100644 test/TensorFlowNET.UnitTest/Eager/CApiVariableTest.cs diff --git a/src/TensorFlowNET.Core/Eager/Context.cs b/src/TensorFlowNET.Core/Eager/Context.cs index a5bef053..3d9c875d 100644 --- a/src/TensorFlowNET.Core/Eager/Context.cs +++ b/src/TensorFlowNET.Core/Eager/Context.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace Tensorflow +namespace Tensorflow.Eager { public class Context { diff --git a/src/TensorFlowNET.Core/Eager/ContextOptions.cs b/src/TensorFlowNET.Core/Eager/ContextOptions.cs new file mode 100644 index 00000000..8bc49c8d --- /dev/null +++ b/src/TensorFlowNET.Core/Eager/ContextOptions.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Eager +{ + public class ContextOptions : IDisposable + { + private IntPtr _handle; + + public ContextOptions() + { + _handle = c_api.TFE_NewContextOptions(); + } + + public void Dispose() + { + c_api.TFE_DeleteContextOptions(_handle); + } + + public static implicit operator IntPtr(ContextOptions ctx) + { + return ctx._handle; + } + } +} diff --git a/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs b/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs index 597c4e21..cd639427 100644 --- a/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs +++ b/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs @@ -102,16 +102,21 @@ namespace Tensorflow /// private static void _MarkReachedOps(List from_ops, List reached_ops, List func_graphs) { - foreach(var op in from_ops) + Queue queue = new Queue(from_ops); + while (queue.Count > 0) { - reached_ops.Add(op); - foreach(var output in op.outputs) + var op = queue.Dequeue(); + + if (!reached_ops.Contains(op)) { - reached_ops.AddRange(_Consumers(output, func_graphs)); + reached_ops.Add(op); + foreach (var output in op.outputs) + { + var c = _Consumers(output, func_graphs).ToList(); + c.ForEach(x => queue.Enqueue(x)); + } } } - - reached_ops.Reverse(); } /// diff --git a/src/TensorFlowNET.Core/Variables/RefVariable.cs b/src/TensorFlowNET.Core/Variables/RefVariable.cs index aa02b5f3..d7e1c980 100644 --- a/src/TensorFlowNET.Core/Variables/RefVariable.cs +++ b/src/TensorFlowNET.Core/Variables/RefVariable.cs @@ -12,6 +12,7 @@ namespace Tensorflow public bool _trainable; public Tensor _variable; public Tensor _snapshot; + public Operation op; public RefVariable(object initial_value, bool trainable = true, @@ -91,6 +92,7 @@ namespace Tensorflow _snapshot = gen_array_ops.identity(_variable, name = "read"); } + op = _initializer_op; ops.add_to_collections(collections, this); } } diff --git a/tensorflowlib/README.md b/tensorflowlib/README.md index ea6f02d5..982d8db9 100644 --- a/tensorflowlib/README.md +++ b/tensorflowlib/README.md @@ -4,4 +4,8 @@ Here are some pre-built TensorFlow binaries you can use for each platform: - CPU-only: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.12.0.tar.gz - GPU-enabled: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-1.12.0.tar.gz - Mac: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-1.12.0.tar.gz -- Windows: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.12.0.zip \ No newline at end of file +- Windows: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.12.0.zip + +https://www.tensorflow.org/install/source_windows +pacman -S git patch unzip +bazel build --config=opt //tensorflow:libtensorflow.so \ No newline at end of file diff --git a/test/TensorFlowNET.Examples/LinearRegression.cs b/test/TensorFlowNET.Examples/LinearRegression.cs index 849db828..0288aea1 100644 --- a/test/TensorFlowNET.Examples/LinearRegression.cs +++ b/test/TensorFlowNET.Examples/LinearRegression.cs @@ -50,6 +50,8 @@ namespace TensorFlowNET.Examples // Note, minimize() knows to modify W and b because Variable objects are trainable=True by default var optimizer = tf.train.GradientDescentOptimizer(learning_rate); optimizer.minimize(cost); + + } } } diff --git a/test/TensorFlowNET.UnitTest/ConsumersTest.cs b/test/TensorFlowNET.UnitTest/ConsumersTest.cs new file mode 100644 index 00000000..91af50e5 --- /dev/null +++ b/test/TensorFlowNET.UnitTest/ConsumersTest.cs @@ -0,0 +1,34 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow; + +namespace TensorFlowNET.UnitTest +{ + [TestClass] + public class ConsumersTest : CApiTest + { + [TestMethod] + public void Constant() + { + var X = tf.placeholder(tf.float64); + var W = tf.constant(1.0D); + + var mul = tf.multiply(X, W); + EXPECT_EQ(1, X.op.OutputNumConsumers(0)); + EXPECT_EQ(1, W.op.OutputNumConsumers(0)); + } + + [TestMethod] + public void Variable() + { + var X = tf.placeholder(tf.float64); + var W = tf.Variable(1.0D, name: "var"); + + var mul = tf.multiply(X, W); + EXPECT_EQ(1, X.op.OutputNumConsumers(0)); + EXPECT_EQ(1, W.op.OutputNumConsumers(0)); + } + } +} diff --git a/test/TensorFlowNET.UnitTest/Eager/CApiVariableTest.cs b/test/TensorFlowNET.UnitTest/Eager/CApiVariableTest.cs new file mode 100644 index 00000000..12830db6 --- /dev/null +++ b/test/TensorFlowNET.UnitTest/Eager/CApiVariableTest.cs @@ -0,0 +1,28 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow; + +namespace TensorFlowNET.UnitTest.Eager +{ + /// + /// tensorflow\c\eager\c_api_test.cc + /// + [TestClass] + public class CApiVariableTest : CApiTest, IDisposable + { + Status status = new Status(); + + [TestMethod] + public void Variables() + { + + } + + public void Dispose() + { + + } + } +}