我一直在尝试理解示例代码https://www.tensorflow.org/tutorials/recurrent https://www.tensorflow.org/tutorials/recurrent你可以在以下位置找到https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/ptb_word_lm.py https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/ptb_word_lm.py
(使用张量流1.3.0。)
我总结了(我认为的)我的问题的关键部分,如下:
size = 200
vocab_size = 10000
layers = 2
# input_.input_data is a 2D tensor [batch_size, num_steps] of
# word ids, from 1 to 10000
cell = tf.contrib.rnn.MultiRNNCell(
[tf.contrib.rnn.BasicLSTMCell(size) for _ in range(2)]
)
embedding = tf.get_variable(
"embedding", [vocab_size, size], dtype=tf.float32)
inputs = tf.nn.embedding_lookup(embedding, input_.input_data)
inputs = tf.unstack(inputs, num=num_steps, axis=1)
outputs, state = tf.contrib.rnn.static_rnn(
cell, inputs, initial_state=self._initial_state)
output = tf.reshape(tf.stack(axis=1, values=outputs), [-1, size])
softmax_w = tf.get_variable(
"softmax_w", [size, vocab_size], dtype=data_type())
softmax_b = tf.get_variable("softmax_b", [vocab_size], dtype=data_type())
logits = tf.matmul(output, softmax_w) + softmax_b
# Then calculate loss, do gradient descent, etc.
我最大的问题是给定句子的前几个单词,如何使用生成的模型实际生成下一个单词建议?具体来说,我想象流程是这样的,但我无法理解注释行的代码是什么:
prefix = ["What", "is", "your"]
state = #Zeroes
# Call static_rnn(cell) once for each word in prefix to initialize state
# Use final output to set a string, next_word
print(next_word)
我的子问题是:
- 为什么使用随机(未初始化、未经训练的)词嵌入?
- 为什么使用softmax?
- 隐藏层是否必须与输入的维度匹配(即 word2vec 嵌入的维度)
- 我如何/可以引入预先训练的 word2vec 模型,而不是未初始化的模型?
(我将它们作为一个问题来问,因为我怀疑它们都是相互关联的,并且与我的理解中的某些差距有关。)
我期望在这里看到的是加载现有的 word2vec 词嵌入集(例如使用 gensim 的KeyedVectors.load_word2vec_format()
),在加载每个句子时将输入语料库中的每个单词转换为该表示,然后 LSTM 会吐出相同维度的向量,我们将尝试找到最相似的单词(例如使用 gensim 的similar_by_vector(y, topn=1)
).
使用 softmax 是否可以让我们免于相对较慢的速度similar_by_vector(y, topn=1)
call?
顺便说一句,对于我的问题中预先存在的 word2vec 部分使用预训练的 word2vec 和 LSTM 进行单词生成 https://stackoverflow.com/q/42064690/841830很相似。然而,目前那里的答案并不是我正在寻找的。我希望得到一个简单的英语解释,为我打开灯,并填补我理解中的任何空白。在lstm语言模型中使用预训练的word2vec? https://stackoverflow.com/questions/44614097/use-pre-trained-word2vec-in-lstm-language-model是另一个类似的问题。
UPDATE: 使用语言模型张量流示例预测下一个单词 https://stackoverflow.com/q/33773661/841830 and 使用 LSTM ptb 模型张量流示例预测下一个单词 https://stackoverflow.com/q/36286594/841830是类似的问题。然而,两者都没有显示代码实际获取句子的前几个单词,并打印出对下一个单词的预测。我尝试粘贴第二个问题的代码,以及https://stackoverflow.com/a/39282697/841830 https://stackoverflow.com/a/39282697/841830(附带一个 github 分支),但无法在没有错误的情况下运行。我认为它们可能适用于 TensorFlow 的早期版本?
另一个更新:还有一个问题询问基本上相同的事情:从 Tensorflow 示例预测 LSTM 模型的下一个单词 https://stackoverflow.com/q/42333101/841830它链接到使用语言模型张量流示例预测下一个单词 https://stackoverflow.com/q/33773661/841830(再说一次,那里的答案并不完全是我想要的)。
如果还不清楚,我正在尝试编写一个名为的高级函数getNextWord(model, sentencePrefix)
, where model
是我从磁盘加载的先前构建的 LSTM,并且sentencePrefix
是一个字符串,例如“Open the”,它可能返回“pod”。然后我可能会用“Open the pod”来调用它,它将返回“bay”,依此类推。
一个示例(使用字符 RNN,并使用 mxnet)是sample()
函数显示在接近末尾处https://github.com/zackchase/mxnet-the-straight-dope/blob/master/chapter05_recurrent-neural-networks/simple-rnn.ipynb https://github.com/zackchase/mxnet-the-straight-dope/blob/master/chapter05_recurrent-neural-networks/simple-rnn.ipynb您可以致电sample()
在训练期间,但您也可以在训练后调用它,并使用您想要的任何句子。