如何在TensorFlow中微调特定图层中的权重?

时间:2017-07-14 02:07:57

标签: tensorflow

我试图实施Progressive Neural Networks,在本文中,作者应用转移学习来利用以前学到的知识来训练当前的强化学习代理。 2个问题:

  1. 如何锁定某些图层,以便更新这些图层的权重和偏差?
  2. 我怎样才能在训练期间训练特定的图层?
  3. 这是我的代码:

    def __create_network(self):
        with tf.variable_scope('inputs'):
            self.inputs = tf.placeholder(shape=[-1, 80, 80, 4], dtype=tf.float32, name='input_data')
    
        with tf.variable_scope('networks'):
            with tf.variable_scope('conv_1'):
                self.conv_1 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.inputs, num_outputs=32,
                                          kernel_size=[8, 8], stride=4, padding='SAME')
    
            with tf.variable_scope('conv_2'):
                self.conv_2 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_1, num_outputs=64,
                                          kernel_size=[4, 4], stride=2, padding='SAME')
    
            with tf.variable_scope('conv_3'):
                self.conv_3 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_2, num_outputs=64,
                                          kernel_size=[3, 3], stride=1, padding='SAME')
    
            with tf.variable_scope('fc'):
                self.fc = slim.fully_connected(slim.flatten(self.conv_3), 512, activation_fn=tf.nn.elu)
    

    我想锁定conv_1conv_2conv_3,并且只在恢复检查点数据后训练fc

1 个答案:

答案 0 :(得分:1)

要锁定某些变量,它有点复杂,有几种方法可以做到。这个post涵盖了它,与您的问题非常相似。

最简单的方法是执行以下操作:

fc_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='fc')
train_op = opt.minimize(loss, var_list=fc_vars)