pythontensorflowmachine-learningkerassoftmax

How to make a prediction using karas TensorFlow?


Ive coded this machine learning algoritm but it retured to me a wierd array. I want to input 2 numbers and then those numbers be clasified into similar results found in Y, How do I make a prediction using this model?

import numpy as np # mutivariate clasification
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense


X =np.array(
[[3, 7],
 [3, 6],
 [3, 7.2],
 [6, 8],
 [7, 7.5],
 [7.9, 7.5]])

Y =np.array([1, 1, 1, 2, 3, 3])

model = Sequential([
    Dense(units = 25, activation = "relu"),
    Dense(units = 15, activation = "relu"),
    Dense(units = 10, activation = "softmax"),])

from keras.losses import SparseCategoricalCrossentropy
model.compile(loss = SparseCategoricalCrossentropy())
model.fit(X, Y, epochs = 100)

I tried this code:

Xpred = [[3,7.8]]
prediction = model.predict(Xpred, verbose = 1)
print(prediction)

and it returned:

[[3.4789115e-02 8.4235787e-01 7.6775238e-02 1.9370530e-02 1.0821970e-02
  4.8491983e-03 4.7121649e-03 7.4993627e-04 2.9366722e-04 5.2804169e-03]]

Im new to stack and ML so please let me know how I could improve or if you have any reading materal or resources for ML you could share!


Solution

  • There's a lot to understand here here and I suggest that you work through some more tutorials on classification and follow the steps closely (keras documentation is quite good for this), but I'll attempt to talk you through enough to understand what you're seeing and get your basic example working.

    The array of floating point numbers you get at the end is an array of probabilities for each class. There are 10 probabilities because you set the number of units in the output layer to 10, even though you only have 3 classes in your data. I'm guessing that you just want to get a classification for your new set of features ([3, 7.8]), so you take the highest probability. In this case you can see just from inspection that the predicted class is 1 because the highest probability is 8.4235787e-01 which is in the 1st position, but in general you can get this using np.argmax on a numpy array.

    Steps to get your code working the way you expect:

    The code ends up looking like this:

    import numpy as np # mutivariate clasification
    import tensorflow as tf
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    
    
    X =np.array(
    [[3, 7],
    [3, 6],
    [3, 7.2],
    [6, 8],
    [7, 7.5],
    [7.9, 7.5]])
    
    Y =np.array([0, 0, 0, 1, 2, 2])
    
    model = Sequential([
        Dense(units = 25, activation = "relu"),
        Dense(units = 15, activation = "relu"),
        Dense(units = 3, activation = "softmax")
    ])
    
    from keras.losses import SparseCategoricalCrossentropy
    model.compile(loss = SparseCategoricalCrossentropy())
    model.fit(X, Y, epochs = 100)
    
    for prediction in model.predict([[3, 7.8]]):
        print(prediction)
        print(np.argmax(prediction))
    

    The final part of the output is:

    [0.916569   0.07700075 0.00643022]
    0
    

    So the predicted class is 0 (or 1 based on the original data you posted), which is what we'd expect based on inspection of the training data and new data.