Seq2seq嵌入预训练的word2vec

时间:2017-03-29 23:50:52

标签: tensorflow

我知道这个问题已经发布过,但是看看答案我无法弄清楚如何做到这一点。我想在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

我该如何解决这个问题?对不起,我是tensorflow的新手。

1 个答案:

答案 0 :(得分:0)

看看 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)
        """
……

我认为这很有用。

相关问题