📘 What is Keras?
Keras is a high-level deep learning API used to build and train neural networks with Python. It provides easy-to-use tools for creating layers, selecting activation functions, training models and making predictions.
In this example, we will use Keras to create a neural network that learns from simple student information and predicts whether a student is likely to Pass or Need Improvement.
🎓 Real-Life Problem
Suppose a teacher wants to estimate a student's academic performance using three pieces of information:
- 📚 Study Hours per day
- 🏫 Attendance percentage
- 📝 Previous Marks percentage
The neural network will learn the relationship between these inputs and the student's result.
↓
Neural Network
↓
Pass / Need Improvement
🔷 Three-Layer Neural Network
The model contains three trainable layers:
Input Layer
3 Features
Hidden Layer
8 Neurons
Output Layer
1 Neuron
📊 Sample Training Dataset
| Study Hours | Attendance % | Previous Marks % | Result |
|---|---|---|---|
| 1 | 55 | 40 | 0 |
| 2 | 60 | 45 | 0 |
| 3 | 70 | 55 | 1 |
| 4 | 75 | 60 | 1 |
| 5 | 85 | 75 | 1 |
Here 0 = Need Improvement and 1 = Pass.
⚙️ Step 1: Install TensorFlow
Open the terminal or command prompt and run:
pip install tensorflow numpy
💻 Step 2: Complete Python Code
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
# --------------------------------
# 1. Training Data
# --------------------------------
X = np.array([
[1, 55, 40],
[2, 60, 45],
[3, 70, 55],
[4, 75, 60],
[5, 85, 75],
[6, 90, 80],
[2, 65, 50],
[4, 80, 70]
], dtype=float)
# 0 = Need Improvement
# 1 = Pass
y = np.array([
0,
0,
1,
1,
1,
1,
0,
1
], dtype=float)
# --------------------------------
# 2. Create Three-Layer Model
# --------------------------------
model = keras.Sequential([
# Layer 1: Input + Hidden Layer
layers.Dense(
8,
activation="relu",
input_shape=(3,)
),
# Layer 2: Second Hidden Layer
layers.Dense(
4,
activation="relu"
),
# Layer 3: Output Layer
layers.Dense(
1,
activation="sigmoid"
)
])
# --------------------------------
# 3. Compile the Model
# --------------------------------
model.compile(
optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"]
)
# --------------------------------
# 4. Train the Model
# --------------------------------
model.fit(
X,
y,
epochs=300,
verbose=0
)
# --------------------------------
# 5. Evaluate the Model
# --------------------------------
loss, accuracy = model.evaluate(
X,
y,
verbose=0
)
print("Model Accuracy:", accuracy)
# --------------------------------
# 6. Test a New Student
# --------------------------------
new_student = np.array([
[5, 88, 78]
], dtype=float)
prediction = model.predict(
new_student,
verbose=0
)
probability = prediction[0][0]
print("Pass Probability:", probability)
if probability >= 0.5:
print("Prediction: PASS")
else:
print("Prediction: NEED IMPROVEMENT")
⚠️ Understanding the Three Layers
In Keras, the model above contains three Dense layers:
- First Dense Layer: 8 neurons with ReLU activation.
- Second Dense Layer: 4 neurons with ReLU activation.
- Third Dense Layer: 1 neuron with Sigmoid activation.
Therefore, this example is a three-Dense-layer neural network.
🔍 Step-by-Step Code Explanation
1️⃣ Import Libraries
import numpy as np from tensorflow import keras from tensorflow.keras import layers
NumPy is used for numerical data, while Keras is used to construct and train the neural network.
2️⃣ Prepare Input Data
X = [
[1,55,40],
[2,60,45],
[3,70,55]
]
Each row represents one student.
The three values represent:
- Study Hours
- Attendance
- Previous Marks
3️⃣ Create the First Layer
layers.Dense(8, activation="relu")
This layer contains 8 neurons. ReLU helps the network learn non-linear relationships.
4️⃣ Create the Second Layer
layers.Dense(4, activation="relu")
The second layer receives information from the previous layer and extracts more useful patterns.
5️⃣ Create the Output Layer
layers.Dense(1, activation="sigmoid")
The output layer contains one neuron. Sigmoid produces a value between 0 and 1, which can be interpreted as the estimated probability of the positive class.
⚡ Activation Functions
ReLU
ReLU is used in the hidden layers to introduce non-linearity.
Sigmoid
Sigmoid converts the final output into a value between 0 and 1.
📐 Basic Mathematics
Every neuron first calculates a weighted sum:
Then the activation function is applied.
During training, Keras adjusts the weights and biases so that the predicted result becomes closer to the actual result.
🏋️ How Training Works
This process is repeated for many epochs. The optimizer Adam changes the model's parameters to reduce the loss.
🔮 Predicting a New Student
new_student = np.array([
[5, 88, 78]
])
prediction = model.predict(new_student)
if prediction[0][0] >= 0.5:
print("PASS")
else:
print("NEED IMPROVEMENT")
The new student's information is:
- Study Hours = 5
- Attendance = 88%
- Previous Marks = 78%
The trained neural network calculates a probability. If the probability is at least 0.5, we classify the student as PASS.
🖥️ Example Output
Model Accuracy: 1.0 Pass Probability: 0.98 Prediction: PASS
Note: Neural-network results can vary slightly because the model starts with randomly initialized parameters and the dataset here is very small. This dataset is for educational demonstration, not for making real academic decisions.
📚 Important Keras Terms
| Term | Meaning |
|---|---|
| Sequential | Creates a model where layers are arranged sequentially. |
| Dense | A fully connected neural-network layer. |
| ReLU | Activation function used in the hidden layers. |
| Sigmoid | Activation function that produces values between 0 and 1. |
| Epoch | One complete training pass through the dataset. |
| Optimizer | Algorithm used to update model parameters during training. |
🎯 Learning Summary
- Collect input features.
- Prepare training data.
- Create the Keras Sequential model.
- Add the first Dense layer.
- Add the second Dense layer.
- Add the output Dense layer.
- Compile the model.
- Train the model using
fit(). - Evaluate the model using
evaluate(). - Predict the result of a new student using
predict().
Created by Bijan Krishna Paul
No comments:
Post a Comment