我是 TensorFlow 新手,需要训练语言模型,但在阅读 document 时遇到了一些困难如下图所示。
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)
我不明白为什么需要这条线,
logits = tf.matmul(output, softmax_w) + softmax_b
因为我了解到,一旦计算出输出并且知道 target_words,我们就可以直接计算出损失。看来伪代码增加了一个额外的层。另外,前面没有提到的softmax_w和softmax_b是什么。我想我提出这样一个简单的问题可能错过了一些重要的事情。
请为我指出正确的方向,我们非常感谢任何建议。多谢。
最佳答案
所有代码所做的就是在计算 softmax 之前添加额外的线性变换。 softmax_w
应该是一个包含权重矩阵的 tf.Variable
。 softmax_b
应该是一个包含偏差向量的 tf.Variable
。
查看本教程中的 softmax 示例以了解更多详细信息: https://www.tensorflow.org/versions/r0.10/tutorials/mnist/beginners/index.html#softmax-regressions
关于tensorflow - 本文档中的softmax_w和softmax_b是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39039793/