【问题标题】:Seq2seq embedding with pretrained word2vec带有预训练 word2vec 的 Seq2seq 嵌入
【发布时间】: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


【解决方案1】:

看看 chatbot.py

 def loadEmbedding(self, sess):
        """ Initialize embeddings with pre-trained word2vec vectors
        Will modify the embedding weights of the current loaded model
        Uses the GoogleNews pre-trained values (path hardcoded)
        """
……

我认为这很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    相关资源
    最近更新 更多