Search code examples
tensorflowk-meanstflearn

Using KMeans tflearn estimator as part of a graph in tensorflow


I am trying to use tensorflow.contrib.learn.KMeansClustering as part of a graph in Tensorflow. I would like to use it as a component of a graph, giving me predictions and centers. The relevant part of the code is the following:

with tf.variable_scope('kmeans'):
    kmeans = KMeansClustering(num_clusters=num_clusters,
                              relative_tolerance=0.0001)
    kmeans.fit(input_fn= (lambda : [X, None]))
    clusters = kmeans.clusters()

init_vars = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init_vars, feed_dict={X: full_data_x})
clusters_np = sess.run(clusters, feed_dict={X: full_data_x})

However, I get the following error:

ValueError: Tensor("kmeans/strided_slice:0", shape=(), dtype=int32) must be from the same graph as Tensor("sub:0", shape=(), dtype=int32).

I believe this is because KMeansClustering is a TFLearn estimator; which would be more akin to a whole graph than a single module. Is that correct? Can I transform it to a module of the default graph? If not, is there a function to do KMeans within another graph?

Thanks!


Solution

  • The KMeansClustering Estimator uses ops from tf.contrib.factorization. The factorization MNIST example uses KMeans without an Estimator.