【问题标题】:How to fine-tune weights in specific layers in TensorFlow?如何微调 TensorFlow 中特定层的权重?
【发布时间】:2017-07-14 14:51:39
【问题描述】:

我正在尝试实现Progressive Neural Networks,在本文中,作者应用迁移学习来利用先前学习的知识来训练当前的强化学习代理。 2 个问题:

  1. 如何锁定某些层,使这些层的权重和偏差无法更新?
  2. 如何才能在训练期间只训练特定层?

这是我的代码:

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

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 2021-10-21
      • 1970-01-01
      • 2021-11-28
      相关资源
      最近更新 更多