๐ What is Keras?
Keras is a high-level deep-learning API that provides a simple way to build, train, evaluate and use neural networks with Python.
Keras provides ready-to-use components such as Dense layers, activation functions, optimizers, loss functions and training methods.
In this example, we will use Keras to create a neural network that predicts a student's expected examination marks using information such as study time, attendance, previous marks and assignment performance.
๐ Real-Life Example: Student Marks Prediction
Suppose a teacher wants to estimate the expected marks of a student. The following four features are available:
- ๐ Study Hours per day
- ๐ซ Attendance percentage
- ๐ Previous Marks percentage
- ๐ Assignment Score percentage
The neural network learns a relationship between these four inputs and the student's expected examination marks.
↓
Neural Network
↓
Predicted Examination Marks
๐ท Four-Layer Neural Network Architecture
The model contains four trainable Dense layers. The input consists of four features.
4 Features
16 Neurons
ReLU
12 Neurons
ReLU
8 Neurons
ReLU
1 Neuron
Linear
๐ Sample Training Dataset
The following is a small educational dataset. The target value is the student's examination mark.
| Study Hours | Attendance % | Previous Marks % | Assignment % | Exam Marks |
|---|---|---|---|---|
| 2 | 60 | 45 | 50 | 48 |
| 3 | 65 | 50 | 55 | 55 |
| 4 | 70 | 60 | 65 | 65 |
| 5 | 75 | 68 | 70 | 72 |
| 6 | 85 | 78 | 80 | 82 |
| 7 | 90 | 85 | 88 | 90 |
Note: This small dataset is intended to demonstrate the Keras workflow. A real prediction system would require a much larger, representative dataset and appropriate validation.
⚙️ Step 1: Install Required Libraries
Install TensorFlow and NumPy using:
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
# ---------------------------------------
# Columns:
# 1 = Study Hours
# 2 = Attendance
# 3 = Previous Marks
# 4 = Assignment Score
X = np.array([
[2, 60, 45, 50],
[3, 65, 50, 55],
[4, 70, 60, 65],
[5, 75, 68, 70],
[6, 85, 78, 80],
[7, 90, 85, 88],
[3, 68, 52, 58],
[5, 80, 72, 75],
[6, 88, 82, 85],
[4, 72, 62, 68]
], dtype=float)
# Target: Examination Marks
y = np.array([
48,
55,
65,
72,
82,
90,
57,
76,
86,
67
], dtype=float)
# ---------------------------------------
# 2. Create Four-Layer Neural Network
# ---------------------------------------
model = keras.Sequential([
# Layer 1
layers.Dense(
16,
activation="relu",
input_shape=(4,)
),
# Layer 2
layers.Dense(
12,
activation="relu"
),
# Layer 3
layers.Dense(
8,
activation="relu"
),
# Layer 4 - Output
layers.Dense(
1,
activation="linear"
)
])
# ---------------------------------------
# 3. Compile the Model
# ---------------------------------------
model.compile(
optimizer="adam",
loss="mean_squared_error",
metrics=["mae"]
)
# ---------------------------------------
# 4. Display Model Architecture
# ---------------------------------------
model.summary()
# ---------------------------------------
# 5. Train the Model
# ---------------------------------------
model.fit(
X,
y,
epochs=500,
verbose=0
)
# ---------------------------------------
# 6. Evaluate the Model
# ---------------------------------------
loss, mae = model.evaluate(
X,
y,
verbose=0
)
print("Mean Absolute Error:", mae)
# ---------------------------------------
# 7. Test a New Student
# ---------------------------------------
new_student = np.array([
[5.5, 82, 76, 80]
], dtype=float)
# ---------------------------------------
# 8. Predict Examination Marks
# ---------------------------------------
prediction = model.predict(
new_student,
verbose=0
)
print(
"Predicted Examination Marks:",
prediction[0][0]
)
๐ Description of Each Layer
1️⃣ Layer 1 – 16 Neurons
layers.Dense(16, activation="relu", input_shape=(4,))
This is the first Dense layer. It receives four input features:
- Study Hours
- Attendance
- Previous Marks
- Assignment Score
It contains 16 neurons and uses the ReLU activation function.
2️⃣ Layer 2 – 12 Neurons
layers.Dense(12, activation="relu")
The second layer receives the features learned by the first layer. It contains 12 neurons.
This layer allows the network to learn more complex relationships between the student's characteristics.
3️⃣ Layer 3 – 8 Neurons
layers.Dense(8, activation="relu")
The third layer further processes the information received from Layer 2 and learns higher-level patterns.
4️⃣ Layer 4 – Output Layer
layers.Dense(1, activation="linear")
The final layer contains one neuron because the model needs to produce one numerical prediction: expected examination marks.
A linear output is appropriate for this simple regression example.
๐ Understanding Sequential Model
model = keras.Sequential([
layers.Dense(16, activation="relu", input_shape=(4,)),
layers.Dense(12, activation="relu"),
layers.Dense(8, activation="relu"),
layers.Dense(1, activation="linear")
])
Sequential means that the layers are connected in a simple sequence. The output of one layer becomes the input to the next layer.
⚙️ Understanding model.compile()
model.compile(
optimizer="adam",
loss="mean_squared_error",
metrics=["mae"]
)
๐น Optimizer – Adam
Adam controls how the model's weights are updated during training.
๐น Loss – Mean Squared Error
Here y is the actual mark and ลท is the predicted mark.
๐น Metric – MAE
MAE represents the average absolute difference between actual and predicted values.
๐ How the Four-Layer Model Learns
After producing a prediction, the model calculates the loss by comparing the predicted mark with the actual mark. The optimizer then updates the weights. This process is repeated over many epochs.
๐ฎ Predicting a New Student's Marks
new_student = np.array([
[5.5, 82, 76, 80]
])
prediction = model.predict(new_student)
print(
"Predicted Examination Marks:",
prediction[0][0]
)
The new student's information is:
- ๐ Study Hours = 5.5 hours/day
- ๐ซ Attendance = 82%
- ๐ Previous Marks = 76%
- ๐ Assignment Score = 80%
The four values are passed through the four-layer neural network, and the final neuron produces the predicted examination mark.
๐ฅ️ Example Output
Model: "sequential" Layer (type) Output Shape ----------------------------------------- Dense (None, 16) Dense (None, 12) Dense (None, 8) Dense (None, 1) Mean Absolute Error: 1.5 Predicted Examination Marks: 78.6
The exact output can differ because neural networks generally start with randomly initialized weights and this example uses a very small dataset.
⚡ Why ReLU and Linear Activation?
ReLU
ReLU is used in the hidden layers because it introduces non-linearity and helps the network learn complex patterns.
Linear
A linear output is useful for regression tasks where the predicted value is a continuous number such as marks, temperature or price.
๐ 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. |
| Linear | Activation used here for continuous numerical prediction. |
| Epoch | One complete pass through the training dataset. |
| Loss | A measure of how different the prediction is from the actual value. |
| Optimizer | Updates model parameters to reduce the loss. |
๐ฏ Complete Learning Flow
↓
4 Input Features
↓
16-Neuron Layer
↓
12-Neuron Layer
↓
8-Neuron Layer
↓
1-Neuron Output Layer
↓
Predicted Examination Marks
Key Points
- Keras makes neural-network development easier.
- A Dense layer contains interconnected neurons.
- The first three layers use ReLU activation.
- The final layer uses linear activation for regression.
- Adam is used as the optimizer.
- Mean Squared Error is used as the loss function.
- MAE is used to measure prediction error.
fit()trains the model.evaluate()measures model performance.predict()generates predictions for new data.
Created by Bijan Krishna Paul
No comments:
Post a Comment