Learn With Champak

Pass or Fail Prediction with TensorFlow

Build, train and test a simple binary-classification neural network using Python, TensorFlow, Keras, NumPy and Matplotlib.

What are we building?

In this project, we train a neural network to predict whether a student will pass or fail from the marks obtained.

Marks below 40 → Fail
Marks 40 or above → Pass

Concepts covered

  • Creating training data with NumPy
  • Normalizing marks between 0 and 1
  • Representing Fail as 0 and Pass as 1
  • Creating a TensorFlow functional model
  • Using a Dense neural-network layer
  • Using the sigmoid activation function
  • Using binary cross-entropy as the loss function
  • Training the model using Adam
  • Generating predictions for new marks
  • Applying a decision threshold of 0.5
  • Plotting the learned prediction curve

Run the complete Python program

The complete program is loaded in the editor below. Run it, examine the predictions and modify the marks to test the trained model.

Browser Version

Try the TensorFlow.js Model

The same Pass or Fail neural network has also been created using TensorFlow.js. It trains and runs directly inside the web browser without requiring Python or a separate server.

Wait for the model to finish training. Enter marks between 0 and 100, then select Predict Pass or Fail.

Open TensorFlow.js Project in a New Tab

How the model works

1. Training data

The input array contains marks. The output array contains the correct classification for each mark: 0 represents Fail and 1 represents Pass.

2. Normalization

Each mark is divided by 100. Therefore, 10 becomes 0.10, 40 becomes 0.40 and 90 becomes 0.90. The meaning remains unchanged; only the numeric scale changes.

3. Neural-network layer

The model has one input and one output. Its Dense layer learns the relationship between a student’s marks and the corresponding result.

4. Sigmoid activation

Sigmoid produces a value between 0 and 1. A value closer to 0 indicates Fail, while a value closer to 1 indicates Pass.

Prediction below 0.5 → Fail
Prediction equal to or above 0.5 → Pass

Try these experiments

  1. Add more marks to the training data.
  2. Predict the result for marks close to 40.
  3. Change the learning rate and retrain the model.
  4. Reduce the number of epochs and compare the predictions.
  5. Study how the prediction curve changes after training.

Learn With Champak

Python, TensorFlow, Machine Learning and Programming
Programmer’s Picnic | Champak Roy