You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

NeuralNetXor.cs 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Text;
  16. using NumSharp;
  17. using Tensorflow;
  18. using TensorFlowNET.Examples.Utility;
  19. using static Tensorflow.Python;
  20. namespace TensorFlowNET.Examples
  21. {
  22. /// <summary>
  23. /// Simple vanilla neural net solving the famous XOR problem
  24. /// https://github.com/amygdala/tensorflow-workshop/blob/master/workshop_sections/getting_started/xor/README.md
  25. /// </summary>
  26. public class NeuralNetXor : IExample
  27. {
  28. public bool Enabled { get; set; } = true;
  29. public string Name => "NN XOR";
  30. public bool IsImportingGraph { get; set; } = false;
  31. public int num_steps = 10000;
  32. private NDArray data;
  33. private (Operation, Tensor, Tensor) make_graph(Tensor features,Tensor labels, int num_hidden = 8)
  34. {
  35. var stddev = 1 / Math.Sqrt(2);
  36. var hidden_weights = tf.Variable(tf.truncated_normal(new int []{2, num_hidden}, seed:1, stddev: (float) stddev ));
  37. // Shape [4, num_hidden]
  38. var hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights));
  39. var output_weights = tf.Variable(tf.truncated_normal(
  40. new[] {num_hidden, 1},
  41. seed: 17,
  42. stddev: (float) (1 / Math.Sqrt(num_hidden))
  43. ));
  44. // Shape [4, 1]
  45. var logits = tf.matmul(hidden_activations, output_weights);
  46. // Shape [4]
  47. var predictions = tf.sigmoid(tf.squeeze(logits));
  48. var loss = tf.reduce_mean(tf.square(predictions - tf.cast(labels, tf.float32)), name:"loss");
  49. var gs = tf.Variable(0, trainable: false, name: "global_step");
  50. var train_op = tf.train.GradientDescentOptimizer(0.2f).minimize(loss, global_step: gs);
  51. return (train_op, loss, gs);
  52. }
  53. public bool Run()
  54. {
  55. PrepareData();
  56. float loss_value = 0;
  57. if (IsImportingGraph)
  58. loss_value = RunWithImportedGraph();
  59. else
  60. loss_value = RunWithBuiltGraph();
  61. return loss_value < 0.0628;
  62. }
  63. private float RunWithImportedGraph()
  64. {
  65. var graph = tf.Graph().as_default();
  66. tf.train.import_meta_graph("graph/xor.meta");
  67. Tensor features = graph.get_operation_by_name("Placeholder");
  68. Tensor labels = graph.get_operation_by_name("Placeholder_1");
  69. Tensor loss = graph.get_operation_by_name("loss");
  70. Tensor train_op = graph.get_operation_by_name("train_op");
  71. Tensor global_step = graph.get_operation_by_name("global_step");
  72. var init = tf.global_variables_initializer();
  73. float loss_value = 0;
  74. // Start tf session
  75. with(tf.Session(graph), sess =>
  76. {
  77. sess.run(init);
  78. var step = 0;
  79. var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32);
  80. while (step < num_steps)
  81. {
  82. // original python:
  83. //_, step, loss_value = sess.run(
  84. // [train_op, gs, loss],
  85. // feed_dict={features: xy, labels: y_}
  86. // )
  87. var result = sess.run(new ITensorOrOperation[] { train_op, global_step, loss }, new FeedItem(features, data), new FeedItem(labels, y_));
  88. loss_value = result[2];
  89. step = result[1];
  90. if (step % 1000 == 0)
  91. Console.WriteLine($"Step {step} loss: {loss_value}");
  92. }
  93. Console.WriteLine($"Final loss: {loss_value}");
  94. });
  95. return loss_value;
  96. }
  97. private float RunWithBuiltGraph()
  98. {
  99. var graph = tf.Graph().as_default();
  100. var features = tf.placeholder(tf.float32, new TensorShape(4, 2));
  101. var labels = tf.placeholder(tf.int32, new TensorShape(4));
  102. var (train_op, loss, gs) = make_graph(features, labels);
  103. var init = tf.global_variables_initializer();
  104. float loss_value = 0;
  105. // Start tf session
  106. with(tf.Session(graph), sess =>
  107. {
  108. sess.run(init);
  109. var step = 0;
  110. var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32);
  111. while (step < num_steps)
  112. {
  113. var result = sess.run(new ITensorOrOperation[] { train_op, gs, loss }, new FeedItem(features, data), new FeedItem(labels, y_));
  114. loss_value = result[2];
  115. step = result[1];
  116. if (step % 1000 == 0)
  117. Console.WriteLine($"Step {step} loss: {loss_value}");
  118. }
  119. Console.WriteLine($"Final loss: {loss_value}");
  120. });
  121. return loss_value;
  122. }
  123. public void PrepareData()
  124. {
  125. data = new float[,]
  126. {
  127. {1, 0 },
  128. {1, 1 },
  129. {0, 0 },
  130. {0, 1 }
  131. };
  132. if (IsImportingGraph)
  133. {
  134. // download graph meta data
  135. string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/xor.meta";
  136. Web.Download(url, "graph", "xor.meta");
  137. }
  138. }
  139. public Graph ImportGraph()
  140. {
  141. throw new NotImplementedException();
  142. }
  143. public Graph BuildGraph()
  144. {
  145. throw new NotImplementedException();
  146. }
  147. public void Train(Session sess)
  148. {
  149. throw new NotImplementedException();
  150. }
  151. public void Predict(Session sess)
  152. {
  153. throw new NotImplementedException();
  154. }
  155. public void Test(Session sess)
  156. {
  157. throw new NotImplementedException();
  158. }
  159. }
  160. }