【发布时间】:2021-07-03 03:05:39
【问题描述】:
def build_model(layers):
model = Sequential()
# By setting return_sequences to True we are able to stack another LSTM layer
model.add(LSTM(layers[0], input_shape=(1, 2), return_sequences=True))
model.add(LSTM(layers[0], input_shape=(1, 2),
return_sequences=False))
model.add(Dropout(0.2))
model.add(Activation("linear"))
start = time.time()
model.compile(loss="mse", optimizer="rmsprop", metrics=['accuracy'])
print("Compile Time : ", time.time() - start)
return model
然后当我尝试在构建后运行 model.fit 时。那是抛出错误的时候。这是正在构建的模型和 model.fit 函数的代码的 sn-p。
window = 20
print("X_train", X_train.shape)
print("y_train", y_train.shape)
print("X_test", X_test.shape)
print("y_test", y_test.shape)
model = build_model([1374, window, 100, 1])
model.fit(X_train,
y_train,
batch_size=3,
epochs=5,
validation_split=0.1,
verbose=0).
这是错误消息。 ValueError:“顺序”层的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。什么是ndim?它的值对模型有何调整?我如何理解我设置的 ndim。
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 2).
这是打印出来的形状。
X_train (1032, 2)
y_train (1032, 2)
X_test (344, 2)
y_test (344, 2)
【问题讨论】:
-
LSTM layer is a recurrent layer, hence it expects a 3-dimensional input (batch_size, timesteps, input_dim)。在问题中包含X_train形状。
标签: python tensorflow keras deep-learning python-3.6