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