如何在 Tensorflow 中获取 CNN 内核值 [英] How to get CNN kernel values in Tensorflow

查看:23
本文介绍了如何在 Tensorflow 中获取 CNN 内核值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码来创建 CNN 层.

I am using the code below to create CNN layers.

conv1 = tf.layers.conv2d(inputs = input, filters = 20, kernel_size = [3,3],
    padding = "same", activation = tf.nn.relu)

并且我想在训练后获得所有内核的值.它不起作用,我只是做

and I want to get the values of all kernels after training. It does not work it I simply do

kernels = conv1.kernel

那么我应该如何检索这些内核的值呢?我也不确定 conv2d 有哪些变量和方法,因为 tensorflow 并没有在 conv2d 类中真正告诉它.

So how should I retrieve the value of these kernels? I am also not sure what variables and method does conv2d has since tensorflow don't really tell it in conv2d class.

推荐答案

您可以在 tf.global_variables() 返回的列表中查找所有变量,并轻松查找您需要的变量.

You can find all the variables in list returned by tf.global_variables() and easily lookup for variable you need.

如果您希望通过名称获取这些变量,请将图层声明为:

If you wish to get these variables by name, declare a layer as:

conv_layer_1 = tf.layers.conv2d(activation=tf.nn.relu, 
                                filters=10, 
                                inputs=input_placeholder, 
                                kernel_size=(3, 3), 
                                name="conv1",         # NOTE THE NAME 
                                padding="same", 
                                strides=(1, 1))

恢复图形为:

gr = tf.get_default_graph()

将内核值恢复为:

conv1_kernel_val = gr.get_tensor_by_name('conv1/kernel:0').eval()

将偏差值恢复为:

conv1_bias_val = gr.get_tensor_by_name('conv1/bias:0').eval()

这篇关于如何在 Tensorflow 中获取 CNN 内核值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆