|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using NumSharp; |
| 5 | +using Tensorflow; |
| 6 | + |
| 7 | +namespace TensorFlowNET.Examples |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Simple vanilla neural net solving the famous XOR problem |
| 11 | + /// https://github.com/amygdala/tensorflow-workshop/blob/master/workshop_sections/getting_started/xor/README.md |
| 12 | + /// </summary> |
| 13 | + public class NeuralNetXor : Python, IExample |
| 14 | + { |
| 15 | + public int Priority => 2; |
| 16 | + public bool Enabled { get; set; } = true; |
| 17 | + public string Name => "NN XOR"; |
| 18 | + |
| 19 | + public int num_steps = 5000; |
| 20 | + |
| 21 | + private (Operation, Tensor, RefVariable) make_graph(Tensor features,Tensor labels, int num_hidden = 8) |
| 22 | + { |
| 23 | + var stddev = 1 / Math.Sqrt(2); |
| 24 | + var hidden_weights = tf.Variable(tf.truncated_normal(new int []{2, num_hidden}, stddev: (float) stddev )); |
| 25 | + |
| 26 | + // Shape [4, num_hidden] |
| 27 | + var hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights)); |
| 28 | + |
| 29 | + var output_weights = tf.Variable(tf.truncated_normal( |
| 30 | + new[] {num_hidden, 1}, |
| 31 | + stddev: (float) (1 / Math.Sqrt(num_hidden)) |
| 32 | + )); |
| 33 | + |
| 34 | + // Shape [4, 1] |
| 35 | + var logits = tf.matmul(hidden_activations, output_weights); |
| 36 | + |
| 37 | + // Shape [4] |
| 38 | + var predictions = tf.sigmoid(tf.squeeze(logits)); |
| 39 | + var loss = tf.reduce_mean(tf.square(predictions - tf.cast(labels, tf.float32))); |
| 40 | + |
| 41 | + var gs = tf.Variable(0, trainable: false); |
| 42 | + var train_op = tf.train.GradientDescentOptimizer(0.2f).minimize(loss, global_step: gs); |
| 43 | + |
| 44 | + return (train_op, loss, gs); |
| 45 | + } |
| 46 | + |
| 47 | + public bool Run() |
| 48 | + { |
| 49 | + |
| 50 | + var graph = tf.Graph(); |
| 51 | + |
| 52 | + var init=with(graph.as_default(), g => |
| 53 | + { |
| 54 | + var features = tf.placeholder(tf.float32, new TensorShape(4, 2)); |
| 55 | + var labels = tf.placeholder(tf.int32, new TensorShape(4)); |
| 56 | + |
| 57 | + var (train_op, loss, gs) = make_graph(features, labels); |
| 58 | + return tf.global_variables_initializer(); |
| 59 | + }); |
| 60 | + |
| 61 | + // Start tf session |
| 62 | + with<Session>(tf.Session(), sess => |
| 63 | + { |
| 64 | + init.run(); |
| 65 | + var step = 0; |
| 66 | + var xy = np.array(new bool[,] |
| 67 | + { |
| 68 | + {true, false, }, |
| 69 | + {true, true, }, |
| 70 | + {false, false, }, |
| 71 | + {false, true, }, |
| 72 | + }, dtype: np.float32); |
| 73 | + |
| 74 | + var y_ = np.array(new[] {true, false, false, true}, dtype: np.int32); |
| 75 | + while (step < num_steps) |
| 76 | + { |
| 77 | + // original python: |
| 78 | + //_, step, loss_value = sess.run( |
| 79 | + // [train_op, gs, loss], |
| 80 | + // feed_dict={features: xy, labels: y_} |
| 81 | + // ) |
| 82 | + // TODO: how the hell to port that to c#? |
| 83 | + // var ( _, step, loss_value) = sess.run(new object[] {train_op, gs, loss},feed_dict: new {"features": xy, "labels": y_}); |
| 84 | + } |
| 85 | + //tf.logging.info('Final loss is: {}'.format(loss_value)) |
| 86 | + //Console.WriteLine($"Final loss is: {loss_value}"); |
| 87 | + |
| 88 | + }); |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + public void PrepareData() |
| 93 | + { |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments