【发布时间】:2020-03-06 17:05:56
【问题描述】:
如果这个问题看起来很愚蠢,我提前道歉,但我想了解 machinelearningmastery blog 关于 LSTM 类型 ML 算法的内容,更具体地说,是关于输入数据如何被重塑。而且我在这方面没有太多的智慧或CS学位
关于LSTM CNN部分的博客大约进入一半,Jason谈到:
第一步是将输入序列拆分为子序列 可以通过 CNN 模型进行处理。例如,我们可以先拆分我们的 通过四个步骤将单变量时间序列数据转换为输入/输出样本 作为输入,一个作为输出。然后每个样本可以分成两个 子样本,每个都有两个时间步长。 CNN 可以解释每个 两个时间步的子序列,并提供一个时间序列 将 LSTM 模型的子序列解释为 输入。
我们可以参数化它并将子序列的数量定义为 n_seq 和每个子序列的时间步数为 n_steps。这 然后可以将输入数据重新整形以具有所需的结构:
[samples, subsequences, timesteps, features]
我的问题是这是否要求数据仅成型为4 steps?或者它可以更大吗?下面的代码将尝试打印数组,我在我的 git 帐户上使用我自己的数据样本 here。
import pandas as pd
import numpy as np
# univariate data preparation
from numpy import array
df = pd.read_csv("trainData.csv")
df = df[['kW']].shift(-1)
df = df.dropna()
raw_seq = df.values.tolist()
# split a univariate sequence into samples
def split_sequence(sequence, n_steps):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the sequence
if end_ix > len(sequence)-1:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
# define input sequence
#raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# choose a number of time steps
n_steps = 4
# split into samples
X, y = split_sequence(raw_seq, n_steps)
# reshape from [samples, timesteps] into [samples, subsequences, timesteps, features]
n_features = 1
n_seq = 2
n_steps = 2
X = X.reshape((X.shape[0], n_seq, n_steps, n_features))
# summarize the data
for i in range(len(X)):
print(X[i], y[i])
上面的代码有效,但是当我更改 n_steps = 7(从 4 开始)时,我得到了这个形状错误。
File "convArray.py", line 39, in <module>
X = X.reshape((X.shape[0], n_seq, n_steps, n_features))
ValueError: cannot reshape array of size 2499 into shape (357,2,2,1)
我想尝试使用 7 个时间步长的原因是我正在试验的数据是建筑物每天的电力需求单位,一周 7 天将是一个理想的实验时间步长!
非常感谢任何提示
【问题讨论】:
标签: python tensorflow machine-learning keras lstm