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.

KMeansClustering.cs 1.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using NumSharp.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Tensorflow;
  6. using Tensorflow.Clustering;
  7. using TensorFlowNET.Examples.Utility;
  8. namespace TensorFlowNET.Examples
  9. {
  10. /// <summary>
  11. /// Implement K-Means algorithm with TensorFlow.NET, and apply it to classify
  12. /// handwritten digit images.
  13. /// https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/2_BasicModels/kmeans.py
  14. /// </summary>
  15. public class KMeansClustering : Python, IExample
  16. {
  17. public int Priority => 8;
  18. public bool Enabled => false;
  19. public string Name => "K-means Clustering";
  20. Datasets mnist;
  21. NDArray full_data_x;
  22. int num_steps = 50; // Total steps to train
  23. int batch_size = 1024; // The number of samples per batch
  24. int k = 25; // The number of clusters
  25. int num_classes = 10; // The 10 digits
  26. int num_features = 784; // Each image is 28x28 pixels
  27. public bool Run()
  28. {
  29. // Input images
  30. var X = tf.placeholder(tf.float32, shape: new TensorShape(-1, num_features));
  31. // Labels (for assigning a label to a centroid and testing)
  32. var Y = tf.placeholder(tf.float32, shape: new TensorShape(-1, num_classes));
  33. // K-Means Parameters
  34. var kmeans = new KMeans(X, k, distance_metric: KMeans.COSINE_DISTANCE, use_mini_batch: true);
  35. // Build KMeans graph
  36. var training_graph = kmeans.training_graph();
  37. return false;
  38. }
  39. public void PrepareData()
  40. {
  41. mnist = MnistDataSet.read_data_sets("mnist", one_hot: true);
  42. full_data_x = mnist.train.images;
  43. }
  44. }
  45. }

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