From 04394f2948e141cc76a85bfbd3a09246e3cd32bf Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 14 Apr 2019 07:41:23 -0500 Subject: [PATCH] add more debug code --- .../Operations/Operation.Output.cs | 6 ++-- .../Operations/Operation.cs | 11 +++--- .../TensorFlowNET.Core.csproj | 1 + src/TensorFlowNET.Core/Tensors/Tensor.cs | 12 +++---- .../control_flow_ops_test/CondTestCases.cs | 35 ++++++++++++------- 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/TensorFlowNET.Core/Operations/Operation.Output.cs b/src/TensorFlowNET.Core/Operations/Operation.Output.cs index d7e975bb..8bad5c97 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.Output.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.Output.cs @@ -1,5 +1,4 @@ -//using Newtonsoft.Json; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; @@ -15,10 +14,11 @@ namespace Tensorflow private Tensor[] _outputs; public Tensor[] outputs => _outputs; - //[JsonIgnore] + public Tensor output => _outputs.FirstOrDefault(); public int NumControlOutputs => c_api.TF_OperationNumControlOutputs(_handle); + public int OutputNumConsumers(int index) => c_api.TF_OperationOutputNumConsumers(new TF_Output(_handle, index)); public unsafe TF_Input[] OutputConsumers(int index, int max_consumers) diff --git a/src/TensorFlowNET.Core/Operations/Operation.cs b/src/TensorFlowNET.Core/Operations/Operation.cs index 7357f49f..4f3880d1 100644 --- a/src/TensorFlowNET.Core/Operations/Operation.cs +++ b/src/TensorFlowNET.Core/Operations/Operation.cs @@ -1,5 +1,6 @@ using Google.Protobuf.Collections; -//using Newtonsoft.Json; +using Newtonsoft.Json; +//using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; @@ -33,15 +34,15 @@ namespace Tensorflow private readonly IntPtr _operDesc; private Graph _graph; - //[JsonIgnore] + [JsonIgnore] public Graph graph => _graph; - //[JsonIgnore] + [JsonIgnore] public int _id => _id_value; - //[JsonIgnore] + [JsonIgnore] public int _id_value; public string type => OpType; - //[JsonIgnore] + [JsonIgnore] public Operation op => this; public TF_DataType dtype => TF_DataType.DtInvalid; private Status status = new Status(); diff --git a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj index f4d8727e..d7ad32c8 100644 --- a/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj +++ b/src/TensorFlowNET.Core/TensorFlowNET.Core.csproj @@ -45,6 +45,7 @@ Bug memory leak issue when allocating Tensor. + diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.cs b/src/TensorFlowNET.Core/Tensors/Tensor.cs index 4d5c58db..035f4bb2 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.cs @@ -1,4 +1,5 @@ //using Newtonsoft.Json; +using Newtonsoft.Json; using NumSharp; using System; using System.Collections.Generic; @@ -18,13 +19,13 @@ namespace Tensorflow private readonly IntPtr _handle; private int _id; - //[JsonIgnore] + [JsonIgnore] public int Id => _id; - //[JsonIgnore] + [JsonIgnore] public Graph graph => op?.graph; - //[JsonIgnore] + [JsonIgnore] public Operation op { get; } - //[JsonIgnore] + [JsonIgnore] public Tensor[] outputs => op.outputs; /// @@ -112,9 +113,6 @@ namespace Tensorflow public int NDims => rank; - //[JsonIgnore] - public Operation[] Consumers => consumers(); - public string Device => op.Device; public Operation[] consumers() diff --git a/test/TensorFlowNET.UnitTest/control_flow_ops_test/CondTestCases.cs b/test/TensorFlowNET.UnitTest/control_flow_ops_test/CondTestCases.cs index ce267fda..01a774a5 100644 --- a/test/TensorFlowNET.UnitTest/control_flow_ops_test/CondTestCases.cs +++ b/test/TensorFlowNET.UnitTest/control_flow_ops_test/CondTestCases.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; using System; using Tensorflow; @@ -14,26 +15,30 @@ namespace TensorFlowNET.UnitTest.control_flow_ops_test public void testCondTrue() { var graph = tf.Graph().as_default(); + // tf.train.import_meta_graph("cond_test.meta"); + var json = JsonConvert.SerializeObject(graph._nodes_by_name, Formatting.Indented); with(tf.Session(graph), sess => { - var x = tf.constant(2); - var y = tf.constant(5); - var pred = tf.less(x, y); + var x = tf.constant(2, name: "x"); // graph.get_operation_by_name("Const").output; + var y = tf.constant(5, name: "y"); // graph.get_operation_by_name("Const_1").output; + var pred = tf.less(x, y); // graph.get_operation_by_name("Less").output; Func if_true = delegate { - return tf.multiply(x, 17); + return tf.constant(2, name: "t2"); }; Func if_false = delegate { - return tf.add(y, 23); + return tf.constant(5, name: "f5"); }; - var z = control_flow_ops.cond(pred, if_true, if_false); + var z = control_flow_ops.cond(pred, if_true, if_false); // graph.get_operation_by_name("cond/Merge").output + + json = JsonConvert.SerializeObject(graph._nodes_by_name, Formatting.Indented); int result = z.eval(sess); - assertEquals(result, 34); + assertEquals(result, 2); }); } @@ -58,25 +63,31 @@ namespace TensorFlowNET.UnitTest.control_flow_ops_test print(result == 24) */ + var graph = tf.Graph().as_default(); + //tf.train.import_meta_graph("cond_test.meta"); + //var json = JsonConvert.SerializeObject(graph._nodes_by_name, Formatting.Indented); + with(tf.Session(), sess => { - var x = tf.constant(2); - var y = tf.constant(1); + var x = tf.constant(2, name: "x"); + var y = tf.constant(1, name: "y"); var pred = tf.less(x, y); Func if_true = delegate { - return tf.multiply(x, 17); + return tf.constant(2, name: "t2"); }; Func if_false = delegate { - return tf.add(y, 23); + return tf.constant(1, name: "f1"); }; var z = control_flow_ops.cond(pred, if_true, if_false); + + var json1 = JsonConvert.SerializeObject(graph._nodes_by_name, Formatting.Indented); int result = z.eval(sess); - assertEquals(result, 24); + assertEquals(result, 1); }); }