Skip to content

Commit 04394f2

Browse files
committed
add more debug code
1 parent 6bd08d6 commit 04394f2

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

src/TensorFlowNET.Core/Operations/Operation.Output.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//using Newtonsoft.Json;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54
using System.Runtime.InteropServices;
@@ -15,10 +14,11 @@ public partial class Operation
1514

1615
private Tensor[] _outputs;
1716
public Tensor[] outputs => _outputs;
18-
//[JsonIgnore]
17+
1918
public Tensor output => _outputs.FirstOrDefault();
2019

2120
public int NumControlOutputs => c_api.TF_OperationNumControlOutputs(_handle);
21+
2222
public int OutputNumConsumers(int index) => c_api.TF_OperationOutputNumConsumers(new TF_Output(_handle, index));
2323

2424
public unsafe TF_Input[] OutputConsumers(int index, int max_consumers)

src/TensorFlowNET.Core/Operations/Operation.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Google.Protobuf.Collections;
2-
//using Newtonsoft.Json;
2+
using Newtonsoft.Json;
3+
//using Newtonsoft.Json;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -33,15 +34,15 @@ public partial class Operation : ITensorOrOperation
3334
private readonly IntPtr _operDesc;
3435

3536
private Graph _graph;
36-
//[JsonIgnore]
37+
[JsonIgnore]
3738
public Graph graph => _graph;
38-
//[JsonIgnore]
39+
[JsonIgnore]
3940
public int _id => _id_value;
40-
//[JsonIgnore]
41+
[JsonIgnore]
4142
public int _id_value;
4243

4344
public string type => OpType;
44-
//[JsonIgnore]
45+
[JsonIgnore]
4546
public Operation op => this;
4647
public TF_DataType dtype => TF_DataType.DtInvalid;
4748
private Status status = new Status();

src/TensorFlowNET.Core/TensorFlowNET.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Bug memory leak issue when allocating Tensor.</PackageReleaseNotes>
4545

4646
<ItemGroup>
4747
<PackageReference Include="Google.Protobuf" Version="3.7.0" />
48+
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
4849
</ItemGroup>
4950

5051
<ItemGroup>

src/TensorFlowNET.Core/Tensors/Tensor.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//using Newtonsoft.Json;
2+
using Newtonsoft.Json;
23
using NumSharp;
34
using System;
45
using System.Collections.Generic;
@@ -18,13 +19,13 @@ public partial class Tensor : Python, IDisposable, ITensorOrOperation
1819
private readonly IntPtr _handle;
1920

2021
private int _id;
21-
//[JsonIgnore]
22+
[JsonIgnore]
2223
public int Id => _id;
23-
//[JsonIgnore]
24+
[JsonIgnore]
2425
public Graph graph => op?.graph;
25-
//[JsonIgnore]
26+
[JsonIgnore]
2627
public Operation op { get; }
27-
//[JsonIgnore]
28+
[JsonIgnore]
2829
public Tensor[] outputs => op.outputs;
2930

3031
/// <summary>
@@ -112,9 +113,6 @@ public int rank
112113

113114
public int NDims => rank;
114115

115-
//[JsonIgnore]
116-
public Operation[] Consumers => consumers();
117-
118116
public string Device => op.Device;
119117

120118
public Operation[] consumers()

test/TensorFlowNET.UnitTest/control_flow_ops_test/CondTestCases.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Newtonsoft.Json;
23
using System;
34
using Tensorflow;
45

@@ -14,26 +15,30 @@ public class CondTestCases : PythonTest
1415
public void testCondTrue()
1516
{
1617
var graph = tf.Graph().as_default();
18+
// tf.train.import_meta_graph("cond_test.meta");
19+
var json = JsonConvert.SerializeObject(graph._nodes_by_name, Formatting.Indented);
1720

1821
with(tf.Session(graph), sess =>
1922
{
20-
var x = tf.constant(2);
21-
var y = tf.constant(5);
22-
var pred = tf.less(x, y);
23+
var x = tf.constant(2, name: "x"); // graph.get_operation_by_name("Const").output;
24+
var y = tf.constant(5, name: "y"); // graph.get_operation_by_name("Const_1").output;
25+
var pred = tf.less(x, y); // graph.get_operation_by_name("Less").output;
2326

2427
Func<ITensorOrOperation> if_true = delegate
2528
{
26-
return tf.multiply(x, 17);
29+
return tf.constant(2, name: "t2");
2730
};
2831

2932
Func<ITensorOrOperation> if_false = delegate
3033
{
31-
return tf.add(y, 23);
34+
return tf.constant(5, name: "f5");
3235
};
3336

34-
var z = control_flow_ops.cond(pred, if_true, if_false);
37+
var z = control_flow_ops.cond(pred, if_true, if_false); // graph.get_operation_by_name("cond/Merge").output
38+
39+
json = JsonConvert.SerializeObject(graph._nodes_by_name, Formatting.Indented);
3540
int result = z.eval(sess);
36-
assertEquals(result, 34);
41+
assertEquals(result, 2);
3742
});
3843
}
3944

@@ -58,25 +63,31 @@ with tf.Session() as sess:
5863
5964
print(result == 24) */
6065

66+
var graph = tf.Graph().as_default();
67+
//tf.train.import_meta_graph("cond_test.meta");
68+
//var json = JsonConvert.SerializeObject(graph._nodes_by_name, Formatting.Indented);
69+
6170
with(tf.Session(), sess =>
6271
{
63-
var x = tf.constant(2);
64-
var y = tf.constant(1);
72+
var x = tf.constant(2, name: "x");
73+
var y = tf.constant(1, name: "y");
6574
var pred = tf.less(x, y);
6675

6776
Func<ITensorOrOperation> if_true = delegate
6877
{
69-
return tf.multiply(x, 17);
78+
return tf.constant(2, name: "t2");
7079
};
7180

7281
Func<ITensorOrOperation> if_false = delegate
7382
{
74-
return tf.add(y, 23);
83+
return tf.constant(1, name: "f1");
7584
};
7685

7786
var z = control_flow_ops.cond(pred, if_true, if_false);
87+
88+
var json1 = JsonConvert.SerializeObject(graph._nodes_by_name, Formatting.Indented);
7889
int result = z.eval(sess);
79-
assertEquals(result, 24);
90+
assertEquals(result, 1);
8091
});
8192
}
8293

0 commit comments

Comments
 (0)