【问题标题】:Extract word/sentence probabilities from lm_1b trained model从 lm_1b 训练模型中提取单词/句子概率
【发布时间】:2017-11-17 23:21:41
【问题描述】:

我已经成功下载了使用 CNN-LSTM (https://github.com/tensorflow/models/tree/master/research/lm_1b) 训练的 1B 单词语言模型,我希望能够输入句子或部分句子以获得句子中每个后续单词的概率。

例如,如果我有一个类似“会说的动物”这样的句子,我想知道下一个词是“woof”与“meow”的概率。

我了解运行以下命令会产生 LSTM 嵌入:

bazel-bin/lm_1b/lm_1b_eval --mode dump_lstm_emb \
                           --pbtxt data/graph-2016-09-10.pbtxt \
                           --vocab_file data/vocab-2016-09-10.txt \
                           --ckpt 'data/ckpt-*' \
                           --sentence "An animal that says woof" \                             
                           --save_dir output

这将生成文件lstm_emb_step_*.npy,其中每个文件是句子中每个单词的 LSTM 嵌入。如何将这些转换为经过训练的模型的概率,以便能够比较 P(woof|An animal that says)P(meow|An animal that says)

提前致谢。

【问题讨论】:

  • 你解决了吗?

标签: python tensorflow nlp lstm language-model


【解决方案1】:

我想做同样的事情,这就是我想出的,改编自他们的一些演示代码。我不完全确定这是正确的,但它似乎产生了合理的值。

def get_probability_of_next_word(sess, t, vocab, prefix_words, query):
  """
  Return the probability of the given word based on the sequence of prefix 
  words. 

  :param sess: Tensorflow session object
  :param t: Tensorflow ??? object
  :param vocab: Vocabulary model, maps id <-> string, stores max word chard id length
  :param list prefix_words: List of words that appear before this one. 
  :param str query: The query word
  """
  targets = np.zeros([BATCH_SIZE, NUM_TIMESTEPS], np.int32)
  weights = np.ones([BATCH_SIZE, NUM_TIMESTEPS], np.float32)

  if not prefix_words or prefix_words[0] != "<S>":
    prefix_words.insert(0, "<S>")

  prefix = [vocab.word_to_id(w) for w in prefix_words]
  prefix_char_ids = [vocab.word_to_char_ids(w) for w in prefix_words]

  inputs = np.zeros([BATCH_SIZE, NUM_TIMESTEPS], np.int32)
  char_ids_inputs = np.zeros(
    [BATCH_SIZE, NUM_TIMESTEPS, vocab.max_word_length], np.int32)
  inputs[0, 0] = prefix[0]
  char_ids_inputs[0, 0, :] = prefix_char_ids[0]
  softmax = sess.run(t['softmax_out'],
                     feed_dict={t['char_inputs_in']: char_ids_inputs,
                                t['inputs_in']: inputs,
                                t['targets_in']: targets,
                                t['target_weights_in']: weights})

  return softmax[0, vocab.word_to_id(query)]

示例用法

vocab = CharsVocabulary(vocab_path, MAX_WORD_LEN)
sess, t = LoadModel(model_path, ckptdir + "/ckpt-*")
result = get_probability_of_next_word(sess, t, vocab, ["Hello", "my", "friend"], "for")

给出8.811023e-05 的结果。请注意,CharsVocabularyLoadModel 与 repo 中的内容略有不同。

还要注意这个函数很慢。也许有人知道如何改进它。

【讨论】:

    猜你喜欢
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多