kmeans.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """ K-Means.
  2. Implement K-Means algorithm with TensorFlow, and apply it to classify
  3. handwritten digit images. This example is using the MNIST database of
  4. handwritten digits as training samples (http://yann.lecun.com/exdb/mnist/).
  5. Note: This example requires TensorFlow v1.1.0 or over.
  6. Author: Aymeric Damien
  7. Project: https://github.com/aymericdamien/TensorFlow-Examples/
  8. """
  9. from __future__ import print_function
  10. import numpy as np
  11. import tensorflow as tf
  12. from tensorflow.contrib.factorization import KMeans
  13. # Ignore all GPUs, tf k-means does not benefit from it.
  14. import os
  15. os.environ["CUDA_VISIBLE_DEVICES"] = ""
  16. # Import MNIST data
  17. from tensorflow.examples.tutorials.mnist import input_data
  18. mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
  19. full_data_x = mnist.train.images
  20. # Parameters
  21. num_steps = 50 # Total steps to train
  22. batch_size = 1024 # The number of samples per batch
  23. k = 25 # The number of clusters
  24. num_classes = 10 # The 10 digits
  25. num_features = 784 # Each image is 28x28 pixels
  26. # Input images
  27. X = tf.placeholder(tf.float32, shape=[None, num_features])
  28. # Labels (for assigning a label to a centroid and testing)
  29. Y = tf.placeholder(tf.float32, shape=[None, num_classes])
  30. # K-Means Parameters
  31. kmeans = KMeans(inputs=X, num_clusters=k, distance_metric='cosine',
  32. use_mini_batch=True)
  33. # Build KMeans graph
  34. training_graph = kmeans.training_graph()
  35. if len(training_graph) > 6: # Tensorflow 1.4+
  36. (all_scores, cluster_idx, scores, cluster_centers_initialized,
  37. cluster_centers_var, init_op, train_op) = training_graph
  38. else:
  39. (all_scores, cluster_idx, scores, cluster_centers_initialized,
  40. init_op, train_op) = training_graph
  41. cluster_idx = cluster_idx[0] # fix for cluster_idx being a tuple
  42. avg_distance = tf.reduce_mean(scores)
  43. # Initialize the variables (i.e. assign their default value)
  44. init_vars = tf.global_variables_initializer()
  45. # Start TensorFlow session
  46. sess = tf.Session()
  47. # Run the initializer
  48. sess.run(init_vars, feed_dict={X: full_data_x})
  49. sess.run(init_op, feed_dict={X: full_data_x})
  50. # Training
  51. for i in range(1, num_steps + 1):
  52. _, d, idx = sess.run([train_op, avg_distance, cluster_idx],
  53. feed_dict={X: full_data_x})
  54. if i % 10 == 0 or i == 1:
  55. print("Step %i, Avg Distance: %f" % (i, d))
  56. # Assign a label to each centroid
  57. # Count total number of labels per centroid, using the label of each training
  58. # sample to their closest centroid (given by 'idx')
  59. counts = np.zeros(shape=(k, num_classes))
  60. for i in range(len(idx)):
  61. counts[idx[i]] += mnist.train.labels[i]
  62. # Assign the most frequent label to the centroid
  63. labels_map = [np.argmax(c) for c in counts]
  64. labels_map = tf.convert_to_tensor(labels_map)
  65. # Evaluation ops
  66. # Lookup: centroid_id -> label
  67. cluster_label = tf.nn.embedding_lookup(labels_map, cluster_idx)
  68. # Compute accuracy
  69. correct_prediction = tf.equal(cluster_label, tf.cast(tf.argmax(Y, 1), tf.int32))
  70. accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  71. # Test Model
  72. test_x, test_y = mnist.test.images, mnist.test.labels
  73. print("Test Accuracy:", sess.run(accuracy_op, feed_dict={X: test_x, Y: test_y}))