【问题标题】:Cannot set trainable=False to freeze some layers in TensorFlow无法设置 trainable=False 以冻结 TensorFlow 中的某些层
【发布时间】:2019-07-31 02:38:42
【问题描述】:

我尝试将 AlexNet 的某些层冻结为self.FROZEN_LAYER=['conv2', 'conv3']。 这是sn-p:

for op_name in weights_dict:

    # Check if layer should be trained from scratch
    if op_name not in self.SKIP_LAYER:

        with tf.variable_scope(op_name, reuse=True):

            # Assign weights/biases to their corresponding tf variable
            for data in weights_dict[op_name]:
                if len(data.shape) == 1:
                    var = tf.get_variable('biases',
                                          trainable=[True if op_name not in self.FROZEN_LAYER else False][
                                              0])  # todo: trainable
                    session.run(var.assign(data))

                # Weights
                else:
                    var = tf.get_variable('weights',
                                          trainable=[True if op_name not in self.FROZEN_LAYER else False][0])
                    session.run(var.assign(data))

但是当我在tf.get_variable()函数中调试时(op_name: 'conv2''conv3'在调试器中),trainable参数不能设置为False。有谁知道问题出在哪里?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    Is it possible to make a trainable variable not trainable? 中也出现了同样的问题。这个问题中提出的第一个答案对我的问题有效。

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 2022-07-25
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 2021-12-18
      • 2013-07-15
      相关资源
      最近更新 更多