简体   繁体   English

形状为 (64, 4) 的目标数组被传递给形状为 (None, 3) 的 output,同时用作损失`binary_crossentropy`

[英]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')) #最后一层

In model.compile() please change your loss function.在 model.compile() 请更改您的损失 function。

  • If your labels are is in binary form ie 0 or 1, use BinaryCrossentropy()如果您的标签是二进制形式,即 0 或 1,请使用 BinaryCrossentropy()
  • If your labels are multi-class classification and they are one-hot encoded, use CategoricalCrossentropy()如果您的标签是多类分类并且它们是一次性编码的,请使用 CategoricalCrossentropy()
  • If your labels are multi-class classification and they are in integer form use SparseCategoricalCrossentropy()如果您的标签是多类分类并且它们采用 integer 形式,请使用 SparseCategoricalCrossentropy()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个形状为 (687, 809) 的目标数组被传递给形状 (None, 25) 的输出,同时使用作为损失`binary_crossentropy - A target array with shape (687, 809) was passed for an output of shape (None, 25) while using as loss `binary_crossentropy 形状为 (6400, 1) 的目标数组被传递给形状为 (None, 2) 的 output,同时用作损失`binary_crossentropy` - A target array with shape (6400, 1) was passed for an output of shape (None, 2) while using as loss `binary_crossentropy` ValueError:形状为 (160, 299, 299, 3) 的目标数组被传递给形状为 (None, 1) 的 output,同时用作损失 `binary_crossentropy` - ValueError: A target array with shape (160, 299, 299, 3) was passed for an output of shape (None, 1) while using as loss `binary_crossentropy` 将形状为 (15000, 250) 的目标数组传递给形状为 (None, 1) 的输出,同时使用“binary_crossentropy”作为损失。 我该怎么办? - A target array with shape (15000, 250) was passed for an output of shape (None, 1) while using as loss `binary_crossentropy`. What do I do? 一个形状为 (11203, 25) 的目标数组被传递给形状为 (None, 3) 的输出,同时用作损失`categorical_crossentropy` - A target array with shape (11203, 25) was passed for an output of shape (None, 3) while using as loss `categorical_crossentropy` 形状为 (32, 3) 的目标数组被传递给形状为 (None, 15, 15, 3) 的 output,同时用作损失`categorical_crossentropy` - A target array with shape (32, 3) was passed for an output of shape (None, 15, 15, 3) while using as loss `categorical_crossentropy` ValueError: 形状为 (72148, 23) 的目标数组被传递给形状为 (None, 826, 23) 的 output,同时用作损失`categorical_crossentropy` - ValueError: A target array with shape (72148, 23) was passed for an output of shape (None, 826, 23) while using as loss `categorical_crossentropy` 使用 softmax 作为 output function 而使用 binary_crossentropy 作为损失 function? - Using softmax as output function while using binary_crossentropy as loss function? 类型错误:目标数据丢失。 你的 model 有 `loss`: binary_crossentropy,因此期望目标数据在 `fit()` 中传递 - TypeError: Target data is missing. Your model has `loss`: binary_crossentropy, and therefore expects target data to be passed in `fit()` CNN 值错误输入形状,同时用作损失“categorical_crossentropy”。 此损失期望目标与输出具有相同的形状 - CNN value error input shape while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM