Total Pageviews

Thursday, September 10, 2026

๐Ÿง  How Keras Works Simple Neural Network Example using Python Hours Studied → Neural Network → Pass / Fail

๐Ÿง  How Keras Works

Simple Neural Network Example using Python

Hours Studied → Neural Network → Pass / Fail

๐Ÿ“Œ What is Keras?

Keras is a high-level deep-learning API used to create and train neural networks with Python. It provides simple building blocks such as layers, activation functions, optimizers and loss functions.

Instead of manually programming every mathematical operation of a neural network, we can describe the network using Keras and allow the framework to perform the training process automatically.

Data → Neural Network → Prediction → Loss → Weight Update → Better Prediction

๐ŸŒฑ Step 1 — Simple Dataset

Suppose we want to predict whether a student will pass based on the number of hours studied.

Hours Studied Result Numerical Target
1 Fail 0
2 Fail 0
3 Fail 0
4 Pass 1
5 Pass 1
6 Pass 1
7 Pass 1
8 Pass 1

Here 0 = Fail and 1 = Pass.

๐Ÿง  Step 2 — Build the Neural Network

INPUT

๐Ÿ“š Hours Studied
x = 6
→
HIDDEN LAYER

● Neuron 1
● Neuron 2
● Neuron 3
● ...
● Neuron 8
→
OUTPUT

Probability
0 → Fail
1 → Pass

๐Ÿ’ป Complete Keras Python Program


# =========================================================
# SIMPLE KERAS NEURAL NETWORK
# HOURS STUDIED -> PASS / FAIL
# =========================================================

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# ---------------------------------------------------------
# 1. Create Simple Dataset
# ---------------------------------------------------------

X = np.array([
    [1],
    [2],
    [3],
    [4],
    [5],
    [6],
    [7],
    [8]
], dtype=float)

# 0 = Fail
# 1 = Pass

y = np.array([
    0,
    0,
    0,
    1,
    1,
    1,
    1,
    1
], dtype=float)

# ---------------------------------------------------------
# 2. Create Neural Network
# ---------------------------------------------------------

model = keras.Sequential([

    layers.Dense(
        8,
        activation="relu",
        input_shape=(1,)
    ),

    layers.Dense(
        1,
        activation="sigmoid"
    )
])

# ---------------------------------------------------------
# 3. Compile Model
# ---------------------------------------------------------

model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)

# ---------------------------------------------------------
# 4. Display Model Architecture
# ---------------------------------------------------------

model.summary()

# ---------------------------------------------------------
# 5. Train Model
# ---------------------------------------------------------

history = model.fit(
    X,
    y,
    epochs=100,
    verbose=0
)

print("\nTraining completed!")

# ---------------------------------------------------------
# 6. Evaluate Training Data
# ---------------------------------------------------------

loss, accuracy = model.evaluate(
    X,
    y,
    verbose=0
)

print("\nLoss:", round(loss, 4))
print("Accuracy:", round(accuracy * 100, 2), "%")

# ---------------------------------------------------------
# 7. Predict New Students
# ---------------------------------------------------------

new_students = np.array([
    [2],
    [4],
    [6],
    [8]
], dtype=float)

probabilities = model.predict(
    new_students,
    verbose=0
)

# ---------------------------------------------------------
# 8. Convert Probability to Class
# ---------------------------------------------------------

for hours, probability in zip(
    new_students.flatten(),
    probabilities.flatten()
):

    if probability >= 0.5:
        result = "PASS"
    else:
        result = "FAIL"

    print(
        "Hours:", int(hours),
        "| Probability:",
        round(float(probability), 3),
        "| Prediction:",
        result
    )

# ---------------------------------------------------------
# 9. Plot Training Loss
# ---------------------------------------------------------

import matplotlib.pyplot as plt

plt.figure(figsize=(8, 5))

plt.plot(
    history.history["loss"]
)

plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.title("Keras Training Loss")

plt.grid(True)
plt.show()

# =========================================================
# END
# =========================================================

๐Ÿ”— Step 3 — Understanding Sequential()

The following statement creates a neural network in which layers are arranged one after another:

model = keras.Sequential([...])

Our model contains two layers:

Layer Neurons Activation Purpose
Hidden Layer 8 ReLU Learn useful patterns
Output Layer 1 Sigmoid Produce Pass probability

๐Ÿ“ Step 4 — Mathematics Inside a Neuron

A neuron receives an input, multiplies it by a weight, adds a bias and then passes the result through an activation function.

z = wx + b

Where:

  • x = input value
  • w = weight
  • b = bias
  • z = weighted sum

ReLU Activation

ReLU(z) = max(0, z)

Sigmoid Activation

ฯƒ(z) = 1 / (1 + e-z)

The sigmoid function produces a value between 0 and 1, making it convenient for binary classification.

⚙️ Step 5 — Compile the Model


model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)
⚙️ Optimizer

Adam adjusts the model's weights during training to reduce the loss.

๐Ÿ“‰ Loss Function

Binary cross-entropy measures the error for a two-class prediction problem.

๐Ÿ“Š Accuracy

Accuracy tells us the proportion of predictions that are classified correctly.

๐Ÿ”„ Step 6 — How Training Happens

Input Data
Hours Studied
↓
Forward Propagation
Calculate prediction
↓
Loss Calculation
Compare prediction with actual answer
↓
Backpropagation
Calculate how weights contributed to the error
↓
Optimizer
Update weights and biases
↓
Repeat
Continue for many epochs

๐Ÿ” What is an Epoch?

An epoch means one complete pass through the training dataset.

In our program:

epochs = 100

This means the model processes the eight training examples repeatedly for 100 complete training cycles.

Epoch 1 → High Error Epoch 20 → Lower Error Epoch 50 → Better Epoch 100 → Trained Model

๐ŸŽฏ Step 7 — Prediction

After training, suppose we give the model:

2 Hours
Probability → low
FAIL
6 Hours
Probability → high
PASS
8 Hours
Probability → high
PASS

If the output probability is 0.5 or greater, our example interprets the prediction as PASS; otherwise it interprets it as FAIL.

๐Ÿ“‰ Understanding Loss

Loss tells the neural network how far its prediction is from the expected answer. During training, Keras attempts to reduce this value.

Higher Loss → Larger Prediction Error
↓
Optimizer Updates Weights
↓
Lower Loss → Better Predictions

๐ŸŽ“ Complete Keras Workflow

Create Dataset
↓
Define Neural Network
↓
Compile Model
↓
Train with fit()
↓
Calculate Loss
↓
Update Weights
↓
Repeat for Epochs
↓
Evaluate Model
↓
Predict New Data

Remember: Keras provides the high-level tools, while the underlying TensorFlow system performs the numerical computations needed to train the neural network.

⭐ Four Important Keras Commands

Command Purpose
keras.Sequential() Builds a sequence of neural-network layers.
model.compile() Defines optimizer, loss function and evaluation metrics.
model.fit() Trains the neural network.
model.predict() Generates predictions for new observations.
๐ŸŒ Created by Bijan Krishna Paul
Python • Keras • TensorFlow • Neural Networks
Simple educational example for understanding Keras

No comments:

Post a Comment