简体   繁体   English

Tensorflow-使用tf.contrib.layers.conv2d时,我可以设置权重和偏差的名称吗?

[英]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/weightsConv/biases Conv_1/weightsConv_1/weightsConv_1/biases Conv_2/weightsConv_2/weightsConv_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/weightsConv/biases属于一个名为C2的变量,因此我想将其命名为C2/weightsC2/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.nntf.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.conv2dtf.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/weightsmy_conv1/bias

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tf.contrib.slim.conv2d和tf.contrib.layers.conv2d有什么区别? - What's the differences between tf.contrib.slim.conv2d and tf.contrib.layers.conv2d? tf.layers.conv2d 和 tf.contrib.slim.conv2d 的区别 - Difference between tf.layers.conv2d and tf.contrib.slim.conv2d 使用tf.layers.conv2d时如何将跨步设置为零 - how to set stride to zero when using tf.layers.conv2d 在tf.layers.conv2d中,use_bias = True,这些偏差是捆绑在一起还是不捆绑在一起? - In tf.layers.conv2d, with use_bias=True, are the biases tied or untied? 如何在重新训练时初始化tensorflow.contrib.slim fully_connected图层的权重和偏差? - How can I initialize weights and biases of tensorflow.contrib.slim fully_connected layer while re-training? 如何从使用 tf.contrib.layers.fully_connected 创建的层访问权重? - How can I access weights from a layer created with tf.contrib.layers.fully_connected? Tensorflow tf.layers.conv3d输出形状 - Tensorflow tf.layers.conv3d output shape Tensorflow:为什么tf.nn.conv2d运行得比tf.layers.conv2d快? - Tensorflow: why tf.nn.conv2d runs faster than tf.layers.conv2d? 如何使用tf.keras.layers通过Tensorflow conv2d批处理图像序列 - How to Feed Batched Sequences of Images through Tensorflow conv2d using tf.keras.layers 使用权重初始化程序与tf.nn.conv2d - Using weights initializer with tf.nn.conv2d
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM