【问题标题】: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)您传递给模型的 Numpy 数组列表不是模型预期的大小。预计会看到 2 个数组
【发布时间】:2019-01-10 13:13:10
【问题描述】:

在 Keras 中遇到该错误。 设想: 输入:

  1. 火车形状为 (50000, 32, 32, 3) 的图像
  2. 形状为 (50000, 1) 的辅助输入
  3. 基本事实:(50000, 1)

这是模型拟合代码

x_train_input = Input(shape=(32,32,3))

aux_rand_input = Input(shape=(1,))

out = model_inst.build_model(x_train_input, aux_rand_input)

model = Model(inputs=[x_train_input, aux_rand_input], outputs=[out])

model.fit(x=[x_train, aux_input], y=y_train, batch_size=batch_size, steps_per_epoch=x_train.shape[0] // batch_size, epochs=maxepoches, validation_data=(x_test, y_test), callbacks=[reduce_lr, tensorboard], verbose=2)

运行时出现此错误。

检查模型输入时出错:您所在的 Numpy 数组列表 传递给您的模型不是模型预期的大小。预计 查看 2 个数组,但得到了以下 1 个数组的列表:

这就是build_model 的最后几层的样子。

    flatten = Flatten()(drop_5)
    # aux_input = Input(shape=(1,))
    concat = Concatenate(axis=1)([flatten, aux_input])

    fc1 = Dense(512, kernel_regularizer=regularizers.l2(weight_decay))(concat)
    fc1 = Activation('relu')(fc1)
    fc1 = BatchNormalization()(fc1)

    fc1_drop = Dropout(0.5)(fc1)
    fc2 = Dense(self.num_classes)(fc1_drop)
    out = Activation('softmax')(fc2)
    return out

【问题讨论】:

    标签: python numpy keras


    【解决方案1】:

    对于验证数据,您只传递一个数组作为输入。

    model.fit(x=[x_train, aux_input], y=y_train, batch_size=batch_size, steps_per_epoch=x_train.shape[0] // batch_size, epochs=maxepoches, validation_data=(x_test, y_test), callbacks=[reduce_lr, tensorboard], verbose=2)
    

    您应该同时传递 aux_rand_inputx_train_input 的值。如果您有aux_test 变量来保存aux_rand_input 的测试数据,那么可以按如下方式完成

    model.fit(x=[x_train, aux_input], y=y_train, batch_size=batch_size, steps_per_epoch=x_train.shape[0] // batch_size, epochs=maxepoches, validation_data=([x_test, aux_test], y_test), callbacks=[reduce_lr, tensorboard], verbose=2)
    

    编辑:

    要使用model.fit_generator 方法,生成器必须产生两个元素的元组或列表,其中第一个元素由两个数组组成。例如

    def generator(x, aux, y):
       ## part of the code...
       yield [batch_x, batch_aux], batch_y
    

    【讨论】:

    • 只是一个断章取义的问题 - 我如何使用 model.fit_generator 来做同样的事情?
    猜你喜欢
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    相关资源
    最近更新 更多