[英]A target array with shape (64, 4) was passed for an output of shape (None, 3) while using as loss `binary_crossentropy`
# Organize file names and class labels in X and Y variables
prepareNameWithLabels(classLabels[0])
prepareNameWithLabels(classLabels[1])
prepareNameWithLabels(classLabels[2])
prepareNameWithLabels(classLabels[3])
X=np.asarray(X)
Y=np.asarray(Y)
# learning rate
batch_size = 64
epoch=50
activationFunction='relu'
def getModel():
model = Sequential()
model.add(Conv2D(64, (3, 3), padding='same', activation=activationFunction, input_shape=(img_rows, img_cols, 3)))
model.add(Conv2D(64, (3, 3), activation=activationFunction))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(32, (3, 3), padding='same', activation=activationFunction))
model.add(Conv2D(32, (3, 3), activation=activationFunction))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(16, (3, 3), padding='same', activation=activationFunction))
model.add(Conv2D(16, (3, 3), activation=activationFunction))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64, activation=activationFunction)) # we can drop
model.add(Dropout(0.1)) # this layers
model.add(Dense(32, activation=activationFunction))
model.add(Dropout(0.1))
model.add(Dense(16, activation=activationFunction))
model.add(Dropout(0.1))
model.add(Dense(3, activation='softmax'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return model
The Following Errors Pops Out弹出以下错误
ValueError: A target array with shape (64, 4) was passed for an output of shape (None, 3) while using as loss
binary_crossentropy
.ValueError: 形状为 (64, 4) 的目标数组被传递给形状为 (None, 3) 的 output,同时用作损失
binary_crossentropy
。 This loss expects targets to have the same shape as the output.这种损失预计目标具有与 output 相同的形状。
As your code reflects, you have 4 separate classes.正如您的代码所反映的,您有 4 个单独的类。 So, your last layer (output layer) should have 4 neurons, but you have specified 3. Change output units to 4.
因此,您的最后一层(输出层)应该有 4 个神经元,但您指定了 3 个。将 output 单位更改为 4。
Additionally, Your model output has more than one neuron, but your loss function is binary_crossentropy
.此外,您的 model output 有多个神经元,但您的损失 function 是
binary_crossentropy
。 Note that you can only use binary_crossentropy
if you have only one output as with value 0 and 1, or you have multi output for multi-label problems (It is possible more than one class at the same time activated, not limited to only one class).请注意,如果您只有一个 output 值 0 和 1,或者您有多个 output 用于多标签问题,则只能使用
binary_crossentropy
(在同一时间激活多个 ZA2F2ED4F8EBC2CBB4DC21A2 类时可能有多个 ZA2F2ED4F8EBC2CBB4DC21A29 )。
If you have multiple class classification, and your targets ( y_train
) are one hot encoded you may use categorical_crossentropy
and if it is not one hot encoded you can use sparse_categorical_crossentropy
as loss function.如果您有多个 class 分类,并且您的目标 (
y_train
) 是一种热编码,则可以使用categorical_crossentropy
,如果不是一种热编码,则可以使用sparse_categorical_crossentropy
作为损失 function。
You have 4 separate classes.您有 4 个单独的课程。 So, your last layer (output layer) should have 4 neurons, not 3 neurons.
所以,你的最后一层(输出层)应该有 4 个神经元,而不是 3 个神经元。 Change output units to 4.
将 output 单位更改为 4。
model.add(Dense(4, activation='softmax')) # Last layer model.add(Dense(4, activation='softmax')) #最后一层
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.