我如何计算或绘制 CNN 中每个 class 的误差?

How can i calculate or plot error of each class in CNN?

我将此代码用于我的项目,但我想计算或绘制每个 class.I 的错误有 6 class。我该怎么办?

def plot_history(net_history):
    history = network_history.history
    losses = history['loss']
    accuracies = history['acc']
    plt.xlabel('Epochs')
    plt.ylabel('loss')
    plt.plot(losses)

    plt.figure()
    plt.xlabel('Epochs')
    plt.ylabel('accuracy')
    plt.plot(accuracies)

创建我的模型

myinput = layers.Input(shape=(100,200))
conv1 = layers.Conv1D(16, 3, activation='relu', padding='same', strides=2)(myinput)
conv2 = layers.Conv1D(32, 3, activation='relu', padding='same', strides=2)(conv1)
flat = layers.Flatten()(conv2)
out_layer = layers.Dense(6, activation='softmax')(flat)

mymodel = Model(myinput, out_layer)
mymodel.summary()
mymodel.compile(optimizer=keras.optimizers.Adam(), 
loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])

训练我的模型

network_history = mymodel.fit(X_train, Y_train, batch_size=128,epochs=5, validation_split=0.2)
plot_history(network_history)

评价

test_loss, test_acc = mymodel.evaluate(X_test, Y_test)

test_labels_p = mymodel.predict(X_test)

评估 classifier 的一种简单方法是 scikit-learn 中的 classification_report:

from sklearn.metrics import classification_report

....

# Actual predictions here, not just probabilities
pred = numpy.round(mymodel.predict(X_test))
print(classification_report(Y_test, pred))

其中 Y_test 是 one-hot 个向量的列表。

这将显示每个 class 的精确度、召回率和 f1 度量。缺点是只考虑预测的正确与否,没有考虑模型的确定性。

你必须像二元分类问题一样训练它,然后你可以使用这段代码为不同的 类:

制作学习曲线
plt.plot(network_history.history['loss'])
plt.plot(network_history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()