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.

NaiveBayesClassifier.cs 4.1 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
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Tensorflow;
  5. using NumSharp.Core;
  6. using System.Linq;
  7. namespace TensorFlowNET.Examples
  8. {
  9. /// <summary>
  10. /// https://github.com/nicolov/naive_bayes_tensorflow
  11. /// </summary>
  12. public class NaiveBayesClassifier : Python, IExample
  13. {
  14. public Normal dist { get; set; }
  15. public void Run()
  16. {
  17. np.array<float>(1.0f, 1.0f);
  18. var X = np.array<float>(new float[][] { new float[] { 1.0f, 1.0f }, new float[] { 2.0f, 2.0f }, new float[] { -1.0f, -1.0f }, new float[] { -2.0f, -2.0f }, new float[] { 1.0f, -1.0f }, new float[] { 2.0f, -2.0f }, });
  19. var y = np.array<int>(0,0,1,1,2,2);
  20. fit(X, y);
  21. // Create a regular grid and classify each point
  22. }
  23. public void fit(NDArray X, NDArray y)
  24. {
  25. NDArray unique_y = y.unique<long>();
  26. Dictionary<long, List<List<float>>> dic = new Dictionary<long, List<List<float>>>();
  27. // Init uy in dic
  28. foreach (int uy in unique_y.Data<int>())
  29. {
  30. dic.Add(uy, new List<List<float>>());
  31. }
  32. // Separate training points by class
  33. // Shape : nb_classes * nb_samples * nb_features
  34. int maxCount = 0;
  35. for (int i = 0; i < y.size; i++)
  36. {
  37. long curClass = (long)y[i];
  38. List<List<float>> l = dic[curClass];
  39. List<float> pair = new List<float>();
  40. pair.Add((float)X[i,0]);
  41. pair.Add((float)X[i, 1]);
  42. l.Add(pair);
  43. if (l.Count > maxCount)
  44. {
  45. maxCount = l.Count;
  46. }
  47. dic[curClass] = l;
  48. }
  49. float[,,] points = new float[dic.Count, maxCount, X.shape[1]];
  50. foreach (KeyValuePair<long, List<List<float>>> kv in dic)
  51. {
  52. int j = (int) kv.Key;
  53. for (int i = 0; i < maxCount; i++)
  54. {
  55. for (int k = 0; k < X.shape[1]; k++)
  56. {
  57. points[j, i, k] = kv.Value[i][k];
  58. }
  59. }
  60. }
  61. NDArray points_by_class = np.array<float>(points);
  62. // estimate mean and variance for each class / feature
  63. // shape : nb_classes * nb_features
  64. var cons = tf.constant(points_by_class);
  65. var tup = tf.nn.moments(cons, new int[]{1});
  66. var mean = tup.Item1;
  67. var variance = tup.Item2;
  68. // Create a 3x2 univariate normal distribution with the
  69. // Known mean and variance
  70. var dist = tf.distributions.Normal(mean, tf.sqrt(variance));
  71. this.dist = dist;
  72. }
  73. public Tensor predict (NDArray X)
  74. {
  75. if (dist == null)
  76. {
  77. throw new ArgumentNullException("cant not find the model (normal distribution)!");
  78. }
  79. int nb_classes = (int) dist.scale().shape[0];
  80. int nb_features = (int)dist.scale().shape[1];
  81. // Conditional probabilities log P(x|c) with shape
  82. // (nb_samples, nb_classes)
  83. Tensor tile = tf.tile(new Tensor(X), new Tensor(new int[] { -1, nb_classes, nb_features }));
  84. Tensor r = tf.reshape(tile, new Tensor(new int[] { -1, nb_classes, nb_features }));
  85. var cond_probs = tf.reduce_sum(dist.log_prob(r));
  86. // uniform priors
  87. var priors = np.log(np.array<double>((1.0 / nb_classes) * nb_classes));
  88. // posterior log probability, log P(c) + log P(x|c)
  89. var joint_likelihood = tf.add(new Tensor(priors), cond_probs);
  90. // normalize to get (log)-probabilities
  91. var norm_factor = tf.reduce_logsumexp(joint_likelihood, new int[] { 1 }, true);
  92. var log_prob = joint_likelihood - norm_factor;
  93. // exp to get the actual probabilities
  94. return tf.exp(log_prob);
  95. }
  96. }
  97. }

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