假设我有以下代码:
import tensorflow as tf
with tf.variable_scope('Embedding'):
embd = tf.get_variable('embedding_matrix', [100, 10], dtype = tf.float32)我想在新的作用域中重用名为embedding_matrix的张量:
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_embd有Embedding/embedding_matrix这个名字?
发布于 2018-02-28 22:07:47
根据你的澄清评论,听起来你把variable_scope和name_scope搞混了。
简而言之:
variable_scope用于避免代码的不同部分无意中重用相同的variables.name_scope来对操作进行逻辑分组。因此,在您的示例中,您将使用类似以下内容:
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_scope和variable_scope之间的更多区别(诚然令人困惑),可以在here上找到。
https://stackoverflow.com/questions/49014885
复制相似问题