语言模型的TensorFlow教程可以计算句子的概率:
probabilities = tf.nn.softmax(logits)
在下面的评论中,它还指定了一种预测下一个单词而不是概率的方法,但没有说明如何实现。那么如何使用此示例输出单词而不是概率?
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)
tf.argmax(probabilities,1)
会在训练后给出答案。给出最可能的单词是模型训练的目标,因此它将是模型的输出。您可能需要微调我刚刚给您的函数调用所给出的索引,以从字典中获取单词。 - Guillaume Chevaliercorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
,就能预测出最有可能的值?y
将是你想要进行预测的句子吗?抱歉,我仍然在从一个字符串转换成一个有效的单词列表并给出一些概率的过程中遇到困难。 - jlarks32