【问题标题】:Input Shape error when using Keras Functional API with fit_generator使用带有 fit_generator 的 Keras 功能 API 时出现输入形状错误
【发布时间】:2018-10-18 19:52:05
【问题描述】:

我使用 Keras 功能 API 构建了一个模型,并且在火车集上调用 fit 时它工作正常。现在我决定改变模型来使用我的生成器

def data_generator():
    while 1:
        for i in range(len(sequences1)):
            yield ([sequences1[i], sequences2[i]], trainLabels[i])

这是我数据集中的示例数据

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,)

这是我的模型总结(只是前两部分)

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 34350)        0                                            
__________________________________________________________________________________________________
input_2 (InputLayer)            (None, 34350)        0      

但是当我尝试使用此代码拟合我的模型时

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,)

我该如何解决?

【问题讨论】:

    标签: python-3.x keras generator


    【解决方案1】:

    问题在于生成器必须逐批生成数据。换句话说,sample[0][0].shape 应该是(BATCH_SIZE, 34350),同样适用于第二个序列和标签。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多