您传递给模型的Numpy数组列表不是模型期望的大小。预计会看到2个阵列

时间:2019-01-10 13:13:10

标签: python numpy keras

在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

1 个答案:

答案 0 :(得分: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