[英]How to fix validation_data getting passed into input_1 argument in model.fit_generator?
I'm trying write a hybrid model on the functional API with multiple inputs.我正在尝试在具有多个输入的功能 API 上编写混合 model。 The problem is when training the model I get the ValueError after one epoch:问题是在训练 model 时,我在一个时期后得到 ValueError:
ValueError: Error when checking input: expected input_1 to have shape (168, 5) but got array with shape (5808, 5)
What I'm confused is that is that how did the validation_data (shape (5808,5)) get passed to the input_1 (shape (168,5)) argument in the model.fit_generator?我感到困惑的是,validation_data (shape (5808,5)) 是如何传递给 model.fit_generator 中的 input_1 (shape (168,5)) 参数的?
I've rollbacked to a sequential model to see and confirm if this problem exists, but it trains just fine.我已经回滚到顺序 model 以查看并确认是否存在此问题,但它训练得很好。
This is the model fit func:这是 model 拟合函数:
%%time
model.fit_generator(generator=generator,
epochs=10,
steps_per_epoch=30,
validation_data=validation_data,
callbacks=callbacks)
Its the same as in the sequential data case which works.它与工作的顺序数据情况相同。
The model itself: model 本身:
# 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())
The model summary: model总结:
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 168, 5)] 0
__________________________________________________________________________________________________
input_2 (InputLayer) [(None, 168, 7)] 0
__________________________________________________________________________________________________
dense (Dense) (None, 168, 50) 300 input_1[0][0]
__________________________________________________________________________________________________
lstm (LSTM) (None, 168, 64) 18432 input_2[0][0]
__________________________________________________________________________________________________
concatenate (Concatenate) (None, 168, 114) 0 dense[0][0]
lstm[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 168, 1) 115 concatenate[0][0]
==================================================================================================
The validation data:验证数据:
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))
Note that I do have to pass the entire test data which has 5808 observations.请注意,我必须通过具有 5808 个观察值的整个测试数据。
data_generator数据生成器
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)
https://github.com/Joichiro666/depo/blob/master/LSTM-DNN%20Problem.ipynb https://github.com/Joichiro666/depo/blob/master/LSTM-DNN%20Problem.ipynb
I expect that the validation_data gets passed to the validation_data argument, not the input_1.我希望validation_data 被传递给validation_data 参数,而不是input_1。
When you are doing a fit
with some validation_data
, this validation data will do a forward pass in the network (that's why validation is passed at some point through input_1
).当您使用一些validation_data
进行fit
时,此验证数据将在网络中进行前向传递(这就是验证在某些时候通过input_1
传递的原因)。 Therefore it must fit the requirements of your model, in which you specified that the input data must have a shape of (168, 5)
, which in numpy arrays is going to be (batch_size, 168, 5)
.因此,它必须符合您的 model 的要求,其中您指定输入数据的形状必须为(168, 5)
,在 numpy ZA3CBC3F9D0CE2F2C1554E1B671D_size,1CZ 中是 8 到 8 到 551D_size,1CZ (batch_size, 168, 5)
I think you may have been confused a bit and added the batch_size
in the input shape of your network (I can tell not only by your question but also because you are applying a dense layer on a 2D input).我认为您可能有点困惑,并在网络的输入形状中添加了batch_size
(我不仅可以通过您的问题来判断,还因为您在 2D 输入上应用了密集层)。 In keras you don't need to do that, you should just specify the shape on an instance basis.在 keras 中,您不需要这样做,您应该只根据实例指定形状。 I think if you do that you won't have to expand the dimensions of your inputs anymore.我认为,如果您这样做,您将不必再扩展输入的维度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.