我正在试验MNIST数据集,并且试图获取并打印出我的神经网络犯下的前5个错误的图像。最高误差定义为预测值的概率与实际值之间的最大距离。 我正在使用MatPlotLib.pyplot函数中的plt通过“ plt.imshow”打印图像。
这是我打印出最常见错误的方法:
下面是我遇到问题的代码部分:
#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))