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:

import numpy as np
import tensorflow as tf

# Example data: hours studied, hours slept
X = np.array([
    [4, 6], [5, 7], [2, 8], [1, 3], [3, 4], [0, 5],
    [1, 1], [2, 4], [3, 5], [5, 5], [0, 4], [4, 4],
])

# Labels: 1 if passed, 0 if failed
y = np.array([[1], [1], [1], [0], [0], [0], [0], [0], [1], [1], [0], [1]])

# Define the model with 2 inputs and 1 output
model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(2,)), 
    tf.keras.layers.Dense(5, activation='relu'), 
    tf.keras.layers.Dense(1, activation='sigmoid') 
])

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train the model
model.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:

new_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():

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

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

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal