【发布时间】:2020-09-26 21:36:22
【问题描述】:
我正在尝试用取自 DataFrame 的数据训练 LSTM 网络。
代码如下:
x_lstm=x.to_numpy().reshape(1,x.shape[0],x.shape[1])
model = keras.models.Sequential([
keras.layers.LSTM(x.shape[1], return_sequences=True, input_shape=(x_lstm.shape[1],x_lstm.shape[2])),
keras.layers.LSTM(NORMAL_LAYER_SIZE, return_sequences=True),
keras.layers.LSTM(NORMAL_LAYER_SIZE),
keras.layers.Dense(y.shape[1])
])
optimizer=keras.optimizers.Adadelta()
model.compile(loss="mse", optimizer=optimizer)
for i in range(150):
history = model.fit(x_lstm, y)
save_model(model,'tmp.rnn')
这失败了
ValueError: Data cardinality is ambiguous:
x sizes: 1
y sizes: 99
Please provide data which shares the same first dimension.
当我将模型更改为
model = keras.models.Sequential([
keras.layers.LSTM(x.shape[1], return_sequences=True, input_shape=x_lstm.shape),
keras.layers.LSTM(NORMAL_LAYER_SIZE, return_sequences=True),
keras.layers.LSTM(NORMAL_LAYER_SIZE),
keras.layers.Dense(y.shape[1])
])
它失败并出现以下错误:
Input 0 of layer lstm_9 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 1, 99, 1200]
如何让它工作?
x 的形状为 (99, 1200)(99 个项目,每个项目有 1200 个特征,这只是一个更大的数据集的样本),y 的形状为 (99, 1)
【问题讨论】:
-
尝试 x_lstm=x.to_numpy().reshape(x.shape[0],1,x.shape[1]) 与 lstm 中的 input_shape 等于 (x_lstm.shape[1],x_lstm .shape[2])
-
@MarcoCerliani 是的,可行
标签: python tensorflow keras lstm