How to access kernel variables in tf.layers.conv2d

2019-04-01 00:55发布

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

2条回答
再贱就再见
2楼-- · 2019-04-01 01:33

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()
查看更多
贼婆χ
3楼-- · 2019-04-01 01:49

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

查看更多
登录 后发表回答