将Keras Functional API与fit_generator一起使用时的输入形状错误 [英] Input Shape error when using Keras Functional API with fit_generator
问题描述
我已经使用Keras Functional API构建了一个模型,并且在火车上调用fit
时,它可以正常工作.现在我决定更改模型以使用我的生成器
I've built a model using Keras Functional API and it was working correct when calling fit
on train set. Now I decided to change model to use my generator
def data_generator():
while 1:
for i in range(len(sequences1)):
yield ([sequences1[i], sequences2[i]], trainLabels[i])
这是我的数据集中的示例数据
and here is a sample data from my dataset
sample = next(data_generator())
print(sample)
print(sample[0][0].shape)
# output:
# ([array([ 0, 0, 0, ..., 10, 14, 16], dtype=int32), array([ 0, 0, 0, ..., 19, 1, 4], dtype=int32)], 1)
# (34350,)
这是我的模型摘要(仅前两个部分)
and here is my model summary (just the first two part)
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 34350) 0
__________________________________________________________________________________________________
input_2 (InputLayer) (None, 34350) 0
但是当我尝试使用此代码拟合模型时
but when I'm trying to fit my model using this code
model.fit_generator(data_generator(), epochs=15, steps_per_epoch=64)
我遇到此错误
ValueError: Error when checking input: expected input_1 to have shape (34350,) but got array with shape (1,)
我该如何解决?
推荐答案
问题是生成器必须生成数据 batch-by-batch .换句话说,sample[0][0].shape
应该为(BATCH_SIZE, 34350)
,第二个序列和标签也应如此.
The problem is that the generator must generate the data batch-by-batch. In other words, sample[0][0].shape
should be (BATCH_SIZE, 34350)
, and the same applies to the second sequence and the labels.
这篇关于将Keras Functional API与fit_generator一起使用时的输入形状错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!