Input Shape error when using Keras Functional API

2019-07-20 06:51发布

问题:

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)

I'm getting this error

ValueError: Error when checking input: expected input_1 to have shape (34350,) but got array with shape (1,)

How can I fix it?

回答1:

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.