使用语言模型tensorflow示例预测下一个单词

sta*_*kit 12 python neural-network tensorflow

语言模型的tensorflow教程允许计算句子的概率:

probabilities = tf.nn.softmax(logits)
Run Code Online (Sandbox Code Playgroud)

在下面的评论中,它还指定了一种预测下一个词而不是概率的方法,但没有说明如何做到这一点.那么如何使用这个例子输出一个单词而不是概率呢?

lstm = rnn_cell.BasicLSTMCell(lstm_size)
# Initial state of the LSTM memory.
state = tf.zeros([batch_size, lstm.state_size])

loss = 0.0
for current_batch_of_words in words_in_dataset:
    # The value of state is updated after processing each batch of words.
    output, state = lstm(current_batch_of_words, state)

    # The LSTM output can be used to make next word predictions
    logits = tf.matmul(output, softmax_w) + softmax_b
    probabilities = tf.nn.softmax(logits)
    loss += loss_function(probabilities, target_words)
Run Code Online (Sandbox Code Playgroud)

Cri*_*n F -1

该函数返回概率而不是单词本身实际上是一个优点。由于它使用单词列表以及相关的概率,您可以进行进一步的处理,并提高结果的准确性。

回答你的问题:你可以获取单词列表,迭代它,并使程序显示概率最高的单词。