How to visualize TensorFlow Estimator weights?

2020-05-24 19:44发布

问题:

How can I select a layer from a tf.estimator.Estimator and access the weights vector for each unit in that layer? Specifically, I'm trying to visualize a Dense layer's weights.

Looking at https://github.com/tensorflow/tensorflow/blob/r1.3/tensorflow/python/layers/core.py it seems that the weights are called kernels, but I'm not able to access those when using the Estimator abstraction.

Ps: for an example of an implementation of Estimator, let's reference https://www.tensorflow.org/get_started/estimator

回答1:

Estimator has a method called get_variable_value. So, once you have produced a checkpoint (or loaded the variable values from one) and if you know the name of the dense layer, you could do something like this using matplotlib:

import matplotlib.pyplot as plt

weights = estimator.get_variable_value('dense/kernel')
plt.imshow(weights, cmap='gray')
plt.show()


回答2:

I just used the pre-compiled Estimator for testing and this worked properly for me.

import matplotlib.pyplot as plt

names = classifier.get_variable_names()        
print("name:", names)
for i in names:
    print(classifier.get_variable_value(i)