- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 tensorflow LSTM model进行下一个单词预测。
如本 related question 中所述(没有可接受的答案)该示例包含用于提取下一个单词概率的伪代码:
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)
我对如何解释概率向量感到困惑。我在 ptb_word_lm.py 中修改了 PTBModel
的 __init__
函数。存储概率和对数:
class PTBModel(object):
"""The PTB model."""
def __init__(self, is_training, config):
# General definition of LSTM (unrolled)
# identical to tensorflow example ...
# omitted for brevity ...
# computing the logits (also from example code)
logits = tf.nn.xw_plus_b(output,
tf.get_variable("softmax_w", [size, vocab_size]),
tf.get_variable("softmax_b", [vocab_size]))
loss = seq2seq.sequence_loss_by_example([logits],
[tf.reshape(self._targets, [-1])],
[tf.ones([batch_size * num_steps])],
vocab_size)
self._cost = cost = tf.reduce_sum(loss) / batch_size
self._final_state = states[-1]
# my addition: storing the probabilities and logits
self.probabilities = tf.nn.softmax(logits)
self.logits = logits
# more model definition ...
然后在 run_epoch
函数中打印一些关于它们的信息:
def run_epoch(session, m, data, eval_op, verbose=True):
"""Runs the model on the given data."""
# first part of function unchanged from example
for step, (x, y) in enumerate(reader.ptb_iterator(data, m.batch_size,
m.num_steps)):
# evaluate proobability and logit tensors too:
cost, state, probs, logits, _ = session.run([m.cost, m.final_state, m.probabilities, m.logits, eval_op],
{m.input_data: x,
m.targets: y,
m.initial_state: state})
costs += cost
iters += m.num_steps
if verbose and step % (epoch_size // 10) == 10:
print("%.3f perplexity: %.3f speed: %.0f wps, n_iters: %s" %
(step * 1.0 / epoch_size, np.exp(costs / iters),
iters * m.batch_size / (time.time() - start_time), iters))
chosen_word = np.argmax(probs, 1)
print("Probabilities shape: %s, Logits shape: %s" %
(probs.shape, logits.shape) )
print(chosen_word)
print("Batch size: %s, Num steps: %s" % (m.batch_size, m.num_steps))
return np.exp(costs / iters)
这会产生如下输出:
0.000 perplexity: 741.577 speed: 230 wps, n_iters: 220
(20, 10000) (20, 10000)
[ 14 1 6 589 1 5 0 87 6 5 3 5 2 2 2 2 6 2 6 1]
Batch size: 1, Num steps: 20
我期望 probs
向量是一个概率数组,词汇表中的每个单词都有一个概率(例如形状为 (1, vocab_size)
),意思是我可以按照另一个问题中的建议使用 np.argmax(probs, 1)
获得预测词。
但是,向量的第一维实际上等于展开的 LSTM 中的步数(如果使用小配置设置,则为 20),我不确定该怎么做。要访问预测词,我是否只需要使用最后一个值(因为它是最后一步的输出)?还是我还缺少其他东西?
我试图通过查看 seq2seq.sequence_loss_by_example 的实现来理解预测是如何做出和评估的,它必须执行此评估,但这最终会调用 gen_nn_ops._sparse_softmax_cross_entropy_with_logits
,它似乎没有包含在 github 存储库中,所以我不确定还有什么地方可以看。
我对 tensorflow 和 LSTM 都很陌生,所以非常感谢您的帮助!
最佳答案
output
张量包含每个时间步的 LSTM 单元输出的串联(参见其定义 here )。因此,您可以通过采用 chosen_word[-1]
(或 chosen_word[sequence_length - 1]
如果序列已被填充以匹配展开的 LSTM ).
tf.nn.sparse_softmax_cross_entropy_with_logits()
op 以不同的名称记录在公共(public) API 中。由于技术原因,它调用了一个未出现在 GitHub 存储库中的生成的包装器函数。 op 的实现是在 C++ 中,here .
关于python - 使用 LSTM ptb 模型 tensorflow 示例预测下一个词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36286594/
我无法准确理解 LSTM 单元的范围——它如何映射到网络层。来自格雷夫斯 (2014): 在我看来,在单层网络中,layer = lstm 单元。这实际上如何在多层 rnn 中工作? 三层RNN LS
这是代码 model = Sequential() model.add(LSTM(256, input_shape=(None, 1), return_sequences=True)) model.a
为什么我们需要在pytorch中初始化LSTM中的隐藏状态h0。由于 h0 无论如何都会被计算并被覆盖?是不是很像 整合一个一 = 0 一个= 4 即使我们不做a=0,也应该没问题.. 最佳答案 重点
我正在尝试使用 LSTM 在 Deeplearning4j 中进行一些简单的时间序列预测,但我很难让它工作。 我有一个简单的文本文件,其中包含如下所示的数字列表,并希望网络学习预测下一个数字。 有没有
在大量阅读和绘制图表之后,我想我已经提出了一个模型,我可以将其用作更多测试我需要调整哪些参数和功能的基础。但是,我对如何实现以下测试用例感到困惑(所有数字都比最终模型小几个数量级,但我想从小处着手):
我正在尝试实现“Livelinet:用于预测教育视频中的活力的多模式深度循环神经网络”中的结构。 为了简单说明,我将 10 秒音频剪辑分成 10 个 1 秒音频剪辑,并从该 1 秒音频剪辑中获取频谱图
我正在 Tensorflow 中制作 LSTM 神经网络。 输入张量大小为 92。 import tensorflow as tf from tensorflow.contrib import rnn
我正在尝试 keras IMDB 数据的示例,数据形状是这样的: x_train shape: (25000, 80) 我只是把keras例子的原始代码改成了这样的代码: model = Sequen
我需要了解如何使用 torch.nn 的不同组件正确准备批量训练的输入。模块。具体来说,我希望为 seq2seq 模型创建一个编码器-解码器网络。 假设我有一个包含这三层的模块,按顺序: nn.Emb
我很难概念化 Keras 中有状态 LSTM 和无状态 LSTM 之间的区别。我的理解是,在每个批处理结束时,在无状态情况下“网络状态被重置”,而对于有状态情况,网络状态会为每个批处理保留,然后必须在
nn.Embedding() 是学习 LSTM 所必需的吗? 我在 PyTorch 中使用 LSTM 来预测 NER - 此处是类似任务的示例 - https://pytorch.org/tutori
我正在尝试找出适合我想要拟合的模型的正确语法。这是一个时间序列预测问题,我想在将时间序列输入 LSTM 之前使用一些密集层来改进时间序列的表示。 这是我正在使用的虚拟系列: import pandas
我在理解堆叠式 LSTM 网络中各层的输入-输出流时遇到了一些困难。假设我已经创建了一个如下所示的堆叠式 LSTM 网络: # parameters time_steps = 10 features
LSTM 类中的默认非线性激活函数是 tanh。我希望在我的项目中使用 ReLU。浏览文档和其他资源,我无法找到一种简单的方法来做到这一点。我能找到的唯一方法是定义我自己的自定义 LSTMCell,但
在 PyTorch 中,有一个 LSTM 模块,除了输入序列、隐藏状态和单元状态之外,它还接受 num_layers 参数,该参数指定我们的 LSTM 有多少层。 然而,还有另一个模块 LSTMCel
没什么好说的作为介绍:我想在 TensorFlow 中将 LSTM 堆叠在另一个 LSTM 上,但一直被错误阻止,我不太明白,更不用说单独解决了。 代码如下: def RNN(_X, _istate,
有人可以解释一下吗?我知道双向 LSTM 具有前向和反向传递,但是与单向 LSTM 相比,它有什么优势? 它们各自更适合什么? 最佳答案 LSTM 的核心是使用隐藏状态保留已经通过它的输入信息。 单向
我想构建一个带有特殊词嵌入的 LSTM,但我对它的工作原理有一些疑问。 您可能知道,一些 LSTM 对字符进行操作,因此它是字符输入,字符输出。我想做同样的事情,通过对单词的抽象来学习使用嵌套的 LS
我编写了一个LSTM回归模型。它是最后一个LSTM层的BATCH_SIZE=1和RETURN_Sequence=True的模型。我还设置了VERIFICATION_DATA和耐心进行培训。但似乎存在一
给定一个训练有素的 LSTM 模型,我想对单个时间步执行推理,即以下示例中的 seq_length = 1。在每个时间步之后,需要为下一个“批处理”记住内部 LSTM(内存和隐藏)状态。在推理的最开始
我是一名优秀的程序员,十分优秀!