I prefer initialising weights at 0th epoch, and calculating loss at the end of each epoch, because that gives me the respective loss values of each
weight (including the first), which ensures that I can plot the entire gradient descent process from first to last.
During optimisation, I adjust gradient instead of learning rate, because Adam adjusts the learning rate differently in different directions,
so it seems like dividing the gradient value makes sense because it has multiple components, while learning rate is a single scalar value.
Unless this somehow results to a wrong value, I want it done this way.
The above two are my preferred design choices, please do not suggest me to change them (unless they’re giving wrong results, and the only
solution is to not use them). Instead fix the code while maintaining this design choice.
Introduction
I'm solving a simplified logical regression problem, that only uses weights w1 and w2 and no bias term. The data used here is generated synthetically
to nicely fit into such model. (Not having a bias terms, reduces the parameters to update during training to w1 and w2, making it possible for me to
visualise the entire gradient descent process with only 3 dimension axes later on).
Theres two separate implementations of this here: 1st one does not use helper functions, and it is able to converge to 87% accuracy. While the second
implementation uses helper functions to reduce code repetititon, but something sneaky is going on in the second implementation, that is causing the
accuracy to be stuck at 20% no matter how many epochs you train it for.
Implementation 1: does not use helper functions, able to converge to 87% accuracy
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.model_selection import train_test_split
We will be using binary crossentrophy as the loss function to minimize
class BinaryCrossEntropy:
@staticmethod
def value(y_true, y_pred):
# Average loss over all of the samples
return -np.mean(y_true * np.log(y_pred + 1e-8) + (1 - y_true) * np.log(1 - y_pred + 1e-8))
code
@staticmethod
def gradient(y_true, y_pred):
# Gradient of loss function w.r.t predictions
return (y_pred - y_true) / (y_pred * (1 - y_pred) + 1e-8)
def fit(self, X_train, y_train, X_val, y_val, epochs=100, initial_weights=None, learning_rate=0.003, batch_size=32, verbose=True):
self.learning_rate = learning_rate
num_samples, num_features = X_train.shape
# Initialize lists to store metrics for each epoch
train_losses, train_accuracies, val_losses, val_accuracies = [], [], [], []
weight_history = []
for epoch in range(epochs):
# If it's the first epoch, initialize weights
if epoch == 0:
np.random.seed(42)
self.weights = np.array(initial_weights) if initial_weights else np.random.uniform(-1, 1, num_features)
# In the beginning of other epochs, we update the weights
else:
# Process data in mini-batches
for i in range(0, num_samples, batch_size):
X_batch = X_train[i:i + batch_size]
y_true_batch = y_train[i:i + batch_size]
# Calculate predictions for the batch
y_predicted_batch = compute_predictions(X_batch, self.weights)
# Gradient of loss function w.r.t. predicted y
grad_loss_batch = self.loss_f.gradient(y_true_batch, y_predicted_batch)
# Use chain rule to get gradient of loss function w.r.t. weights
delta_w_batch = np.dot(X_batch.T, grad_loss_batch) / batch_size
# Get the adjusted gradient from the optimizer
if self.optimizer:
delta_w_batch = self.optimizer.update(delta_w_batch)
# Update the weights using that gradient
self.weights -= self.learning_rate * delta_w_batch
# Make predictions for the whole training set after all updates in the epoch
y_predicted = compute_predictions(X_train, self.weights)
# Compute the binary cross-entropy loss and accuracy for training data
train_loss = self.loss_f.value(y_train, y_predicted)
train_accuracy = self.accuracy(y_train, y_predicted)
# Validation
val_y_predicted = compute_predictions(X_val, self.weights)
val_loss = self.loss_f.value(y_val, val_y_predicted)
val_accuracy = self.accuracy(y_val, val_y_predicted)
# Store the weights and corresponding metrics
weight_history.append(self.weights.copy())
train_losses.append(train_loss)
train_accuracies.append(train_accuracy)
val_losses.append(val_loss)
val_accuracies.append(val_accuracy)
# Display the metrics for the current epoch
if verbose:
print(f"Epoch {epoch + 1}:")
print(f" Weights: {self.weights}")
print(f" Train Loss: {train_loss:.8f}, Train Accuracy: {train_accuracy:.4f}")
print(f" Validation Loss: {val_loss:.8f}, Validation Accuracy: {val_accuracy:.4f}")
print("--------------------------")
# Return all the training metrics
return train_losses, train_accuracies, val_losses, val_accuracies, weight_history
def predict(self, X):
return compute_predictions(X, self.weights)
"""# Define the optimizer"""
class Adam:
def init(self, learning_rate=0.001, beta1=0.9, beta2=0.999, epsilon=1e-8):
self.learning_rate = learning_rate
self.beta1 = beta1
self.beta2 = beta2
self.epsilon = epsilon
self.m_t = None # First moment estimate (momentum)
self.v_t = None # Second moment estimate (RMSProp-like)
self.t = 0 # Timestep for bias correction
code
def update(self, gradient):
if self.m_t is None:
self.m_t = np.zeros_like(gradient)
if self.v_t is None:
self.v_t = np.zeros_like(gradient)
self.t += 1
# Update biased first and second moment estimates
self.m_t = self.beta1 * self.m_t + (1 - self.beta1) * gradient
self.v_t = self.beta2 * self.v_t + (1 - self.beta2) * (gradient ** 2)
# Bias-corrected moment estimates
m_hat = self.m_t / (1 - self.beta1 ** self.t)
v_hat = self.v_t / (1 - self.beta2 ** self.t)
# Update weights
adjusted_gradient = m_hat / (np.sqrt(v_hat) + self.epsilon)
return self.learning_rate * adjusted_gradient
"""# Perform Gradient Descent
Here the accuracy never improves beyoned 20%
"""
The issue you're encountering in your second implementation lies in the calculation of the gradient in your BinaryCrossEntropy loss function. Specifically, the gradient of the loss with respect to the predictions y_pred is incorrectly including an unnecessary denominator (y_pred * (1 - y_pred)). This inclusion leads to incorrect gradient values, causing your model's accuracy to remain stuck at 20%.
Here's why:
Correct Gradient Calculation:
For logistic regression with binary cross-entropy loss, the gradient of the loss with respect to the weights can be derived as:
This formula shows that the gradient with respect to the weights depends on the difference between the predicted and true values, scaled by the input features X.
Including the denominator (y_pred * (1 - y_pred)) is unnecessary when you're already applying the chain rule correctly in your gradient descent. This denominator should cancel out when you consider the derivative of the sigmoid function, but in your implementation, it doesn't, leading to incorrect gradient values.
Solution:
Modify your gradient method in the BinaryCrossEntropy class to remove the denominator, like so:
python
class BinaryCrossEntropy:
@staticmethod
def value(y_true, y_pred):
# Average loss over all of the samples
return -np.mean(y_true * np.log(y_pred + 1e-8) + (1 - y_true) * np.log(1 - y_pred + 1e-8))
@staticmethod
def gradient(y_true, y_pred):
# Correct gradient of loss function w.r.t predictions
return y_pred - y_true
By making this change, you ensure that the gradient calculation aligns with the correct mathematical derivation for logistic regression using binary cross-entropy loss.
Why This Works:
When you compute the gradient of the loss with respect to the weights using the chain rule, the derivative of the sigmoid function (which gives (y_pred * (1 - y_pred))) cancels out the denominator in your original gradient calculation. Including the denominator again in your gradient method effectively squares this term, which is incorrect.
Updated Implementation:
Here’s the corrected version of your second implementation:
python
# ... [Rest of your code remains unchanged] ...
"""# Define Helper Functions"""
# Corrected BinaryCrossEntropy class
class BinaryCrossEntropy:
@staticmethod
def value(y_true, y_pred):
# Average loss over all of the samples
return -np.mean(
y_true * np.log(y_pred + 1e-8) +
(1 - y_true) * np.log(1 - y_pred + 1e-8)
)
@staticmethod
def gradient(y_true, y_pred):
# Correct gradient of loss function w.r.t predictions
return y_pred - y_true
# ... [Rest of your code remains unchanged] ...
"""# Perform Gradient Descent
Now the accuracy should improve over epochs.
"""
gd = GradientDescent(optimizer=Adam())
# Testing with corrected gradient
metrics = gd.fit(
X_train, y_train, X_val, y_val,
epochs=100, batch_size=32,
initial_weights=[15.0, -20.0],
learning_rate=0.02
)
Result:
After making this change, you'll observe that the model's accuracy improves over epochs, similar to your first implementation. The weights will update correctly, and the model should converge to a similar accuracy as before.
Preserving Your Design Choices:
This fix aligns with your design choices:
Initializing weights at epoch 0 and calculating loss at the end of each epoch: This change does not affect the initialization or when you calculate the loss. It simply ensures that the gradient used during the weight updates is correct.
Adjusting the gradient instead of the learning rate: The adjustment is made in the gradient calculation, and you continue to adjust the gradient via the optimizer (Adam), without modifying the learning rate schema.
Conclusion:
The key takeaway is that in logistic regression with binary cross-entropy loss, the gradient of the loss with respect to the predictions simplifies to (y_pred - y_true). Including additional terms can lead to incorrect gradient calculations and hinder the training process.