检查模型输入时发生错误:预期convolution2d_input_1具有4个维度,但数组的形状为(32,32,3) [英] Error when checking model input: expected convolution2d_input_1 to have 4 dimensions, but got array with shape (32, 32, 3)

查看:984
本文介绍了检查模型输入时发生错误:预期convolution2d_input_1具有4个维度,但数组的形状为(32,32,3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从下一层开始训练深度网络:

I want to train a deep network starting with the following layer:

model = Sequential()
model.add(Conv2D(32, 3, 3, input_shape=(32, 32, 3)))

使用

history = model.fit_generator(get_training_data(),
                samples_per_epoch=1, nb_epoch=1,nb_val_samples=5,
                verbose=1,validation_data=get_validation_data()

使用以下生成器:

def get_training_data(self):
     while 1:
        for i in range(1,5):
            image = self.X_train[i]
            label = self.Y_train[i]
            yield (image,label)

(验证生成器看起来很相似).

(validation generator looks similar).

在培训过程中,我得到了错误:

During training, I get the error:

Error when checking model input: expected convolution2d_input_1 to have 4 
dimensions, but got array with shape (32, 32, 3)

第一层怎么办

 model.add(Conv2D(32, 3, 3, input_shape=(32, 32, 3)))

?

推荐答案

您定义的输入形状是单个样本的形状.该模型本身希望将一些样本数组作为输入(即使其长度为1的数组).

The input shape you have defined is the shape of a single sample. The model itself expects some array of samples as input (even if its an array of length 1).

您的输出实际上应该是4-d,其中第一个维度用于枚举样本.也就是说,对于一张图片,您应该返回(1、32、32、3)形状.

Your output really should be 4-d, with the 1st dimension to enumerate the samples. i.e. for a single image you should return a shape of (1, 32, 32, 3).

您可以在"Convolution2D"/输入形状"下的此处中找到更多信息. "

You can find more information here under "Convolution2D"/"Input shape"

这篇关于检查模型输入时发生错误:预期convolution2d_input_1具有4个维度,但数组的形状为(32,32,3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆