如何从MNIST获取Top错误?

2024-03-29 12:37:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用MNIST数据集进行实验,我试图获得并打印出我的神经网络所犯的前5个错误的图像。顶部误差定义为预测值概率与实际值之间的最大距离。 我正在打印图像plt.imshow公司“,plt来自MatPlotLib.pyplot文件功能。你知道吗

下面是我打印最常见错误的方法:

  1. 打印包含期望值和实际值之间概率差异的errors\u prob数组的最大值索引。你知道吗
  2. 删除我刚打印出索引的值(使用np.删除)你知道吗
  3. 重复此过程5次

下面是我遇到问题的代码部分:

#wrong
actual_classes = #list numbers the network should predict, actual/expected
#list of the index of the errors/incorrect predictions
errors = np.asarray(np.nonzero(network.predict_classes(test_images) != actual_classes)).T
#errors_sorted = the difference in probability between what network predicted and what it should have predicted (predicted-expected)
errors_prob = np.zeros(len(errors)).reshape(len(errors),1)
for i in range(0,len(errors)):
  errors_prob[i] = np.abs((predicted_prob[int(errors[i])][int(actual_classes[int(errors[i])])]-predicted_prob[int(errors[i])][int(predicted_classes[int(errors[i])])]))

#print out the top 5 errors
for i in range(0,5):
    plt.imshow(train_images[np.argmax(errors_prob)])
    errors_prob = np.delete(errors_sorted,np.max(errors_prob))

下面是我如何设置网络的,以备您咨询。你知道吗

from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import to_categorical
import matplotlib.pyplot as plt
import numpy as np


(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

#process the data

train_images = train_images.reshape((60000, 28*28))
train_images = train_images.astype('float32')/255

test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype('float32')/255

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)


#create the model
network = models.Sequential()
network.add(layers.Dense(16, activation='relu', input_shape=(28*28,)))
network.add(layers.Dense(16, activation='tanh', input_shape=(28*28,)))
network.add(layers.Dense(10, activation='softmax'))

network.compile(optimizer = 'rmsprop', 
                loss='categorical_crossentropy',
                metrics=['accuracy'])


#train and evaluate the images
network.fit(train_images, train_labels, epochs=5, batch_size=128)
test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)
print (network.summary())


#todo: 
predicted_classes = network.predict_classes(test_images)
predicted_prob = network.predict_proba(test_images)

#wrong
actual_classes = np.zeros_like(predicted_classes)
for i in range(0,len(test_labels)):
    for j in range(0,len(test_labels[i])):
        if(test_labels[i][j] == 1):
            actual_classes[i] = j
errors = np.asarray(np.nonzero(network.predict_classes(test_images) != actual_classes)).T
errors_sorted = np.zeros(len(errors)).reshape(len(errors),1)
for i in range(0,len(errors)):
  errors_sorted[i] = np.abs((predicted_prob[int(errors[i])][int(actual_classes[int(errors[i])])]-predicted_prob[int(errors[i])][int(predicted_classes[int(errors[i])])]))

for i in range(0,5):
    print(np.argmax(errors_sorted))
    errors_sorted = np.delete(errors_sorted,np.max(errors_sorted))

Tags: thetestlabelslennptrainnetworkclasses