如何在变量作用域中动态更改重复使用的变量?

2024-03-29 14:44:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望通过在模型函数中传递一个参数,在训练阶段将重用变量值从False动态更改为True

我试过了

def model_func(reuse=True):
    with tf.variable_scope('batch_norm',reuse = reuse):
      #...some batch normalization layer...

这是我的原始代码:

def model_architecture(self, X, keep_prob,reuse=True,is_training=True):
        with tf.variable_scope("model_architecture", reuse=reuse):
            # Reshape input picture
            X = tf.reshape(X, shape=[-1, self.height, self.width, 1])
            #               --{1st BLOCK}--
            # 1st layer
            shape = [3,3,1,4] # first layer
            with tf.variable_scope("convolution_layer1",reuse=reuse):
                conv_l1 = mf.conv_layer(X,shape,"conv_l1")
            with tf.variable_scope("batch_norm_layer1",reuse=reuse):
                conv_l1 = mf.batch_n(conv_l1,'batch_norm_l1')

值错误8重用必须为真、假或无

所以我需要调用model\u架构(…,reuse=False,…) 并更改所有变量的作用域重用变量的值。有办法吗

我需要这个,因为我将我的训练集划分为较小的训练集,因为输入数据较大,所以我在一个集中训练我的模型保存/恢复,并使用新输入从最后一点继续训练。有办法吗


Tags: 模型selflayertruenorml1modeltf