From 27fbfb02c0404da381366e4e69d140ff6642327b Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Wed, 30 Jan 2019 06:56:58 -0600 Subject: [PATCH] fix TF_OperationOutputNumConsumers --- src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs | 8 ++++---- src/TensorFlowNET.Core/Operations/Operation.cs | 2 +- src/TensorFlowNET.Core/Operations/c_api.ops.cs | 3 --- .../Sessions/c_api.tf_session_helper.cs | 2 +- src/TensorFlowNET.Core/Tensors/Tensor.cs | 2 +- test/TensorFlowNET.Examples/LinearRegression.cs | 8 ++++++++ test/TensorFlowNET.Examples/Program.cs | 6 +++++- 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs b/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs index 45cea9ed..e884bdac 100644 --- a/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs +++ b/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; namespace Tensorflow { @@ -49,10 +50,9 @@ namespace Tensorflow all.AddRange(stop_gradients1); all.AddRange(grad_ys1); - string grad_scope = ""; - using (var namescope = new ops.name_scope(name, "gradients", values: all)) + Python.with(new ops.name_scope(name, "gradients", values: all), scope => { - grad_scope = namescope; + string grad_scope = scope; // Get a uid for this call to gradients that can be used to help // cluster ops for compilation. var gradient_uid = ops.get_default_graph().unique_name("uid"); @@ -63,7 +63,7 @@ namespace Tensorflow var from_ops = xs1.Select(x => x.op).ToList(); var stop_gradient_ops = stop_gradients1.Select(x => x.op).ToList(); _PendingCount(to_ops, from_ops, colocate_gradients_with_ops, new List(), xs1); - } + }); } /// diff --git a/src/TensorFlowNET.Core/Operations/Operation.cs b/src/TensorFlowNET.Core/Operations/Operation.cs index 3cfbe4ea..bda7fbfe 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.cs @@ -38,7 +38,7 @@ namespace Tensorflow return; _handle = handle; - + this.Graph = ops.get_default_graph(); _outputs = new Tensor[NumOutputs]; for (int i = 0; i < NumOutputs; i++) _outputs[i] = new Tensor(this, i, OutputType(i)); diff --git a/src/TensorFlowNET.Core/Operations/c_api.ops.cs b/src/TensorFlowNET.Core/Operations/c_api.ops.cs index 0283e013..b97a003b 100644 --- a/src/TensorFlowNET.Core/Operations/c_api.ops.cs +++ b/src/TensorFlowNET.Core/Operations/c_api.ops.cs @@ -169,9 +169,6 @@ namespace Tensorflow [DllImport(TensorFlowLibName)] public static extern unsafe int TF_OperationOutputConsumers(TF_Output oper_out, IntPtr consumers, int max_consumers); - [DllImport(TensorFlowLibName)] - public static extern int TF_OperationOutputConsumers(TF_Output oper_out); - [DllImport(TensorFlowLibName)] public static extern TF_DataType TF_OperationOutputType(TF_Output oper_out); diff --git a/src/TensorFlowNET.Core/Sessions/c_api.tf_session_helper.cs b/src/TensorFlowNET.Core/Sessions/c_api.tf_session_helper.cs index 97d55ec4..e657210e 100644 --- a/src/TensorFlowNET.Core/Sessions/c_api.tf_session_helper.cs +++ b/src/TensorFlowNET.Core/Sessions/c_api.tf_session_helper.cs @@ -9,7 +9,7 @@ namespace Tensorflow { public static string[] TF_OperationOutputConsumers_wrapper(TF_Output oper_out) { - int num_consumers = TF_OperationOutputConsumers(oper_out); + int num_consumers = TF_OperationOutputNumConsumers(oper_out); int size = Marshal.SizeOf(); var handle = Marshal.AllocHGlobal(size * num_consumers); int num = TF_OperationOutputConsumers(oper_out, handle, num_consumers); diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.cs b/src/TensorFlowNET.Core/Tensors/Tensor.cs index 1b88b55d..f350bc8c 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.cs @@ -19,7 +19,7 @@ namespace Tensorflow private int _id; public int Id => _id; - public Graph Graph => op.Graph; + public Graph Graph => op?.Graph; public Operation op { get; } /// diff --git a/test/TensorFlowNET.Examples/LinearRegression.cs b/test/TensorFlowNET.Examples/LinearRegression.cs index 0288aea1..3311b064 100644 --- a/test/TensorFlowNET.Examples/LinearRegression.cs +++ b/test/TensorFlowNET.Examples/LinearRegression.cs @@ -51,7 +51,15 @@ namespace TensorFlowNET.Examples var optimizer = tf.train.GradientDescentOptimizer(learning_rate); optimizer.minimize(cost); + // Initialize the variables (i.e. assign their default value) + var init = tf.global_variables_initializer(); + // Start training + Python.with(tf.Session(), sess => + { + // Run the initializer + sess.run(init); + }); } } } diff --git a/test/TensorFlowNET.Examples/Program.cs b/test/TensorFlowNET.Examples/Program.cs index 41fd4e54..8ca5b6db 100644 --- a/test/TensorFlowNET.Examples/Program.cs +++ b/test/TensorFlowNET.Examples/Program.cs @@ -13,7 +13,9 @@ namespace TensorFlowNET.Examples { if (args.Length > 0 && !args.Contains(type.Name)) continue; - + + Console.WriteLine($"{DateTime.UtcNow} Starting {type.Name}"); + var example = (IExample)Activator.CreateInstance(type); try @@ -24,6 +26,8 @@ namespace TensorFlowNET.Examples { Console.WriteLine(ex); } + + Console.WriteLine($"{DateTime.UtcNow} Completed {type.Name}"); } Console.ReadLine();