2

I'm currently using the KMeans Class from tensorflow.contrib.factorization module. My input is (assuming all variables are defined):

kmeans = KMeans(inputs=X, num_clusters=k, distance_metric='cosine', use_mini_batch=True)

I'm following the documentation at https://www.tensorflow.org/api_docs/python/tf/contrib/factorization/KMeans to unpack the values like:

(all_scores, cluster_idx, scores, cluster_centers_initialized, init_op, train_op) = kmeans.training_graph()

I get the error:

----> (all_scores, cluster_idx, scores, cluster_centers_initialized, init_op, train_op) = kmeans.training_graph()    
ValueError: too many values to unpack

I'm strongly guessing that the documentation in the link stated above isn't updated because the output of kmeans.training_graph() is :

((<tf.Tensor 'sub_14:0' shape=(?, ?) dtype=float32>,),
 (<tf.Tensor 'Squeeze_7:0' shape=<unknown> dtype=int64>,),
 (<tf.Tensor 'Squeeze_6:0' shape=<unknown> dtype=float32>,),
 <tf.Variable 'initialized_3:0' shape=() dtype=bool_ref>,
 <tf.Variable 'clusters_3:0' shape=<unknown> dtype=float32_ref>,
 tf.Tensor 'cond_3/Merge:0' shape=() dtype=bool>,
 <tf.Operation 'group_deps_3' type=NoOp>)

Please let me know what is the extra returned valued that I'm not aware of by reading the documentation.

2
  • You need to find the source and look at it.
    – wwii
    Feb 15, 2018 at 19:25
  • I'm referring to the source code and the return type is: return (all_scores, cluster_idx, scores, cluster_centers_initialized, init_op, training_op) So, I'm guessing something's wrong somewhere else. By the way, I'm using juypter notebook (if that is of any concern)
    – Jerry Ajay
    Feb 15, 2018 at 19:25

2 Answers 2

2

Update: from the history of the file clustering_ops.py (master branch), it seems that the additional value (cluster_centers_vars) was removed in the following commit from 6.Oct, i.e. shortly after its introduction.

That means that your initial code should be working perfectly with more recent versions of TF, i.e.

(all_scores, cluster_idx, scores, cluster_centers_initialized, init_op, train_op) = kmeans.training_graph()

should be now again ok.

However, the consequence is that you cannot get the cluster centers through the kmeans.training_graph() function.

If you want to get the cluster centers, there are two solutions.

The first solution is simple, namely using the KMeansClustering Estimator, which is defined in the file kmeans.py. More specifically, you can use the method KMeansClustering.cluster_centers().

The second solution is rather a workaround. If you do not use the KMeansClustering Estimator, but only the KMeans graph constructor defined in the file clustering_ops.py, then you can still get the cluster centers by reading the global TF variable ´clusters:0´:

tf_vble_cluster_centers = tf.global_variables('clusters:0')[0] # get the global TF variable 'clusters:0'
cluster_centers = sess.run(tf_vble_cluster_centers) # evaluate its contents
print(cluster_centers.shape) # nr. rows = nr. of clusters, nr. columns = nr. dimensions
print(cluster_centers[0]) # print cluster centers for the first cluster
1

From the history in the repository

KMeans.training_graph() now returns an additional value, currently unused.

If you click on the link it will take you to the source and show you the extra return item.

return (all_scores, cluster_idx, scores, cluster_centers_initialized,
        init_op, training_op)
        cluster_centers_var, init_op, training_op)

cluster_centers_var is the new item.

1
  • Yes, that is it. Thanks.
    – Jerry Ajay
    Feb 15, 2018 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.