关键字是“多标签分类”。
在输出层中,您有多个神经元,每个神经元代表您的一个类。
现在您应该对每个神经元单独使用二元分类。
因此,如果您有 3 个类别,则网络的输出可能是 [0.1, 0.8, 0.99],这意味着:第一个类别为真,概率为 10%,第二个类别为真,概率为 80%,最后一个类别为真类为 99%。因此,网络决定对于单个输入图像,两个类同时为真!
在 Keras/Tensorflow 中实现这一点非常容易。
您可以使用一些 binary_crossentropy 作为您的 损失函数 和 Sigmoid 函数作为最后一层中的 激活。因此,您可以获得每个输出神经元的区间 (0, 1) 中的值。
作为指标,您可以使用准确度,它告诉您有多少图像被正确分类(作为相对频率)。
请看下面的例子:
from tensorflow.keras.layers import *
from tensorflow.keras.activations import *
from tensorflow.keras.models import *
from tensorflow.keras.optimizers import *
import numpy as np
# put your data right here:
num_classes = 3 # here I'm assuming 3 classes, e.g. dog, cat and bird
x_train = np.zeros((100, 128, 128, 3)) # I'm just using zeros to simplify the example
y_train = np.zeros((100, num_classes))
model = Sequential()
# put your conv layer / conv blocks here:
model.add(Conv2D(32, kernel_size=3, activation='relu', input_shape=(128, 128, 3)))
model.add(Flatten())
model.add(Dense(units=num_classes, activation='sigmoid'))
model.compile(
loss="binary_crossentropy",
optimizer=Adam(0.005),
metrics=["accuracy"])
training_history = model.fit(x=x_train, y=y_train, epochs=5)
我使用的是 TensorFlow 2.2.0。
我希望这会对你有所帮助:)