首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有什么方法可以不继承父variable_scope吗?

有什么方法可以不继承父variable_scope吗?
EN

Stack Overflow用户
提问于 2018-02-28 01:46:47
回答 1查看 249关注 0票数 1

假设我有以下代码:

代码语言:javascript
运行
复制
import tensorflow as tf
with tf.variable_scope('Embedding'):
    embd = tf.get_variable('embedding_matrix', [100, 10], dtype = tf.float32)

我想在新的作用域中重用名为embedding_matrix的张量:

代码语言:javascript
运行
复制
with tf.variable_scope('not_related'):
    with tf.variable_scope('Embedding', reuse = True) as scope:
        # I want the name of 'call_embd' be 'Embedding/embedding_matrix
        # but not 'not_related/Embedding/embedding_matrix'
        call_embd = tf.get_variable('embedding_matrix')

有没有办法让call_embdEmbedding/embedding_matrix这个名字?

EN

回答 1

Stack Overflow用户

发布于 2018-02-28 22:07:47

根据你的澄清评论,听起来你把variable_scopename_scope搞混了。

简而言之:

  • variable_scope用于避免代码的不同部分无意中重用相同的variables.
  • name_scope来对操作进行逻辑分组。

因此,在您的示例中,您将使用类似以下内容:

代码语言:javascript
运行
复制
with tf.name_scope('Encoding'):
  with tf.variable_scope('Embedding'):
    embd = tf.get_variable('embedding_matrix', [100, 10], dtype = tf.float32)
    # ... use this var
# ... later ...
with tf.name_scope('Decoding'):
  with tf.variable_scope('Embedding', reuse=True):
    embd = tf.get_variable('embedding_matrix')
    # ... reuse this var

关于name_scopevariable_scope之间的更多区别(诚然令人困惑),可以在here上找到。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49014885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档