如何强制 tf.variable_scope 重用 name_scope? [英] How do I force tf.variable_scope to reuse name_scope?

查看:31
本文介绍了如何强制 tf.variable_scope 重用 name_scope?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

with tf.variable_scope('aa') as sa:
    with tf.variable_scope('bb'):
        x =  tf.get_variable(
            'biases', (2,),
            initializer=tf.constant_initializer()
        )
        y1 = tf.identity(x, name='bb')
with tf.variable_scope(sa):
    with tf.variable_scope('cc'):
        x =  tf.get_variable(
            'biases', (2,),
            initializer=tf.constant_initializer()
        )
        y2 = tf.identity(x, name='cc') 

我两次输入tf.variable_scope('aa'),生成了2个张量y1,y2.

I entered the tf.variable_scope('aa') twice, and generated 2 tensors y1, y2.

然而,y2.name == 'aa_1/cc/cc:0'.(y1.name == 'aa/bb/bb:0')

是否可以用 y2.name == 'aa/cc/cc:0' 代替?

推荐答案

有点晚了,但是尝试在名称范围的末尾添加 / 以明确表明您要重用范围.否则,正如您所注意到的,它会添加一个 _1.我在使用 name_scope 时遇到了类似的问题,但我认为该解决方案也适用于 variable_scope.

It's a little late, but try to add a / at the end of the namescope to explicitly indicate you want to reuse the scope. Otherwise, as you noticed, it add a _1. I had a similar problem with name_scope but I think the solution works with variable_scope too.

with tf.variable_scope('aa/'):
    ... # Some initialisation
with tf.variable_scope('aa/'):
    ... # Reuse the name scope

这篇关于如何强制 tf.variable_scope 重用 name_scope?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆