[英]How to get a proper prediction from an neural net trained on MNIST from kaggle?
I have trained a neural net on the MNIST dataset from kaggle.I am having trouble with getting the neural net to predict the number which it is receiving.我已经在 kaggle 的 MNIST 数据集上训练了一个神经网络。我在让神经网络预测它接收到的数字时遇到了麻烦。
I don't know what to try to fix this issue.我不知道如何尝试解决这个问题。
'''python '''Python
import pandas as pd
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
mnist=pd.read_csv(r"C:\Users\Chandrasang\python projects\digit-recognizer\train.csv").values
xtest=pd.read_csv(r"C:\Users\Chandrasang\python projects\digit-recognizer\test.csv").values
ytrain=mnist[:,0]
xtrain=mnist[:,1:]
x_train=keras.utils.normalize(xtrain,axis=1)
x_test=keras.utils.normalize(xtest,axis=1)
x=0
xtrain2=[]
while True:
d=x_train[x]
d.shape=(28,28)
xtrain2.append(d)
x+=1
if x==42000:
break
y=0
xtest2=[]
while True:
b=x_test[y]
b.shape=(28,28)
xtest2.append(b)
y+=1
if y==28000:
break
train=np.array(xtrain2,dtype=np.float32)
test=np.array(xtest2,dtype=np.float32)
model=keras.models.Sequential()
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(256,activation=keras.activations.relu))
model.add(keras.layers.Dense(256,activation=keras.activations.relu))
model.add(keras.layers.Dense(10,activation=keras.activations.softmax))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train,ytrain,epochs=10)
ans=model.predict(x_test)
print(ans[3])
''' '''
I expect the output to be a Whole number instead it gives me the following array:我希望输出是一个整数,而不是它给我以下数组:
[2.7538205e-02 1.0337318e-11 2.9973364e-03 5.7095995e-06 1.6916725e-07 6.9060135e-08 1.3406207e-09 1.1861910e-06 1.4758119e-06 9.6945578e-01] [2.7538205e-02 1.0337318e-11 2.9973364e-03 5.7095995e-06 1.6916725e-07 6.9060135e-08 1.3406207e-6e-6e-1095e-03 5.7095995e-06
Your output is normal, it is a vector of probabilities.你的输出是正常的,它是一个概率向量。 You have 10 classes (digits from 0 to 9) and your network compute the probability of your image to be in each class.Looking at your results, your network classified your input as a 9, with a probability of roughly 0.96.
您有 10 个类别(从 0 到 9 的数字),您的网络计算您的图像出现在每个类别中的概率。查看您的结果,您的网络将您的输入分类为 9,概率大约为 0.96。
If you want to see just the predicted class, as Chris A. said use predict_classes
.如果您只想查看预测的类,如 Chris A. 所说,请使用
predict_classes
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.