Skip to content

Commit 0b99f3c

Browse files
committed
Xor is working without imported graph!
1 parent f4227db commit 0b99f3c

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

‎test/TensorFlowNET.Examples/NeuralNetXor.cs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public class NeuralNetXor : Python, IExample
1616
public int Priority => 10;
1717
public bool Enabled { get; set; } = true;
1818
public string Name => "NN XOR";
19-
public bool ImportGraph { get; set; } = true;
19+
public bool ImportGraph { get; set; } = false;
2020

2121
public int num_steps = 5000;
2222

2323
private NDArray data;
2424

25-
private (Operation, Tensor, RefVariable) make_graph(Tensor features,Tensor labels, int num_hidden = 8)
25+
private (Operation, Tensor, Tensor) make_graph(Tensor features,Tensor labels, int num_hidden = 8)
2626
{
2727
var stddev = 1 / Math.Sqrt(2);
2828
var hidden_weights = tf.Variable(tf.truncated_normal(new int []{2, num_hidden}, stddev: (float) stddev ));
@@ -69,6 +69,8 @@ private float RunWithImportedGraph()
6969
Tensor features = graph.get_operation_by_name("Placeholder");
7070
Tensor labels = graph.get_operation_by_name("Placeholder_1");
7171
Tensor loss = graph.get_operation_by_name("loss");
72+
Tensor train_op = graph.get_operation_by_name("train_op");
73+
Tensor global_step = graph.get_operation_by_name("global_step");
7274

7375
var init = tf.global_variables_initializer();
7476
float loss_value = 0;
@@ -86,7 +88,8 @@ private float RunWithImportedGraph()
8688
// [train_op, gs, loss],
8789
// feed_dict={features: xy, labels: y_}
8890
// )
89-
loss_value = sess.run(loss, new FeedItem(features, data), new FeedItem(labels, y_));
91+
var result = sess.run(new ITensorOrOperation[] { train_op, global_step, loss }, new FeedItem(features, data), new FeedItem(labels, y_));
92+
loss_value = result[2];
9093
step++;
9194
if (step % 1000 == 0)
9295
Console.WriteLine($"Step {step} loss: {loss_value}");
@@ -122,7 +125,9 @@ private float RunWithBuiltGraph()
122125
// [train_op, gs, loss],
123126
// feed_dict={features: xy, labels: y_}
124127
// )
125-
loss_value = sess.run(loss, new FeedItem(features, data), new FeedItem(labels, y_));
128+
var result = sess.run(new ITensorOrOperation[] { train_op, gs, loss }, new FeedItem(features, data), new FeedItem(labels, y_));
129+
loss_value = result[2];
130+
//step = result[1];
126131
step++;
127132
if (step % 1000 == 0)
128133
Console.WriteLine($"Step {step} loss: {loss_value}");

0 commit comments

Comments
 (0)