
Training a Neural Network
Prerequisites for Understanding This Blog:Blog_1
So, now let's start more interesting topic Neural Math...
Great! we have a small dataset with 4 individuals and 3 features: Weight, Height, and Gender.
Here's the data in a clean table format:
📐 A Simple Example
Suppose we have a model that predicts someone’s weight based on their height.
Name | Weight (lb) | Height (in) | Gender |
---|---|---|---|
Alice | 133 | 65 | F |
Bob | 160 | 72 | M |
Charlie | 152 | 70 | M |
Diana | 120 | 60 | F |
Converting Gender to Numeric (for ML)
import pandas as pd
# Data
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Weight': [133, 160, 152, 120],
'Height': [65, 72, 70, 60],
'Gender': ['F', 'M', 'M', 'F']
}
df = pd.DataFrame(data)
# Encode gender
df['Gender_encoded'] = df['Gender'].map({'F': 0, 'M': 1})
print(df)
Output will be:
Name | Weight | Height | Gender | Gender_encoded |
---|---|---|---|---|
Alice | 133 | 65 | F | 0 |
Bob | 160 | 72 | M | 1 |
Charlie | 152 | 70 | M | 1 |
Diana | 120 | 60 | F | 0 |
Preparing the Data
Before feeding data into a neural network, we need to prepare it properly. This includes:
- Separating the input features (X) and the target variable (y)
- Normalizing the input features so that they are on a similar scale
Splitting Features and Target
In our dataset, we are using Weight
and Height
to predict Gender_encoded
.
X = df[['Weight', 'Height']].values # Input features
y = df['Gender_encoded'].values # Target variable (0 for Female, 1 for Male)
Understanding the Shapes of X
and y
-
X
will be a 2D array with shape(4, 2)
→ This means 4 samples and 2 features (Weight and Height) -
y
will be a 1D array with shape(4,)
→ This contains the binary target values for Gender:[0, 1, 1, 0]
Why Normalization is Important
Neural networks perform better and converge faster when all input features are on a similar scale.
In our dataset:
Weight
values range from 120 to 160Height
values range from 60 to 72
Since weight values are numerically larger than height values, they could dominate the learning process. This can:
- Slow down training
- Cause instability in gradient descent
- Lead to a biased model
✅ Solution: Normalize the input features so that both Weight and Height contribute equally to training.
Applying Standardization (Z-Score Scaling)
We’ll use StandardScaler from scikit-learn to standardize the data using this formula:
z= (x−μ)/σ
Where:
x = original value
μ = mean of the feature
σ = standard deviation
This transforms all features to have:
Mean = 0
Standard deviation = 1
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
fit_transform()
calculates the mean and standard deviation from the data and then transforms it.X_scaled
is now the normalized version ofX
, ready to be used for training the neural network.
🎯 What is Loss in a Neural Network?
When a neural network makes predictions, it doesn’t always get the answer right. The loss function tells us how far off the prediction is from the actual value.
Think of it as:
“How bad was my guess?”
The smaller the loss, the better the model is performing.
📊 Why is Loss Important?
- It gives the network feedback on its performance.
- It helps adjust the weights through backpropagation.
- It's used during training to improve predictions over time.
📐 A Simple Example
Suppose we have a model that predicts someone’s weight based on their height.
Person | Actual Weight (lb) | Predicted Weight (lb) | Difference (Actual - Predicted) | Squared Error |
---|---|---|---|---|
Alice | 133 | 130 | 133 - 130 = 3 | 3² = 9 |
Bob | 160 | 158 | 160 - 158 = 2 | 2² = 4 |
Charlie | 152 | 149 | 152 - 149 = 3 | 3² = 9 |
Let's use Mean Squared Error (MSE) as our Loss Function:
MSE = (1/n) * Σ(y_actual - y_predicted)^2
🔢 Manual Calculation:
MSE = (1/3) * [(133 - 130)^2 + (160 - 158)^2 + (152 - 149)^2]
= (1/3) * [9 + 4 + 9]
= 22 / 3 ≈ 7.33
So, the loss is 7.33 — this value will be used to update the model's weights.
import numpy as np
from sklearn.metrics import mean_squared_error
# Actual and predicted weights
y_actual = np.array([133, 160, 152])
y_pred = np.array([130, 158, 149])
# Calculate MSE
mse = mean_squared_error(y_actual, y_pred)
print(f"Mean Squared Error: {mse:.2f}")
Output:
Mean Squared Error: 7.33
Another code Example
import numpy as np
def mse_loss(y_true, y_pred):
# y_true and y_pred are numpy arrays of the same length.
return ((y_true - y_pred) ** 2).mean()
y_true = np.array([1, 0, 0, 1])
y_pred = np.array([0, 0, 0, 0])
print(mse_loss(y_true, y_pred)) # 0.5
Thanks for reading....
5 Reactions
0 Bookmarks