ValueError:检查目标时出错:预期density_2具有4个维,但数组的形状为(7942,1) [英] ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (7942, 1)

查看:98
本文介绍了ValueError:检查目标时出错:预期density_2具有4个维,但数组的形状为(7942,1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用以下功能API来使用CNN进行图像分类:

I have been using the following functional API for an image classification task using CNN:

def create_model(X_train, X_test):

    visible = Input(shape=(X_train.shape[0], X_train.shape[1], 1))
    conv1 = Conv2D(32, kernel_size=4, activation='relu')(visible)
    hidden1 = Dense(10, activation='relu')(pool2)
    output = Dense(1, activation='sigmoid')(hidden1)

    model = Model(inputs = visible, outputs = output)

    model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

    return model

X_tr = np.reshape(X_train, (1,X_train.shape[0], X_train.shape[1], 1))
X_te = np.reshape(X_test, (1,X_test.shape[0],  X_test.shape[1], 1))

model = create_model(X_train, X_test)

model.fit(X_tr, y_train, validation_split = 0.1, batch_size=10, epochs=10, verbose = 1, callbacks=[EarlyStopping(patience=5,verbose=1)]) 

其中,X_train是7942 * 6400的维列表,y_train是带有相应的7942标签的一维列表.

where, X_train is a 7942*6400 dimensional list and y_train being a 1-D list with corresponding 7942 labels.

错误:

ValueError:检查目标时出错:预期density_2具有4 尺寸,但数组的形状为(7942,1)

ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (7942, 1)

当我是功能API的新手时,这里可能出了什么问题?

What has possibly gone wrong here as I am a newbie to the functional API?

推荐答案

该消息表明y_train与模型的输出不兼容.

The message says that y_train is not compatible with the model's output.

您的模型正在输出(None, width, height, 1).卷积后应添加Flatten()层,以使数据从此刻起仅具有2维.

Your model is outputting (None, width, height, 1). You should add a Flatten() layer after the convolution to make the data have only 2 dimensions from this point on.

其他评论:

输入数据必须具有与模型兼容的形状.

The input data must have a shape compatible with the model.

X_train的形状必须为(7942,80,80,1)
模型的input_shape必须为(80,80,1)

The shape of X_train must be (7942,80,80,1)
The input_shape of the model must be (80,80,1)

如果使用(1,6400, 1)形状,则Conv2D图层将毫无用处,因为它无法将数据解释为2D图像.

If you use a (1,6400, 1) shape, your Conv2D layer will be pretty useless, because it will not be able to intepret the data as a 2D image.

这篇关于ValueError:检查目标时出错:预期density_2具有4个维,但数组的形状为(7942,1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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