在训练LSTM模型时,我几乎在第0天试图理解[samples,timesteps,features]模式。
假设我们有一个时间序列T:
- T=[1,2,3,4,5,6,7,8]
- 滞后/回顾=6
将计算以下两组输入输出:
-
{[1,2,3,4,5,6]-7}
-
{[2,3,4,5,6,7]-8}
我知道这个特定例子中的样本数量等于2,但我注意到有些事情有效,我不明白为什么/如何在输入上使用(1(输入-输出组合:
a。将模型的input_size设置为(1.6(,并保持输入为[1,2,3,4,5,6]
b。将模型的input_size设置为(2,3(,并将输入转换为以下格式[[1,2,3][4,5,6]]
a和b之间有什么区别?
如果我错了,请纠正我,但在我看来,在(a(中,我一次使用6个长度的输入来训练NN,而在(b(中,通过一次给每个输入两个三元组来训练NN。此外,这对NN有何影响?
也许(问号(这与LSTM模型中的内存概念有关。
顺便说一下,我正在使用tensorflow.keras api。
考虑一个简单的模型架构:
model = keras.Sequential()
model.add(LSTM(units=16, input_shape=(n_timesteps, n_features), activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit(X, y, batch_size=4, epochs=200, verbose=0)
假设我们有以下具有6个功能的数据集:
df = pd.DataFrame(data={'F1':np.arange(1, 21, 1),
'F2':np.arange(11, 31, 1),
'F3':np.arange(51, 71, 1),
'F4':np.arange(71, 91, 1),
'F5':np.arange(46, 66, 1),
'F6':np.arange(26, 46, 1)})
Now consider Case A i.e. Set the model's input_size as (1,6):
在这种情况下,为模型提供的数据的形状为(4, 1, 6)
。即使用1 timestep
、6 features
和batch size of 4
。
让我们将数据重塑为3D input
X = df.values
X = X.reshape((X.shape[0], 1, X.shape[1]))
由于我们的批次大小为4
,您的一个批次看起来像这样:
[[[ 1 11 51 71 46 26]]
[[ 2 12 52 72 47 27]]
[[ 3 13 53 73 48 28]]
[[ 4 14 54 74 49 29]]]
Now consider Case B: Set the model's input_size as (2,3):
您为模型提供的数据的形状为(8, 2, 3)
。即使用2 timesteps
、3 features
和batch size of 4
。
让我们将数据重塑为3D input
X = df.values
X = X[:, :3] #As you are using 3 features in this case.
X = X.reshape((10, 2, X.shape[1]))
由于我们的批次大小为4
,现在,您的一个批次看起来像这样:
[[[ 1 11 51]
[ 2 12 52]]
[[ 3 13 53]
[ 4 14 54]]
[[ 5 15 55]
[ 6 16 56]]
[[ 7 17 57]
[ 8 18 58]]]
Case C: Set the model's input_size as (2,6):
您为模型提供的数据的形状为(8, 2, 6)
。即使用2 timesteps
、6 features
和batch size of 4
。
让我们将数据重塑为3D input
X = df.values
X = X.reshape((10, 2, X.shape[1]))
由于我们的批次大小为4
,现在,您的一个批次看起来像这样:
[[[ 1 11 51 71 46 26]
[ 2 12 52 72 47 27]]
[[ 3 13 53 73 48 28]
[ 4 14 54 74 49 29]]
[[ 5 15 55 75 50 30]
[ 6 16 56 76 51 31]]
[[ 7 17 57 77 52 32]
[ 8 18 58 78 53 33]]