How to access kernel variables in tf.layers.conv2d

2019-04-01 01:31发布

问题:

I want to visualize weights in convolutional layers to watch how they change.

But I can not find a way to access weights in convolutional layers in tf.layers.conv2d

Thank you

回答1:

You could access that variable by name:

weights = sess.run('<name_of_your_layer>/weights:0', feed_dict=...)

If you're unsure about the name of your variable, see what it could be by printing tf.trainable_variables()



回答2:

With inspiration from this: How to get CNN kernel values in Tensorflow

Make sure to give it a name:

conv_layer = tf.layers.conv2d(..., name='YOUR_NAME', ...)

Access the variables like this:

gr = tf.get_default_graph()
conv1_kernel_val = gr.get_tensor_by_name('YOUR_NAME/kernel:0').eval()
conv1_bias_val = gr.get_tensor_by_name('YOUR_NAME/bias:0').eval()