简体   繁体   English

如何从 kaggle 的 MNIST 上训练的神经网络中获得正确的预测?

[英]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.

相关问题 如何从经过训练的随机森林模型中获得预测? - how to get prediction from trained random forest model? 如何从我的 Keras 神经网络一次做出一个预测 - How to make a single prediction at a time from my Keras' Neural Net 如何控制在 MNIST 中训练的 GAN 生成哪个数字? - How to control which digit is generated from a GAN trained in MNIST? RNN:在训练模型后从文本输入中获取预测 - RNN: Get prediction from a text input after the model is trained 如何从图像分类中的卷积神经网络中获取预测标签 - How get the predicted label from a Convolution Neural Net in image classification 如何使用Tensorflow 2.0构建基本的mnist神经网络? - How to build a basic mnist neural net using tensorflow 2.0? 根据神经网络的预测创建数据帧系列 - Create dataframe serie from the prediction of a neural network 可以从其权重和 Output 中获得经过训练的 U-Net(卷积神经网络)的输入吗? - Can the Input of a trained U-Net (Convolutional Neural Network) be obtained from its Weights and Output? 改善使用 mnist 数据集训练的神经网络的真实结果 - Improve real-life results of neural network trained with mnist dataset 从 Kaggle 数据集中获取 zip 文件的名称 - Get the name of the zip file from Kaggle dataset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM