【发布时间】:2017-01-15 09:11:42
【问题描述】:
我正在查看 RNN 语言模型的代码。我对 1) 如何构造训练对 (x,y) 以及随后如何2) 计算损失感到困惑。代码借鉴自 Tensorflow RNN 教程(reader 模块)。
在阅读器模块中,定义了一个生成器ptb_iterator。它将数据作为一个序列接收,并根据批量大小和您希望“展开”RNN 的步骤数产生 x,y 对。最好先看一下整个定义,但让我感到困惑的部分是:
for i in range(epoch_size):
x = data[:, i*num_steps:(i+1)*num_steps]
y = data[:, i*num_steps+1:(i+1)*num_steps+1]
yield (x, y)
记录为:
*Yields:
Pairs of the batched data, each a matrix of shape [batch_size, num_steps].
The second element of the tuple is the same data time-shifted to the
right by one.*
所以如果理解正确,对于数据序列[1 2 3 4 5 6] 和num_steps = 2,那么对于随机梯度下降(即batch_size=1)将生成以下对:
- x=[1,2] , y=[2,3]
- x=[3,4] , y=[5,6]
1) 这是正确的方法吗?如果不这样做,那么对是:
- x=[1,2] , y=[2,3]
- x=[2,3] , y=[3,4] ... # 允许更多数据点
或
- x=[1,2] , y=[3]
- x=[2,3] , y=[4] ... # 确保所有预测都使用上下文长度 = num_steps
2) 最后,鉴于这些对是在reader 模块中生成的,当涉及到训练时,计算的损失会不会反映 RNN 在展开的范围内的性能?步骤而不是指定num_steps?
例如,模型将预测 x=3(来自 x=[3,4]),而不考虑 2 在它之前(即展开 RNN 一步而不是两步)。
【问题讨论】:
-
您的第一个示例中有错字。应该是
x1 = [1,2] y1 = [2, 3], x2 = [3, 4], y2 = [4, 5]。
标签: neural-network tensorflow recurrent-neural-network language-model