Chandra Sangala
Chandra Sangala

Reputation: 11

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.

I don't know what to try to fix this issue.

'''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]

Upvotes: 1

Views: 67

Answers (1)

EuclidianHike
EuclidianHike

Reputation: 296

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.

If you want to see just the predicted class, as Chris A. said use predict_classes.

Upvotes: 1

Related Questions