考虑一个简单的模型架构:
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]]