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 2.9 kB

6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Newtonsoft.Json;
  2. using NumSharp.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using Tensorflow;
  8. using TensorFlowNET.Examples.Utility;
  9. namespace TensorFlowNET.Examples
  10. {
  11. /// <summary>
  12. /// A logistic regression learning algorithm example using TensorFlow library.
  13. /// This example is using the MNIST database of handwritten digits
  14. /// https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/2_BasicModels/logistic_regression.py
  15. /// </summary>
  16. public class LogisticRegression : Python, IExample
  17. {
  18. private float learning_rate = 0.01f;
  19. private int training_epochs = 25;
  20. private int batch_size = 100;
  21. private int display_step = 1;
  22. public void Run()
  23. {
  24. PrepareData();
  25. }
  26. private void PrepareData()
  27. {
  28. // tf Graph Input
  29. var x = tf.placeholder(tf.float32, new TensorShape(-1, 784)); // mnist data image of shape 28*28=784
  30. var y = tf.placeholder(tf.float32, new TensorShape(-1, 10)); // 0-9 digits recognition => 10 classes
  31. // Set model weights
  32. var W = tf.Variable(tf.zeros(new Shape(784, 10)));
  33. var b = tf.Variable(tf.zeros(new Shape(10)));
  34. // Construct model
  35. var pred = tf.nn.softmax(tf.matmul(x, W) + b); // Softmax
  36. // Minimize error using cross entropy
  37. var log = tf.log(pred);
  38. var mul = y * log;
  39. var sum = tf.reduce_sum(mul, reduction_indices: 1);
  40. var neg = -sum;
  41. var cost = tf.reduce_mean(neg);
  42. // Gradient Descent
  43. var optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost);
  44. // Initialize the variables (i.e. assign their default value)
  45. var init = tf.global_variables_initializer();
  46. with(tf.Session(), sess =>
  47. {
  48. var mnist = MnistDataSet.read_data_sets("logistic_regression", one_hot: true);
  49. // Run the initializer
  50. sess.run(init);
  51. // Training cycle
  52. foreach(var epoch in range(training_epochs))
  53. {
  54. var avg_cost = 0.0f;
  55. var total_batch = (int)(mnist.train.num_examples / batch_size);
  56. // Loop over all batches
  57. foreach (var i in range(total_batch))
  58. {
  59. var (batch_xs, batch_ys) = mnist.train.next_batch(batch_size);
  60. // Run optimization op (backprop) and cost op (to get loss value)
  61. /*sess.run(optimizer,
  62. new FeedItem(x, batch_xs),
  63. new FeedItem(y, batch_ys));*/
  64. }
  65. }
  66. });
  67. }
  68. }
  69. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。