# Model.predict() output

**URL:** <https://forum.freecodecamp.org/t/model-predict-output/470349>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [July 22, 2021, 1:55am UTC](https://forum.freecodecamp.org/t/model-predict-output/470349 "2021-07-22T01:55:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![solgeller3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/solgeller3/32/183391_2.png) [@solgeller3](https://forum.freecodecamp.org/u/solgeller3)\
**Post date:** [July 22, 2021, 1:55am UTC](https://forum.freecodecamp.org/t/model-predict-output/470349/1 "2021-07-22T01:55:53Z")

</div>

Hey,  
So I have this model which is pretrained by Google and also trained on a tfds dataset:

```auto
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_datasets as tfds

keras = tf.keras

# split the data manually into 80% training, 10% testing, 10% validation
(raw_train, raw_validation, raw_test), metadata = tfds.load(
    'cats_vs_dogs',
    split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
    with_info=True,
    as_supervised=True,
)

"""""
Since the sizes of our images are all different, we need to convert them all to the same size. 
We can create a function that will do that for us below.
"""""

IMG_SIZE = 160 # All images will be resized to 160x160

def format_example(img, lab):
    """
    returns an image that is reshaped to IMG_SIZE
    """
    img = tf.cast(img, tf.float32)
    img = (img / 127.5) - 1
    img = tf.image.resize(img, (IMG_SIZE, IMG_SIZE))
    img = tf.expand_dims(img, axis=0)
    return img, lab

# Now we can apply this function to all our images using .map().
# .map() takes every single example in raw_train, raw_validation, etc and applies the function to it
train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
test = raw_test.map(format_example)

BATCH_SIZE = 32
SHUFFLE_BUFFER_SIZE = 1000

# Finally we will shuffle and batch the images
train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
validation_batches = validation.batch(BATCH_SIZE)
test_batches = test.batch(BATCH_SIZE)

IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)

# Create the base model from the pre-trained model MobileNet V2
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                               include_top=False, # we don't want to load the top layer
                                               weights='imagenet')

base_model.trainable = False

"""""
Adding OUR Classifier

"""""
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()

# Finally, we will add the prediction layer that will be a single dense neuron. We can do this because we
# only have two classes to predict for.
prediction_layer = keras.layers.Dense(1, activation='sigmoid')

# Now we will combine these layers together in a model.
model = tf.keras.Sequential([
    base_model,
    global_average_layer,
    prediction_layer
])

# TRAINING THE MODEL
"""""
Now we will train and compile the model. We will use a very small learning rate to ensure that the model 
does not have any major changes made to it.
"""""
base_learning_rate = 0.0001
model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=base_learning_rate),
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'])

# Now we can train it on our images
initial_epochs = 10
history = model.fit(train_batches,
                    epochs=initial_epochs,
                    validation_data=validation_batches)

 acc = history.history['accuracy']
 print(acc)

# you can save a model and load a model with this syntax so you don't have to train it again every time
model.save("dogs_vs_cats.h5") # we can save the model and reload it at anytime in the future
new_model = tf.keras.models.load_model('dogs_vs_cats.h5')
predictions = new_model.predict(test)
print(predictions)

get_label_name = metadata.features['label'].int2str # creates a function object that we can use to get labels

i = 0
for image, label in raw_train.take(6):
    plt.figure()
    plt.imshow(image)
    plt.title(get_label_name(label))
    print(get_label_name(label))
    print(predictions[i])
    i += 1

```

Anyway, I am getting this output and I don’t know how to use it or understand it:

```auto
dog
[0.33223754]
dog
[0.48130825]
dog
[0.70762724]
cat
[0.26925486]
dog
[0.49678028]
dog
[0.45581067]

```

In the code, I print the label name and the prediction beneath it, but i dont understand what the number inside the array means and how to use it. I would apreciate it if someone could explain.  
Thamk you!

---

<div class="post-metadata">

**Author:** ![AndyG](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andyg/32/232831_2.png) [@AndyG](https://forum.freecodecamp.org/u/AndyG)\
**Post date:** [July 22, 2021, 3:56am UTC](https://forum.freecodecamp.org/t/model-predict-output/470349/2 "2021-07-22T03:56:30Z")

</div>

Hello @solgeller3  
This is the probability of every single output described in the model.  
In many classification models you have a threshold. The common threshold is 0.5, if the result is _above_ of this, then is more likely to be “true”. On the contrary if the result is _below_ is more likely to be false.  
The more easy to understand example of this mechanism is the logistic function.

> 1/1+e^-z

In the specific context of your model, you are making a classification model with images,  
and after 10 iterations and at a base learning rate of 0.0001 only the third element,

> [@solgeller3](#):
>
> ```auto
> dog
> [0.70762724]
> 
> ```

has a strong probability of being classified correctly.  
To optimize the performance of your model:

1. You can adjust the learning rate. For correctly doing that you must be aware of terms like Gradient descent, Loss Function and Hyper parameter(The learning rate is a Hyperparameter).
2. You can change the number of iterations, since a more number of iterations could make a prediction more accurate.  
However there is always a risk of over-fitting , this is called bias-variance tradeoff.

But…, In simple words, these are the probabilities that your model (at a given learning rate, iterations, and parameters) will classify correctly every one of your images.  
If you want more information about classification threshold:

1. The basic concept of threshold  
[طبقه بندی: آستانه &nbsp;|&nbsp; Machine Learning &nbsp;|&nbsp; Google for Developers](https://developers.google.com/machine-learning/crash-course/classification/thresholding)
2. Additional exercises on keras for having fun  
[https://keras.io/api/metrics/classification\_metrics/](https://keras.io/api/metrics/classification_metrics/)  
Hope this helps.

---

<div class="post-metadata">

**Author:** ![AndyG](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andyg/32/232831_2.png) [@AndyG](https://forum.freecodecamp.org/u/AndyG)\
**Post date:** [July 22, 2021, 4:03am UTC](https://forum.freecodecamp.org/t/model-predict-output/470349/3 "2021-07-22T04:03:40Z")

</div>

> [@solgeller3](#):
>
> `prediction_layer = keras.layers.Dense(1, activation='sigmoid')`

I forgot it, even in this model you are using the sigmoid function to make a prediction.  
The logistic function is part of the family of sigmoid function.  
[![](https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Sigmoid-function-2.svg/1280px-Sigmoid-function-2.svg.png) ](https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Sigmoid-function-2.svg/1280px-Sigmoid-function-2.svg.png)  
So a more geometrical explanation would be, the threshold is 0.5 and you can locate every individual output value within the plane:

> [@solgeller3](#):
>
> ```auto
> dog
> [0.33223754]
> dog
> [0.48130825]
> dog
> [0.70762724]
> cat
> [0.26925486]
> dog
> [0.49678028]
> dog
> [0.45581067]
> 
> ```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [January 20, 2022, 4:04pm UTC](https://forum.freecodecamp.org/t/model-predict-output/470349/4 "2022-01-20T16:04:20Z")

</div>

Closed on January 20, 2022, 4:04pm UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
