简体   繁体   中英

Is there a way to apply K-Means to the output of Conv/Dense layers in tensorflow?

I have trained a CNN on Fashion MNIST data with the following configuration: Conv-Pool-Dropout-Conv-Pool-Dropout-Flat-Dense-Dropout-Output I would like to change the configuration to: Conv-Clustering-Pool-Dropout-Conv-Clustering-Pool-Dropout-Flat-Dense-Clustering-Dropout-Output However, I want this new configuration only for testing and not training the model (I can use the weights from the trained model and set them for the model with Clustering configuration). Is there a way to add the Clustering layer using tensorflow? I would like to represent the output of the Conv and Dense layers using the cluster centroids to examine the effects on the accuracy of the model.

What you are asking is in fact two questions:

  1. How can I apply an operation only in some config and not others?
  2. How can I apply k-means?

And here are the answers:

  1. To apply an operation based on the value of some tensorflow variable do_clustering you can use tf.cond with:

     maybe_clustered_ouput = tf.cond(do_clustering, lambda: my_clustering_operation(input), lambda: input)
  2. To apply k-means clustering, you can use tf.compat.v1.estimator.experimental.KMeans (and replace that instead ofmy_clustering_operation` in the above snippet)

Good luck:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM