LKM
LKM

Reputation: 2450

How to access kernel variables in tf.layers.conv2d?

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

Upvotes: 4

Views: 2744

Answers (2)

elgehelge
elgehelge

Reputation: 2150

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

Upvotes: 2

Florentin Hennecker
Florentin Hennecker

Reputation: 2174

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

Upvotes: 3

Related Questions