Q:

How does Kmeans clustering work in tensorflow?

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. 
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))

I get the following error : 
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"]()]]

I guess I'm doing something wrong but couldn't figure out what from the documentation. 
Edit:  
I solved it using input_fn 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? 
 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)

Solved: 
It seems that a relative tolerance should be set. so I changed only one line and it works fine. 
kmeans = tf.contrib.learn.KMeansClustering(num_clusters = n_clusters, relative_tolerance=0.0001)

A:

Your original code returns the following error with Tensorflow 1.2:
    WARNING:tensorflow:From <stdin>:1: calling BaseEstimator.fit (from         
    tensorflow.contrib.learn.python.learn.estimators.estimator) with x 
    is deprecated and will be removed after 2016-12-01.
    Instructions for updating:
    Estimator is decoupled from Scikit Learn interface by moving into
    separate class SKCompat. Arguments x, y and batch_size are only
    available in the SKCompat class, Estimator will only accept input_fn.

Based on your edit, it seems you figured out that input_fn is the only acceptable input. If you really want to use TF, I would upgrade to r1.2 and wrap the Estimator in the SKCompat class as the error message suggests. Otherwise, I would just use the SKLearn package. You can also implement your own clustering algorithm in TF by hand, as shown in this blog.

