Lesson 4
From Training to Prediction: TensorFlow Models for Decision Making
Introduction

Congratulations on successfully building and training your Neural Network model in TensorFlow. Now comes the exciting part—using your trained model to make predictions. In this lesson, we will guide you through the process of creating new inputs for your model, making predictions using the predict() method, and interpreting the model's output. By the end of this lesson, you'll have the skills to apply your model to real-world data and make insightful predictions. Let's dive in and see how we can use our model to predict whether a student will pass or fail based on their study and sleep hours!

Quick Refresh on Model Training

To ensure we're on the same page, let's quickly recap the model we built and trained in previous lessons:

Python
1import numpy as np 2import tensorflow as tf 3 4# Example data: hours studied, hours slept 5X = np.array([ 6 [4, 6], [5, 7], [2, 8], [1, 3], [3, 4], [0, 5], 7 [1, 1], [2, 4], [3, 5], [5, 5], [0, 4], [4, 4], 8]) 9 10# Labels: 1 if passed, 0 if failed 11y = np.array([[1], [1], [1], [0], [0], [0], [0], [0], [1], [1], [0], [1]]) 12 13# Define the model with 2 inputs and 1 output 14model = tf.keras.Sequential([ 15 tf.keras.layers.Input(shape=(2,)), 16 tf.keras.layers.Dense(5, activation='relu'), 17 tf.keras.layers.Dense(1, activation='sigmoid') 18]) 19 20# Compile the model 21model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) 22 23# Train the model 24model.fit(X, y, epochs=10)

In this example, we defined a neural network model that takes in two features—hours studied and hours slept—and outputs a probability for passing or failing. We compiled the model using the Adam optimizer, binary cross-entropy as the loss function, and accuracy as a metric. Finally training the model for 10 epochs.

Creating New Inputs for our Model

When we have successfully trained a machine learning model, our ultimate goal is usually to make predictions or decisions based on new, unseen data. In our student pass/fail prediction scenario, suppose we have a new student who has studied for 3 hours and slept for 6 hours on the day before an exam. We would like to use our model to predict whether this student will pass or fail.

To do this, we must create a new input in the same format that our model has been trained on. You'll recall that our model was trained on arrays with the structure [hours studied, hours slept]. Here's how we can create a new input for our model using numpy:

Python
1new_input = np.array([[3, 6]])

In this code, np.array() is a function from the numpy library that creates an array using the input data provided. The input is [3, 6], corresponding to the two features (hours studied and hours slept) that our model requires to make a prediction.

Alternatively, the input could also be a tensor, created using TensorFlow's tf.constant():

Python
1new_input = tf.constant([[3, 6]])

Both approaches will correctly format the new input data needed for prediction.

Predicting Outcomes with New Inputs

Now that we have a new input for our model, we can use it to make a prediction. For this, we use the predict() method provided by TensorFlow:

Python
1prediction = model.predict(new_input) 2print(prediction)

The output of the above code will be:

1[[0.75]]

When run, the predict() method applies the model to our new input and returns a prediction. It's important to note that the prediction is in the form of a probability output by the model's final sigmoid activation function. This probability gives us insight into the model's certainty about its prediction, where a value closer to 0 indicates a stronger prediction of the 'fail' class and a value closer to 1 indicates a stronger prediction of the 'pass' class.

Interpreting the Model's Output

In our binary classification problem, the model's output is a probability that the observed example belongs to class 1, i.e., the class for 'pass'. The closer the output value is to 1, the more certain the model is that the example belongs to this class. Conversely, the closer the output value is to 0, the more certain the model is that the example does not belong to class 1, implying it belongs to the other class (class 0, for 'fail').

In practice, a threshold of 0.5 is often used to assign the class label. If the model's output is greater than or equal to 0.5, we assign the example to class 1, and if it's less than 0.5, we assign it to class 0. This can be expressed with the following formula:

Class label=1if  probability0.50if  probability<0.5\text{Class label} = \begin{array}{cc} 1 & \text{if} \; \text{probability} \geq 0.5 \\ 0 & \text{if} \; \text{probability} < 0.5 \end{array}

However, this threshold can be adjusted depending on the nature of the problem and how important it is to correctly identify positive cases (e.g., students passing) versus correctly identifying negative cases (e.g., students failing).

Converting Predicted Probabilities into Labels

Finally, we want to convert our raw model output into a class label so that we get a clear prediction: 'pass' or 'fail'. As mentioned above, this is done by applying a threshold to the outputted probabilities. Here, we use a threshold of 0.5 (since we're dealing with a binary classification task):

Python
1print("Prediction (0=fail, 1=pass):", (prediction > 0.5).astype(int)[0][0])

The output will be:

1Prediction (0=fail, 1=pass): 1

This output demonstrates that the model predicts the student will pass, as the predicted probability of passing (0.75) is greater than the 0.5 threshold. This simplistic example illustrates how a model's probability output is translated into a more understandable class label.

Lesson Summary

Great job! You now know how to take a trained TensorFlow model and use it to make predictions for new, unseen inputs. You've learned how to format new data in a way that aligns with your model's requirements, use your model to generate probability predictions, interpret those predictions in the context of your problem, and convert those probabilities into clear class labels.

In the next set of exercises, you'll practice these steps to reinforce your understanding. Remember, applying your machine learning model to make helpful predictions is the final goal of most machine learning tasks, and your newly enhanced skills will be directly applicable to a variety of real-world prediction problems. Keep practising and keep learning - you're doing great!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.