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.

LogisticRegression.cs 7.4 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 NumSharp;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using Tensorflow;
  21. using TensorFlowNET.Examples.Utility;
  22. using static Tensorflow.Python;
  23. namespace TensorFlowNET.Examples
  24. {
  25. /// <summary>
  26. /// A logistic regression learning algorithm example using TensorFlow library.
  27. /// This example is using the MNIST database of handwritten digits
  28. /// https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/2_BasicModels/logistic_regression.py
  29. /// </summary>
  30. public class LogisticRegression : IExample
  31. {
  32. public bool Enabled { get; set; } = true;
  33. public string Name => "Logistic Regression";
  34. public bool IsImportingGraph { get; set; } = false;
  35. public int training_epochs = 10;
  36. public int? train_size = null;
  37. public int validation_size = 5000;
  38. public int? test_size = null;
  39. public int batch_size = 100;
  40. private float learning_rate = 0.01f;
  41. private int display_step = 1;
  42. Datasets<DataSetMnist> mnist;
  43. public bool Run()
  44. {
  45. PrepareData();
  46. // tf Graph Input
  47. var x = tf.placeholder(tf.float32, new TensorShape(-1, 784)); // mnist data image of shape 28*28=784
  48. var y = tf.placeholder(tf.float32, new TensorShape(-1, 10)); // 0-9 digits recognition => 10 classes
  49. // Set model weights
  50. var W = tf.Variable(tf.zeros(new Shape(784, 10)));
  51. var b = tf.Variable(tf.zeros(new Shape(10)));
  52. // Construct model
  53. var pred = tf.nn.softmax(tf.matmul(x, W) + b); // Softmax
  54. // Minimize error using cross entropy
  55. var cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices: 1));
  56. // Gradient Descent
  57. var optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost);
  58. // Initialize the variables (i.e. assign their default value)
  59. var init = tf.global_variables_initializer();
  60. var sw = new Stopwatch();
  61. return with(tf.Session(), sess =>
  62. {
  63. // Run the initializer
  64. sess.run(init);
  65. // Training cycle
  66. foreach (var epoch in range(training_epochs))
  67. {
  68. sw.Start();
  69. var avg_cost = 0.0f;
  70. var total_batch = mnist.train.num_examples / batch_size;
  71. // Loop over all batches
  72. foreach (var i in range(total_batch))
  73. {
  74. var (batch_xs, batch_ys) = mnist.train.next_batch(batch_size);
  75. // Run optimization op (backprop) and cost op (to get loss value)
  76. var result = sess.run(new object[] { optimizer, cost },
  77. new FeedItem(x, batch_xs),
  78. new FeedItem(y, batch_ys));
  79. float c = result[1];
  80. // Compute average loss
  81. avg_cost += c / total_batch;
  82. }
  83. sw.Stop();
  84. // Display logs per epoch step
  85. if ((epoch + 1) % display_step == 0)
  86. print($"Epoch: {(epoch + 1).ToString("D4")} Cost: {avg_cost.ToString("G9")} Elapse: {sw.ElapsedMilliseconds}ms");
  87. sw.Reset();
  88. }
  89. print("Optimization Finished!");
  90. // SaveModel(sess);
  91. // Test model
  92. var correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1));
  93. // Calculate accuracy
  94. var accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32));
  95. float acc = accuracy.eval(new FeedItem(x, mnist.test.data), new FeedItem(y, mnist.test.labels));
  96. print($"Accuracy: {acc.ToString("F4")}");
  97. return acc > 0.9;
  98. });
  99. }
  100. public void PrepareData()
  101. {
  102. mnist = MNIST.read_data_sets("mnist", one_hot: true, train_size: train_size, validation_size: validation_size, test_size: test_size);
  103. }
  104. public void SaveModel(Session sess)
  105. {
  106. var saver = tf.train.Saver();
  107. var save_path = saver.save(sess, "logistic_regression/model.ckpt");
  108. tf.train.write_graph(sess.graph, "logistic_regression", "model.pbtxt", as_text: true);
  109. FreezeGraph.freeze_graph(input_graph: "logistic_regression/model.pbtxt",
  110. input_saver: "",
  111. input_binary: false,
  112. input_checkpoint: "logistic_regression/model.ckpt",
  113. output_node_names: "Softmax",
  114. restore_op_name: "save/restore_all",
  115. filename_tensor_name: "save/Const:0",
  116. output_graph: "logistic_regression/model.pb",
  117. clear_devices: true,
  118. initializer_nodes: "");
  119. }
  120. public void Predict(Session sess)
  121. {
  122. var graph = new Graph().as_default();
  123. graph.Import(Path.Join("logistic_regression", "model.pb"));
  124. // restoring the model
  125. // var saver = tf.train.import_meta_graph("logistic_regression/tensorflowModel.ckpt.meta");
  126. // saver.restore(sess, tf.train.latest_checkpoint('logistic_regression'));
  127. var pred = graph.OperationByName("Softmax");
  128. var output = pred.outputs[0];
  129. var x = graph.OperationByName("Placeholder");
  130. var input = x.outputs[0];
  131. // predict
  132. var (batch_xs, batch_ys) = mnist.train.next_batch(10);
  133. var results = sess.run(output, new FeedItem(input, batch_xs[np.arange(1)]));
  134. if (results.argmax() == (batch_ys[0] as NDArray).argmax())
  135. print("predicted OK!");
  136. else
  137. throw new ValueError("predict error, should be 90% accuracy");
  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 Test(Session sess)
  152. {
  153. throw new NotImplementedException();
  154. }
  155. }
  156. }