简体   繁体   English

如何在Keras中验证预测

[英]How to validate a prediction in Keras

I am going through the Kaggle Digit Recognizer Tutorial and I'm trying to understand how all of this works. 我正在阅读Kaggle Digit Recognizer教程,并且试图了解所有这些工作原理。 I would like to validate a predicted value. 我想验证一个预测值。 Basically, I have a prediction that's wrong, but I want to see what the actual value of that prediction was. 基本上,我有一个错误的预测,但是我想看看该预测的实际价值是什么。 I think I am way off: 我想我还有一段路要走:

...

df = pd.read_csv('data/train.csv')

labels = df['label'].values
x_train = df.drop(columns=['label']).values / 255
# trying to produce a crappy dataset for train/test
x_train, x_test, y_train, y_test = train_test_split(x_train, labels, test_size=0.95)


# Purposely trying to get a crappy model so I can learn about validation
model = tf.keras.models.Sequential()
# model.add(tf.keras.layers.Flatten())
# model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=1)
predictions = model.predict([x_test])


index_to_predict = 0
print('Prediction: ', np.argmax(predictions[index_to_predict]))
print('Actual:     ', predictions.argmax(axis=-1)[index_to_predict])
print(predictions.shape)
vals = x_test[index_to_predict].reshape(28, 28)
plt.imshow(vals)

This yields the following: 这将产生以下结果:

在此处输入图片说明

How can I get a true 'heres the prediction' and 'heres the actual' breakdown? 如何获得真正的“预测在这里”和“实际在这里”细分? My logic on getting the actual is definitely off. 我关于实际的逻辑肯定是不正确的。

The true labels (also sometimes called target values, or ground-truth labels) are stored in y_train and y_test for training and test set respectively. 真实标签(有时也称为目标值或地面真实标签)存储在y_trainy_test ,分别用于训练和测试集。 Therefore, you can easily just print that to find the true label: 因此,您可以轻松地打印出来以找到真正的标签:

print('Actual:', y_test[index_to_predict])
y_test[index_to_predict]

will have the actual label and 将有实际的标签和

predictions[index_to_predict]

should have the predicted probability values for each of your classes. 应该具有您每个类别的预测概率值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM