[英]Tensorflow - When using tf.contrib.layers.conv2d, can I set the name of the weights and biases?
Tensorflow version: 1.10.1 Tensorflow版本:1.10.1
I want to transfer my learned weights and biases of the convolution layers in my pretrained network to a new network. 我想将我的预训练网络中所学的权重和卷积层的偏差转移到新网络中。
However, because I used conv2d
api, the weights and biases in the checkpoint file are automatically named as Conv/weights
, Conv/biases
, Conv_1/weights
, Conv_1/biases
, Conv_2/weights
, Conv_2/biases
etc. 但是,因为我使用了
conv2d
api,所以检查点文件中的权重和偏差会自动命名为Conv/weights
, Conv/biases
Conv_1/weights
, Conv_1/weights
, Conv_1/biases
Conv_2/weights
, Conv_2/weights
, Conv_2/biases
等。
However, these names are not consistent with my variable name in my codes. 但是,这些名称与代码中的变量名称不一致。 For example,
Conv/weights
, Conv/biases
belongs to a variable named C2
in my codes, so I want to name them as C2/weights
, C2/biases
. 例如,在我的代码中,
Conv/weights
, Conv/biases
属于一个名为C2
的变量,因此我想将其命名为C2/weights
, C2/biases
。
I know for varibales defined by get_variable
, I can set their names to whatever strings I want by my_varibale = tf.get_variable("whatever_name_I_want",shape,dtype,initializer)
, where my_variable
is named to "whatever_name_I_want"
. 我知道对于由
get_variable
定义的变量,我可以通过my_varibale = tf.get_variable("whatever_name_I_want",shape,dtype,initializer)
将它们的名称设置为所需的任何字符串,其中my_variable
命名为"whatever_name_I_want"
。
However, when it comes to my_layer = tf.contrib.layers.conv2d(params_blabla)
(or the equivalent api in tf.nn
or tf.layers
), I don't know how I can name my_layer
. 但是,当涉及到
my_layer = tf.contrib.layers.conv2d(params_blabla)
(或tf.nn
或tf.layers
的等效api)时,我不知道如何命名my_layer
。
So, is it possible to do this? 那么,有可能这样做吗? And if possible, how?
如果可能的话,怎么办?
For tf.layers.conv2d and tf.nn.conv2d you can pass an additional parameter called name
. 对于tf.layers.conv2d和tf.nn.conv2d,您可以传递一个名为
name
的附加参数。
Example: 例:
my_conv1 = tf.layers.conv2D(...., name='my_conv1')
my_conv2 = tf.layers.conv2D(...., name='my_conv2 ')
For tf.contrib.layers.conv2d the parameter scope
is used to namespace your layers. 对于tf.contrib.layers.conv2d ,参数
scope
用于为图层命名空间。
Example: 例:
with tf.name_scope('my_conv1') as scope:
my_conv1 = tf.contrib.layers.conv2d(..., scope=scope)
Result: 结果:
In all cases your weights and biases are named like for example my_conv1/weights
and my_conv1/bias
. 在所有情况下,您的权重和偏差都被命名为例如
my_conv1/weights
和my_conv1/bias
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.