title: How does Kmeans clustering work in tensorflow?
body: <p>I saw that there is an implementation of Kmeans clustering available within tensorflow contrib library. However, I was not able to do the simple operation of estimating cluster centers for 2D points. </p>
  
  <p>Code:</p>
  
  <pre><code>## Generate synthetic data
  N,D = 1000, 2 # number of points and dimenstinality
  
  means = np.array([[0.5, 0.0],
                    [0, 0],
                    [-0.5, -0.5],
                    [-0.8, 0.3]])
  covs = np.array([np.diag([0.01, 0.01]),
                   np.diag([0.01, 0.01]),
                   np.diag([0.01, 0.01]),
                   np.diag([0.01, 0.01])])
  n_clusters = means.shape[0]
  
  points = []
  for i in range(n_clusters):
      x = np.random.multivariate_normal(means[i], covs[i], N )
      points.append(x)
  points = np.concatenate(points)
  
  ## construct model
  kmeans = tf.contrib.learn.KMeansClustering(num_clusters = n_clusters)
  kmeans.fit(points.astype(np.float32))
  </code></pre>
  
  <p>I get the following error : </p>
  
  <pre><code>InvalidArgumentError (see above for traceback): Shape [-1,2] has negative dimensions
       [[Node: input = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
  </code></pre>
  
  <p>I guess I'm doing something wrong but couldn't figure out what from the documentation. </p>
  
  <p><strong>Edit</strong>:  </p>
  
  <p>I solved it using <code>input_fn</code> but it is really slow (I had to reduce the number of points in each cluster to 10 to see results). Why is that and how can I make it faster? </p>
  
  <pre><code> def input_fn():
      return tf.constant(points, dtype=tf.float32), None
  
  ## construct model
  kmeans = tf.contrib.learn.KMeansClustering(num_clusters = n_clusters, relative_tolerance=0.0001)
  kmeans.fit(input_fn=input_fn)
  centers = kmeans.clusters()
  print(centers)
  </code></pre>
  
  <p><strong>Solved:</strong> </p>
  
  <p>It seems that a relative tolerance should be set. so I changed only one line and it works fine. 
  <code>kmeans = tf.contrib.learn.KMeansClustering(num_clusters = n_clusters, relative_tolerance=0.0001)</code></p>