我正在尝试使用来自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中的生成器,并且我没有传递x_test1_scaled,其形状为(5808,5)。编辑:(不是validation_data)
%%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相同。另一件事是,每当第一个时期结束时,错误就会逐渐蔓延,这加强了问题的有效性,即validation_data。
答案 0 :(得分:0)
这是因为您的模型需要两组输入,x_batch_1, x_batch_2
中的batch_generator
。虽然您的validation_data
只有一个数组np.expand_dims(x_test_scaled, axis=0)
您需要使validation_data
看起来像batch_generator
,可能是[np.expand_dims(x_test1_scaled, axis=0), np.expand_dims(x_test2_scaled, axis=0)], np.expand_dims(y_test_scaled, axis=0)
。
如果您仍然不了解,请提供有关x_test1_scaled
的信息,例如形状或加载方式。