检查目标时出错:预期density_3具有2维,但数组的形状为(5,200,200,1)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检查目标时出错:预期density_3具有2维,但数组的形状为(5,200,200,1)相关的知识,希望对你有一定的参考价值。
我刚刚开始使用Python 3.7.7学习Tensorflow(2.1.0)和Keras(2.3.7)。
我有这个网络:
def vgg16(input_size = (224,224,3)):
inputs = Input(input_size, name='input')
conv1 = Conv2D(64, (3, 3), activation = 'relu', padding = 'same', name ='conv1_1')(inputs)
conv1 = Conv2D(64, (3, 3), activation = 'relu', padding = 'same', name ='conv1_2')(conv1)
pool1 = MaxPooling2D(pool_size = (2,2), strides = (2,2), name = 'pool_1')(conv1)
conv2 = Conv2D(128, (3, 3), activation = 'relu', padding = 'same', name ='conv2_1')(pool1)
conv2 = Conv2D(128, (3, 3), activation = 'relu', padding = 'same', name ='conv2_2')(conv2)
pool2 = MaxPooling2D(pool_size = (2,2), strides = (2,2), name = 'pool_2')(conv2)
conv3 = Conv2D(256, (3, 3), activation = 'relu', padding = 'same', name ='conv3_1')(pool2)
conv3 = Conv2D(256, (3, 3), activation = 'relu', padding = 'same', name ='conv3_2')(conv3)
conv3 = Conv2D(256, (3, 3), activation = 'relu', padding = 'same', name ='conv3_3')(conv3)
pool3 = MaxPooling2D(pool_size = (2,2), strides = (2,2), name = 'pool_3')(conv3)
conv4 = Conv2D(512, (3, 3), activation = 'relu', padding = 'same', name ='conv4_1')(pool3)
conv4 = Conv2D(512, (3, 3), activation = 'relu', padding = 'same', name ='conv4_2')(conv4)
conv4 = Conv2D(512, (3, 3), activation = 'relu', padding = 'same', name ='conv4_3')(conv4)
conv5 = Conv2D(512, (3, 3), activation = 'relu', padding = 'same', name ='conv5_1')(pool4)
conv5 = Conv2D(512, (3, 3), activation = 'relu', padding = 'same', name ='conv5_2')(conv5)
conv5 = Conv2D(512, (3, 3), activation = 'relu', padding = 'same', name ='conv5_3')(conv5)
pool5 = MaxPooling2D(pool_size = (2,2), strides = (2,2), name = 'pool_5')(conv5)
flatten = Flatten(name = 'flatten')(pool5)
dense1 = Dense(units = 4096,activation = "relu", name = 'dense_1')(flatten)
dense2 = Dense(units = 4096,activation = "relu", name = 'dense_2')(dense1)
dense3 = Dense(units = 2, activation = "softmax", name = 'dense_3')(dense2)
opt = Adam(lr=0.001)
model = Model(inputs = inputs, outputs = dense3, name ='vgg-16')
model.compile(optimizer=opt, loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])
return model
这是使用它的代码:
model = vgg16.vgg16((200, 200, 1))
for episode in range(num_episodes):
selected = np.random.permutation(no_of_samples)[:num_shot + num_query]
# Create our Support Set.
support_set = np.array(D[selected[:num_shot]])
# Create our Query Set.
query_set = np.array(D[selected[num_query:]])
X_train = support_set[:,0,:]
y_train = support_set[:,1,:]
X_valid = query_set[:,0,:]
y_valid = query_set[:,1,:]
results = model.fit(X_train, y_train, epochs=20, batch_size=5,
validation_data=(X_valid, y_valid))
[X_train
,y_train
,X_valid
和y_valid
具有以下形状:(5, 200, 200, 1)
。
但我收到此错误:
检查目标时出错:预期density_3具有2维,但是得到形状为(5,200,200,1)的数组]
我不明白为什么。我更改了fit
调用,删除了batch_size
:
results = model.fit(X_train, y_train, epochs=20, validation_data=(X_valid, y_valid))
但是我遇到同样的错误:
如何解决此错误?
也许,问题在于我正在使用一个频道图像。
您的训练数据具有5
,(200,200,1)
图像,这是正确的,但是目标数据(y
)应该是5
对应的标签,形状为(2,)
。因此,y_train
的形状应为(5,2)
。现在它们的形状不正确。
[
X_train
,y_train
,X_valid
和y_valid
具有以下形状:(5, 200, 200,1)
。
根据您的网络摘要
>> vvg16().summary()
Model: "vgg-16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input (InputLayer) [(None, 224, 224, 3)] 0
_________________________________________________________________
conv1_1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
conv1_2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
pool_1 (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
conv2_1 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
conv2_2 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
pool_2 (MaxPooling2D) (None, 56, 56, 128) 0
_________________________________________________________________
conv3_1 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
conv3_2 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
conv3_3 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
pool_3 (MaxPooling2D) (None, 28, 28, 256) 0
_________________________________________________________________
conv4_1 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
conv4_2 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
conv4_3 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
pool_4 (MaxPooling2D) (None, 14, 14, 512) 0
_________________________________________________________________
conv5_1 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
conv5_2 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
conv5_3 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
pool_5 (MaxPooling2D) (None, 7, 7, 512) 0
_________________________________________________________________
flatten (Flatten) (None, 25088) 0
_________________________________________________________________
dense_1 (Dense) (None, 4096) 102764544
_________________________________________________________________
dense_2 (Dense) (None, 4096) 16781312
_________________________________________________________________
dense_3 (Dense) (None, 2) 8194
=================================================================
Total params: 134,268,738
Trainable params: 134,268,738
Non-trainable params: 0
_________________________________________________________________
您的X_train
和X_valid
必须具有(224,224,3)
的形状由于y_train
],您的y_valid
和2 classes
总计必须为keras.losses.categorical_crossentropy
,并且必须进行一次热编码
编辑:
[当输出是N个类的数组时:
- 最后一层应为
Dense(N,"softmax")
- 损失应为
keras.losses.sparse_categorical_crossentropy
[当输出是单编码的N
- 如果尚未编码,请使用
keras.utils.to_categorical(array, N)
- 最后一层应为
Dense(N,"softmax")
- 损失应为
keras.losses.categorical_crossentropy
当目的不是分类时:
- 您的最后一层应产生相同的形状
- 您需要使用适当的激活
- 您不需要使用拼合
- 检查
autoencoder
概念,以了解如何产生与输入形状相同的输出
以上是关于检查目标时出错:预期density_3具有2维,但数组的形状为(5,200,200,1)的主要内容,如果未能解决你的问题,请参考以下文章
图像分类器ValueError:检查目标时出错:预期dense_31有2维,但得到的数组形状为(1463、224、224、3)
值错误:检查目标时出错:预期dense_1具有形状(无,1)但得到的数组具有形状(6000,3)
ValueError:检查输入时出错:预期dense_11_input 具有3 维,但得到了形状为(0, 1) 的数组
ValueError:检查目标时出错:预期(keras 序列模型层)具有 n 维,但得到的数组具有形状
ValueError:检查目标时出错:预期activation_6具有形状(无,2)但得到的数组具有形状(5760,1)