【发布时间】:2017-03-30 23:23:37
【问题描述】:
我知道这个问题之前已经发布过,但是看着答案我不知道该怎么做。我想在 tensorflow seq2seq 嵌入中使用预训练向量作为编码器输入(tensorflow 教程中的翻译示例)。 我知道在 core_rnn_cell_imp.py 文件中它使用以下代码启动嵌入:
embedding = vs.get_variable(
"embedding", [self._embedding_classes, self._embedding_size],
initializer=initializer,
dtype=data_type)
但是我怎样才能用数组 X 覆盖这个嵌入:
X = np.ndarray(shape=(20,10), dtype='f') # lets say I want to replace the embedding with this pretrained array
with tf.variable_scope("embedding_rnn_seq2seq"):
with tf.variable_scope("embedding_wrapper"):
sess.run(tf.assign(embedding, X))
创建模型后,我在训练之前运行了上面的代码,我收到以下错误:
global name 'embedding' is not defined
我应该如何解决这个问题?对不起,我是张量流的新手。
【问题讨论】:
-
embedding是否在您添加新代码的同一 Python 函数中定义? -
@mrry --不,不是。这就是我收到此错误的原因吗?
-
是的,这是一个 Python 错误,因为该变量名未在同一范围内定义。您需要找到某种方法将
embedding的值传递到您尝试使用它的位置。
标签: tensorflow