过了一段时间,我已经设法正确地理解了维度在哪里。Keras需要一个3d数组.shape(totalRows,sequences,totalColumns)。对我来说sequences这个最让我困惑。在
这是因为当重塑df df.reshape(len(df), 1, len(df.columns))意思是keras会学习1行矩阵时,它给了我糟糕的结果,因为我不知道对我来说,缩放数据最好的方式是,但也可能是(0,1)。在
让我明白的是首先使用多于1行的序列(或天数,因为我的数据集是时间序列)。这意味着不是输入1行特征X得到1个y值,我使用了类似于5行特征X产生1个y值的东西。如:# after scaling the df, resulted in "scaled_dataset"
sequences = 5
result = []
# for loop will walk for each of the 1500 rows
for i in range(0,len(scaled_dataset)):
# every group must have the same length, so if current loop position i + number
# of sequences is higher than df length, breaks
if i+sequences <= len(scaled_dataset):
# this will add into the list as [[R1a,R1b...R1t],[R2a,R2b...R2t],...[R5a,R5b...R5t]]
result.append(scaled_dataset[i:i+sequences].values)
# Converting to array + keras takes float32 better than 64
train_x = np.array(result).astype('float32')
# making the y into same length as X
train_y = np.array(y.tail(train_x.shape[0]).values)
train_x.shape, train_y.shape'>>> (1495, 5, 2400), (1495,)
用另一种方式写下keras的心态塑造了我的问题:
考虑到它是一个时间序列,上面的意思是5天(第0到4行)的数据会产生第5行的值y。在
然后,减去第一天+最后一天之后的第二天-仍然是5天-(行1到5)得到第6行的值y。在
然后,减去第二天+最后一天之后的第二天-仍然是5天-(行2到6)得到第7行的值y。在
对于keras/LSTM的初学者来说,这是相当令人困惑的,但我希望我可以为可能在这里着陆的人详细介绍一下这一点。在