|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Text; |
4 | | -using NumSharp; |
5 | | -using Tensorflow; |
6 | | -using TensorFlowNET.Examples.Utility; |
7 | | -using static Tensorflow.Python; |
8 | | - |
9 | | -namespace TensorFlowNET.Examples |
10 | | -{ |
11 | | - /// <summary> |
12 | | - /// Simple vanilla neural net solving the famous XOR problem |
13 | | - /// https://github.com/amygdala/tensorflow-workshop/blob/master/workshop_sections/getting_started/xor/README.md |
14 | | - /// </summary> |
15 | | - public class NeuralNetXor : IExample |
16 | | - { |
17 | | - public int Priority => 10; |
18 | | - public bool Enabled { get; set; } = true; |
19 | | - public string Name => "NN XOR"; |
20 | | - public bool ImportGraph { get; set; } = false; |
21 | | - |
22 | | - public int num_steps = 10000; |
23 | | - |
24 | | - private NDArray data; |
25 | | - |
26 | | - private (Operation, Tensor, Tensor) make_graph(Tensor features,Tensor labels, int num_hidden = 8) |
27 | | - { |
28 | | - var stddev = 1 / Math.Sqrt(2); |
29 | | - var hidden_weights = tf.Variable(tf.truncated_normal(new int []{2, num_hidden}, seed:1, stddev: (float) stddev )); |
30 | | - |
31 | | - // Shape [4, num_hidden] |
32 | | - var hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights)); |
33 | | - |
34 | | - var output_weights = tf.Variable(tf.truncated_normal( |
35 | | - new[] {num_hidden, 1}, |
36 | | - seed: 17, |
37 | | - stddev: (float) (1 / Math.Sqrt(num_hidden)) |
38 | | - )); |
39 | | - |
40 | | - // Shape [4, 1] |
41 | | - var logits = tf.matmul(hidden_activations, output_weights); |
42 | | - |
43 | | - // Shape [4] |
44 | | - var predictions = tf.sigmoid(tf.squeeze(logits)); |
45 | | - var loss = tf.reduce_mean(tf.square(predictions - tf.cast(labels, tf.float32)), name:"loss"); |
46 | | - |
47 | | - var gs = tf.Variable(0, trainable: false, name: "global_step"); |
48 | | - var train_op = tf.train.GradientDescentOptimizer(0.2f).minimize(loss, global_step: gs); |
49 | | - |
50 | | - return (train_op, loss, gs); |
51 | | - } |
52 | | - |
53 | | - public bool Run() |
54 | | - { |
55 | | - PrepareData(); |
56 | | - float loss_value = 0; |
57 | | - if (ImportGraph) |
58 | | - loss_value = RunWithImportedGraph(); |
59 | | - else |
60 | | - loss_value = RunWithBuiltGraph(); |
61 | | - |
62 | | - return loss_value < 0.0628; |
63 | | - } |
64 | | - |
65 | | - private float RunWithImportedGraph() |
66 | | - { |
67 | | - var graph = tf.Graph().as_default(); |
68 | | - |
69 | | - tf.train.import_meta_graph("graph/xor.meta"); |
70 | | - |
71 | | - Tensor features = graph.get_operation_by_name("Placeholder"); |
72 | | - Tensor labels = graph.get_operation_by_name("Placeholder_1"); |
73 | | - Tensor loss = graph.get_operation_by_name("loss"); |
74 | | - Tensor train_op = graph.get_operation_by_name("train_op"); |
75 | | - Tensor global_step = graph.get_operation_by_name("global_step"); |
76 | | - |
77 | | - var init = tf.global_variables_initializer(); |
78 | | - float loss_value = 0; |
79 | | - // Start tf session |
80 | | - with(tf.Session(graph), sess => |
81 | | - { |
82 | | - sess.run(init); |
83 | | - var step = 0; |
84 | | - |
85 | | - var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32); |
86 | | - while (step < num_steps) |
87 | | - { |
88 | | - // original python: |
89 | | - //_, step, loss_value = sess.run( |
90 | | - // [train_op, gs, loss], |
91 | | - // feed_dict={features: xy, labels: y_} |
92 | | - // ) |
93 | | - var result = sess.run(new ITensorOrOperation[] { train_op, global_step, loss }, new FeedItem(features, data), new FeedItem(labels, y_)); |
94 | | - loss_value = result[2]; |
95 | | - step = result[1]; |
96 | | - if (step % 1000 == 0) |
97 | | - Console.WriteLine($"Step {step} loss: {loss_value}"); |
98 | | - } |
99 | | - Console.WriteLine($"Final loss: {loss_value}"); |
100 | | - }); |
101 | | - |
102 | | - return loss_value; |
103 | | - } |
104 | | - |
105 | | - private float RunWithBuiltGraph() |
106 | | - { |
107 | | - var graph = tf.Graph().as_default(); |
108 | | - |
109 | | - var features = tf.placeholder(tf.float32, new TensorShape(4, 2)); |
110 | | - var labels = tf.placeholder(tf.int32, new TensorShape(4)); |
111 | | - |
112 | | - var (train_op, loss, gs) = make_graph(features, labels); |
113 | | - |
114 | | - var init = tf.global_variables_initializer(); |
115 | | - |
116 | | - float loss_value = 0; |
117 | | - // Start tf session |
118 | | - with(tf.Session(graph), sess => |
119 | | - { |
120 | | - sess.run(init); |
121 | | - var step = 0; |
122 | | - |
123 | | - var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32); |
124 | | - while (step < num_steps) |
125 | | - { |
126 | | - var result = sess.run(new ITensorOrOperation[] { train_op, gs, loss }, new FeedItem(features, data), new FeedItem(labels, y_)); |
127 | | - loss_value = result[2]; |
128 | | - step = result[1]; |
129 | | - if (step % 1000 == 0) |
130 | | - Console.WriteLine($"Step {step} loss: {loss_value}"); |
131 | | - } |
132 | | - Console.WriteLine($"Final loss: {loss_value}"); |
133 | | - }); |
134 | | - |
135 | | - return loss_value; |
136 | | - } |
137 | | - |
138 | | - public void PrepareData() |
139 | | - { |
140 | | - data = new float[,] |
141 | | - { |
142 | | - {1, 0 }, |
143 | | - {1, 1 }, |
144 | | - {0, 0 }, |
145 | | - {0, 1 } |
146 | | - }; |
147 | | - |
148 | | - if (ImportGraph) |
149 | | - { |
150 | | - // download graph meta data |
151 | | - string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/xor.meta"; |
152 | | - Web.Download(url, "graph", "xor.meta"); |
153 | | - } |
154 | | - } |
155 | | - } |
156 | | -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using NumSharp; |
| 5 | +using Tensorflow; |
| 6 | +using TensorFlowNET.Examples.Utility; |
| 7 | +using static Tensorflow.Python; |
| 8 | + |
| 9 | +namespace TensorFlowNET.Examples |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Simple vanilla neural net solving the famous XOR problem |
| 13 | + /// https://github.com/amygdala/tensorflow-workshop/blob/master/workshop_sections/getting_started/xor/README.md |
| 14 | + /// </summary> |
| 15 | + public class NeuralNetXor : IExample |
| 16 | + { |
| 17 | + public int Priority => 10; |
| 18 | + public bool Enabled { get; set; } = true; |
| 19 | + public string Name => "NN XOR"; |
| 20 | + public bool ImportGraph { get; set; } = false; |
| 21 | + |
| 22 | + public int num_steps = 10000; |
| 23 | + |
| 24 | + private NDArray data; |
| 25 | + |
| 26 | + private (Operation, Tensor, Tensor) make_graph(Tensor features,Tensor labels, int num_hidden = 8) |
| 27 | + { |
| 28 | + var stddev = 1 / Math.Sqrt(2); |
| 29 | + var hidden_weights = tf.Variable(tf.truncated_normal(new int []{2, num_hidden}, seed:1, stddev: (float) stddev )); |
| 30 | + |
| 31 | + // Shape [4, num_hidden] |
| 32 | + var hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights)); |
| 33 | + |
| 34 | + var output_weights = tf.Variable(tf.truncated_normal( |
| 35 | + new[] {num_hidden, 1}, |
| 36 | + seed: 17, |
| 37 | + stddev: (float) (1 / Math.Sqrt(num_hidden)) |
| 38 | + )); |
| 39 | + |
| 40 | + // Shape [4, 1] |
| 41 | + var logits = tf.matmul(hidden_activations, output_weights); |
| 42 | + |
| 43 | + // Shape [4] |
| 44 | + var predictions = tf.sigmoid(tf.squeeze(logits)); |
| 45 | + var loss = tf.reduce_mean(tf.square(predictions - tf.cast(labels, tf.float32)), name:"loss"); |
| 46 | + |
| 47 | + var gs = tf.Variable(0, trainable: false, name: "global_step"); |
| 48 | + var train_op = tf.train.GradientDescentOptimizer(0.2f).minimize(loss, global_step: gs); |
| 49 | + |
| 50 | + return (train_op, loss, gs); |
| 51 | + } |
| 52 | + |
| 53 | + public bool Run() |
| 54 | + { |
| 55 | + PrepareData(); |
| 56 | + float loss_value = 0; |
| 57 | + if (ImportGraph) |
| 58 | + loss_value = RunWithImportedGraph(); |
| 59 | + else |
| 60 | + loss_value = RunWithBuiltGraph(); |
| 61 | + |
| 62 | + return loss_value < 0.0628; |
| 63 | + } |
| 64 | + |
| 65 | + private float RunWithImportedGraph() |
| 66 | + { |
| 67 | + var graph = tf.Graph().as_default(); |
| 68 | + |
| 69 | + tf.train.import_meta_graph("graph/xor.meta"); |
| 70 | + |
| 71 | + Tensor features = graph.get_operation_by_name("Placeholder"); |
| 72 | + Tensor labels = graph.get_operation_by_name("Placeholder_1"); |
| 73 | + Tensor loss = graph.get_operation_by_name("loss"); |
| 74 | + Tensor train_op = graph.get_operation_by_name("train_op"); |
| 75 | + Tensor global_step = graph.get_operation_by_name("global_step"); |
| 76 | + |
| 77 | + var init = tf.global_variables_initializer(); |
| 78 | + float loss_value = 0; |
| 79 | + // Start tf session |
| 80 | + with(tf.Session(graph), sess => |
| 81 | + { |
| 82 | + sess.run(init); |
| 83 | + var step = 0; |
| 84 | + |
| 85 | + var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32); |
| 86 | + while (step < num_steps) |
| 87 | + { |
| 88 | + // original python: |
| 89 | + //_, step, loss_value = sess.run( |
| 90 | + // [train_op, gs, loss], |
| 91 | + // feed_dict={features: xy, labels: y_} |
| 92 | + // ) |
| 93 | + var result = sess.run(new ITensorOrOperation[] { train_op, global_step, loss }, new FeedItem(features, data), new FeedItem(labels, y_)); |
| 94 | + loss_value = result[2]; |
| 95 | + step = result[1]; |
| 96 | + if (step % 1000 == 0) |
| 97 | + Console.WriteLine($"Step {step} loss: {loss_value}"); |
| 98 | + } |
| 99 | + Console.WriteLine($"Final loss: {loss_value}"); |
| 100 | + }); |
| 101 | + |
| 102 | + return loss_value; |
| 103 | + } |
| 104 | + |
| 105 | + private float RunWithBuiltGraph() |
| 106 | + { |
| 107 | + var graph = tf.Graph().as_default(); |
| 108 | + |
| 109 | + var features = tf.placeholder(tf.float32, new TensorShape(4, 2)); |
| 110 | + var labels = tf.placeholder(tf.int32, new TensorShape(4)); |
| 111 | + |
| 112 | + var (train_op, loss, gs) = make_graph(features, labels); |
| 113 | + |
| 114 | + var init = tf.global_variables_initializer(); |
| 115 | + |
| 116 | + float loss_value = 0; |
| 117 | + // Start tf session |
| 118 | + with(tf.Session(graph), sess => |
| 119 | + { |
| 120 | + sess.run(init); |
| 121 | + var step = 0; |
| 122 | + |
| 123 | + var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32); |
| 124 | + while (step < num_steps) |
| 125 | + { |
| 126 | + var result = sess.run(new ITensorOrOperation[] { train_op, gs, loss }, new FeedItem(features, data), new FeedItem(labels, y_)); |
| 127 | + loss_value = result[2]; |
| 128 | + step = result[1]; |
| 129 | + if (step % 1000 == 0) |
| 130 | + Console.WriteLine($"Step {step} loss: {loss_value}"); |
| 131 | + } |
| 132 | + Console.WriteLine($"Final loss: {loss_value}"); |
| 133 | + }); |
| 134 | + |
| 135 | + return loss_value; |
| 136 | + } |
| 137 | + |
| 138 | + public void PrepareData() |
| 139 | + { |
| 140 | + data = new float[,] |
| 141 | + { |
| 142 | + {1, 0 }, |
| 143 | + {1, 1 }, |
| 144 | + {0, 0 }, |
| 145 | + {0, 1 } |
| 146 | + }; |
| 147 | + |
| 148 | + if (ImportGraph) |
| 149 | + { |
| 150 | + // download graph meta data |
| 151 | + string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/xor.meta"; |
| 152 | + Web.Download(url, "graph", "xor.meta"); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments