4

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

LKM
  • 2,410
  • 9
  • 28
  • 53

2 Answers2

3

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()

Florentin Hennecker
  • 1,974
  • 23
  • 37
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()
elgehelge
  • 2,014
  • 1
  • 19
  • 24