【发布时间】:2020-02-10 14:59:57
【问题描述】:
我正在尝试使用 Hvass-Labs 时间序列教程 #23 中的代码实现具有多个输入的混合 LSTM-DNN 预测器。基本上,我想使用顺序和非顺序数据预测电力的日前价格(现在只是未来 24 个时间步长)。我使用的模型是两组输入,分别为 LSTM(用于顺序数据)和 Dense 用于非顺序数据,它们的输出连接在一起。它看起来像这样:
基本上每当我尝试在一个时期后拟合模型时,它都会显示此错误:
更新:
ValueError: Error when checking input: expected input_1 to have shape (168, 5) but got array with shape (5808, 5)
我实施的改变:
# Chop off x_test_scaled into two parts:
x_test1_scaled = x_test_scaled[:,0:5] # shape is (5808, 5)
x_test2_scaled = x_test_scaled[:,5:12] # shape is (5808, 7)
validation_data = [np.expand_dims(x_test1_scaled, axis=0), np.expand_dims(x_test2_scaled, axis=0)], np.expand_dims(y_test_scaled, axis=0)
我很困惑,因为我确实已经将生成器分配给了 model.fit_generator 中的生成器,而且我没有传递形状为 (5808, 5) 的 x_test1_scaled。编辑:(不是验证数据)
%%time
model.fit_generator(generator=generator,
epochs=10,
steps_per_epoch=30,
validation_data=validation_data,
callbacks=callbacks)
如果这有帮助,这是我的模型:
# first input model
input_1 = Input(shape=((168,5)))
dense_1 = Dense(50)(input_1)
# second input model
input_2 = Input(shape=((168,7)))
lstm_1 = LSTM(units=64, return_sequences=True, input_shape=(None, 7,))(input_2)
# merge input models
merge = concatenate([dense_1, lstm_1])
output = Dense(num_y_signals, activation='sigmoid')(merge)
model = Model(inputs=[input_1, input_2], outputs=output)
# summarize layers
print(model.summary())
编辑:清除此问题,替换为顶部错误。
到目前为止,我已经完成了一切以实际拟合模型。 每当一个时代结束时,它就会进入错误:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[0.4 , 0.44444442, 0. , ..., 0.1734707 ,
0.07272629, 0.07110982],
[0.3904762 , 0.43434343, 0.04347826, ..., 0.1740398 ,
0.07282589, 0.06936309],
...
我已经尝试过来自其他 stackexchange 帖子的相同错误消息的解决方案。他们没有成功,但我最终能够将问题数组与validation_data 隔离开来。我只是不知道如何将其“重塑”为所需的 2 数组。
批处理生成器:我已经包含了两组输入。 x_batch_1 和 x_batch_2
def batch_generator(batch_size, sequence_length):
"""
Generator function for creating random batches of training-data.
"""
# Infinite loop.
while True:
# Allocate a new array for the batch of input-signals.
x_shape = (batch_size, sequence_length, num_x_signals)
x_batch = np.zeros(shape=x_shape, dtype=np.float16)
# Allocate a new array for the batch of output-signals.
y_shape = (batch_size, sequence_length, num_y_signals)
y_batch = np.zeros(shape=y_shape, dtype=np.float16)
# Fill the batch with random sequences of data.
for i in range(batch_size):
# Get a random start-index.
# This points somewhere into the training-data.
idx = np.random.randint(num_train - sequence_length)
# Copy the sequences of data starting at this index.
x_batch[i] = x_train_scaled[idx:idx+sequence_length]
y_batch[i] = y_train_scaled[idx:idx+sequence_length]
x_batch_1 = x_batch[ :, :, 0:5]
x_batch_2 = x_batch[ :, :, 5:12]
yield ([x_batch_1, x_batch_2], y_batch)
batch_size = 32
sequence_length = 24 * 7
generator = batch_generator(batch_size=batch_size,
sequence_length=sequence_length)
验证集:
validation_data = np.expand_dims(x_test_scaled, axis=0), np.expand_dims(y_test_scaled, axis=0)
最后是模型拟合:
%%time
model.fit_generator(generator=generator,
epochs=10,
steps_per_epoch=30,
validation_data=validation_data,
callbacks=callbacks)
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[0.4 , 0.44444442, 0. , ..., 0.1734707 ,
0.07272629, 0.07110982],
[0.3904762 , 0.43434343, 0.04347826, ..., 0.1740398 ,
0.07282589, 0.06936309],
...
该数组与validation_data 相同。另一件事是,每当第一个 epoch 结束时,错误就会蔓延,这加强了验证数据问题的案例。
【问题讨论】:
标签: python numpy tensorflow keras lstm