【发布时间】:2021-03-21 23:19:28
【问题描述】:
我正在尝试在我的时间序列数据上实现 CNN-LSTM 网络模型。
verbose, epochs, batch_size = 0, 25, 64
n_timesteps, n_features, n_outputs =
X_train.shape[1], X_train.shape[2], y_train.shape[1]
# reshape data into time steps of sub-sequences
n_steps, n_length = 4, 32
X_train = X_train.reshape((X_train.shape[0], n_steps,
n_length, n_features))
X_test = X_test.reshape((X_test.shape[0], n_steps,
n_length, n_features))
# define model
model = Sequential()
model.add(TimeDistributed(Conv1D(filters=64,
kernel_size=3, activation='relu'), input_shape=
(None,n_length,n_features)))
model.add(TimeDistributed(Conv1D(filters=64,
kernel_size=3, activation='relu')))
model.add(TimeDistributed(Dropout(0.5)))
model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(100))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=epochs,
batch_size=batch_size, verbose=verbose)
以下是使用 reshape 之前的形状:
print(X_train.shape, y_train.shape, X_test.shape,
y_test.shape)
(51135, 128, 12) (51135, 1) (21915, 128, 12) (21915, 1)
这是我遇到错误的代码部分:
ValueError: cannot reshape array of size 78543360 into shape (51135, 4,32,32)。
我该如何解决这个问题?
【问题讨论】:
标签: python tensorflow time-series conv-neural-network lstm