【问题标题】:Injecting pre-trained word2vec vectors into TensorFlow seq2seq将预训练的 word2vec 向量注入 TensorFlow seq2seq
【发布时间】:2016-04-05 07:23:36
【问题描述】:

我试图将预训练的 word2vec 向量注入现有的 tensorflow seq2seq 模型。

this answer 之后,我生成了以下代码。但它似乎并没有像应有的那样提高性能,尽管变量中的值已更新。

在我的理解中,错误可能是由于 EmbeddingWrapper 或 embedding_attention_decoder 独立于词汇顺序创建嵌入?

将预训练向量加载到 tensorflow 模型中的最佳方法是什么?

SOURCE_EMBEDDING_KEY = "embedding_attention_seq2seq/RNN/EmbeddingWrapper/embedding"
TARGET_EMBEDDING_KEY = "embedding_attention_seq2seq/embedding_attention_decoder/embedding"


def inject_pretrained_word2vec(session, word2vec_path, input_size, dict_dir, source_vocab_size, target_vocab_size):
  word2vec_model = word2vec.load(word2vec_path, encoding="latin-1")
  print("w2v model created!")
  session.run(tf.initialize_all_variables())

  assign_w2v_pretrained_vectors(session, word2vec_model, SOURCE_EMBEDDING_KEY, source_vocab_path, source_vocab_size)
  assign_w2v_pretrained_vectors(session, word2vec_model, TARGET_EMBEDDING_KEY, target_vocab_path, target_vocab_size)


def assign_w2v_pretrained_vectors(session, word2vec_model, embedding_key, vocab_path, vocab_size):
  vectors_variable = [v for v in tf.trainable_variables() if embedding_key in v.name]
  if len(vectors_variable) != 1:
      print("Word vector variable not found or too many. key: " + embedding_key)
      print("Existing embedding trainable variables:")
      print([v.name for v in tf.trainable_variables() if "embedding" in v.name])
      sys.exit(1)

  vectors_variable = vectors_variable[0]
  vectors = vectors_variable.eval()

  with gfile.GFile(vocab_path, mode="r") as vocab_file:
      counter = 0
      while counter < vocab_size:
          vocab_w = vocab_file.readline().replace("\n", "")
          # for each word in vocabulary check if w2v vector exist and inject.
          # otherwise dont change the value.
          if word2vec_model.__contains__(vocab_w):
              w2w_word_vector = word2vec_model.get_vector(vocab_w)
              vectors[counter] = w2w_word_vector
          counter += 1

  session.run([vectors_variable.initializer],
            {vectors_variable.initializer.inputs[1]: vectors})

【问题讨论】:

    标签: python tensorflow word2vec


    【解决方案1】:

    我不熟悉 seq2seq 示例,但通常您可以使用以下代码 sn-p 来注入嵌入:

    在哪里构建图表:

    with tf.device("/cpu:0"):
      embedding = tf.get_variable("embedding", [vocabulary_size, embedding_size])      
      inputs = tf.nn.embedding_lookup(embedding, input_data)
    

    当您执行时(在构建图表之后和说明训练之前),只需将保存的嵌入分配给嵌入变量:

    session.run(tf.assign(embedding, embeddings_that_you_want_to_use))
    

    这个想法是embedding_lookupinput_data 值替换为embedding 变量中的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 2016-06-11
      相关资源
      最近更新 更多