The regression problem involves predicting a continuous numerical value. Regression models approximate a function
The hypotheses space
The hypotheses space
The simplest example is "fitting a straight line". The model learns the coefficients
The hypotheses space
The simplest example is "fitting a straight line". The model learns the coefficients
The simplest example is "fitting a straight line". The model learns the coefficients
Finding the
The simplest example is "fitting a straight line". The model learns the coefficients
Finding the
Finding the values of the weights
Given the
We want to find
Given the
We want to find
We know that the loss is minimised when its partial derivatives with respect to
We know that the loss is minimised when its partial derivatives with respect to
We know that the loss is minimised when its partial derivatives with respect to
Solving the partial derivatives
We choose any starting point and then compute an estimate of the gradient and move a small amount in the steepest downhill direction, repeating until we converge on a point in the weight space with (local) minima loss.
Gradient Descent Algorithm:
Initialize w randomly
repeat
for each w[i] in w
Compute gradient: g = ∇Loss(w[i])
Update weight: w[i] = w[i] - α * g
until convergence
The size of the step is given by the parameter
Following with our "straight line" example, the update rule for
Following with our "straight line" example, the update rule for
The loss function is represented as a composition of functions.
Following with our "straight line" example, the update rule for
The loss function is represented as a composition of functions.
We need to differentiate the loss function step by step using the chain rule to compute the gradients.
Applying the chain rule to the loss function
Applying the chain rule to the loss function
Applying this to
The update rule for each weight in the "straight line" example is (folding 2 into
The update rule for each weight in the "straight line" example is (folding 2 into
For
These updates constitute the batch gradient descent. For the "straight line", this gradient descent is deterministic.
An epoch is defined as one complete pass through the entire dataset during the training process. Multiple epochs are needed for the model to converge to an optimal solution. Not enough epochs could generate underfitting. Too many epochs could generate overfitting. Another hyperparameter for the model.
Batch Gradient Descent
The algorithm updates
import numpy as np
def batch_gradient_descent(X, y, alpha=0.01, epochs=1000):
"""
X: numpy array of shape (n_samples, n_features)
y: numpy array of shape (n_samples,)
alpha: learning rate
epochs: number of passes over the data
Returns: w (numpy array of shape (n_features,))
"""
n_samples, n_features = X.shape
w = np.zeros(n_features)
for epoch in range(epochs):
grad = [0] * len(w)
for i in range(n_samples):
x_i = X[i]
y_i = y[i]
prediction = dot(w, x_i)
error = prediction - y_i
for j in range(len(w)):
grad[j] += error * x_i[j]
for j in range(len(w)):
grad[j] /= len(n_samples)
w[j] -= alpha * grad[j]
return w
Stochastic Gradient Descent
The algorithm updates
import numpy as np
def stochastic_gradient_descent(X, y, alpha=0.01, epochs=1000):
"""
X: numpy array of shape (n_samples, n_features)
y: numpy array of shape (n_samples,)
alpha: learning rate
epochs: number of passes over the data
Returns: w (numpy array of shape (n_features,))
"""
n_samples, n_features = X.shape
w = np.zeros(n_features)
for epoch in range(epochs):
for i in range(n_samples):
x_i = X[i]
y_i = y[i]
prediction = np.dot(w, x_i)
error = prediction - y_i
for j in range(n_features):
w[j] -= alpha * error * x_i[j]
return w
Mini-batch Gradient Descent
The algorithm updates
import numpy as np
def mini_batch_gradient_descent(X, y, alpha=0.01, epochs=1000, batch_size=32):
"""
X: numpy array of shape (n_samples, n_features)
y: numpy array of shape (n_samples,)
alpha: learning rate
epochs: number of passes over the data
batch_size: number of samples per batch
Returns: w (numpy array of shape (n_features,))
"""
n_samples, n_features = X.shape
w = np.zeros(n_features)
for epoch in range(epochs):
indices = np.arange(n_samples)
np.random.shuffle(indices)
X_shuffled = X[indices]
y_shuffled = y[indices]
for start in range(0, n_samples, batch_size):
end = start + batch_size
X_batch = X_shuffled[start:end]
y_batch = y_shuffled[start:end]
y_pred = X_batch @ w
error = y_pred - y_batch
grad = X_batch.T @ error / len(X_batch)
w -= alpha * grad
return w
Linear Regression
Training Dataset
Hypothesis Space: All possible linear functions of continuous-valued inputs and outputs.
Hypothesis:
Loss Function:
Cost Function:
Linear Regression
Analytical Solution:
Gradient Descent Algorithm:
Initialize w randomly
repeat
for each w[i] in w
Compute gradient: g = ∇Loss(w[i])
Update weight: w[i] = w[i] - α * g
until convergence
Hyperparameters: Learning rate, number of epochs, and batch size.
Multivariate regression extends the simple linear model to handle multiple input features. The model learns a function that maps multiple input variables to a continuous output.
In these problems, each example is a n-element vector. The hypotheses space
In these problems, each example is a n-element vector. The hypotheses space
We want to find the
In these problems, each example is a n-element vector. The hypotheses space
We want to find the
In vector notation:
Where
The loss function for multivariate regression is:
The loss function for multivariate regression is:
In matrix notation:
Where
Given the Loss function:
Given the Loss function:
We want to find
Given the Loss function:
We want to find
Taking the derivative with respect to
Given the Loss function:
We want to find
Taking the derivative with respect to
This leads to the normal equation:
Gradient Descent for Multivariate Regression
The gradient of the loss function is:
Gradient Descent for Multivariate Regression
The gradient of the loss function is:
Update rule for
Where
Underfitting occurs when our hypothesis space
Even the best hypothesis
where
Overfitting occurs when our hypothesis space
Low empirical loss but high generalisation loss:
The hypothesis
Regularisation transforms the loss function into a cost function that penalizes complexity to avoid overfitting
Choosing the complexity function depends on the hypotheses space
Given the new cost function
The best hypothesis
where
Alternatives to mitigate overfitting are feature selection, hyperparameter tuning, splitting dataset in training, validation, and test data (e.g., cross validation algorithm).
Linear classifiers are models that separate data into classes using a linear decision boundary. These classifiers are functions that can decide if an input (i.e., vectors of numbers) belong to a specific class or not.
A decision boundary is a line (or a surface in higher dimensions) that separates data into classes.
A decision boundary is a line (or a surface in higher dimensions) that separates data into classes.
The hypothesis is the result of passing a linear function through a threshold function:
A decision boundary is a line (or a surface in higher dimensions) that separates data into classes.
The hypothesis is the result of passing a linear function through a threshold function:
The optimal hypothesis is the one that minimises the loss function:
Where
The optimal hypothesis is the one that minimises the loss function:
Where
Partial derivatives and gradient methods do not work. Instead, we apply perceptron learning rule.
Perceptron update rule:
The rule is applied one example at a time, choosing examples at random (as in stochastic gradient descent).
An alternative to the discrete threshold function is the logistic function, which is smooth and differentiable.
An alternative to the discrete threshold function is the logistic function, which is smooth and differentiable.
An alternative to the discrete threshold function is the logistic function, which is smooth and differentiable. The process of finding the optimal hypothesis is called logistic regression.
Where
An alternative to the discrete threshold function is the logistic function, which is smooth and differentiable. The process of finding the optimal hypothesis is called logistic regression.
Where
Following the gradient descent algorithm, the update rule is:
Following the gradient descent algorithm, the update rule is:
The loss function is represented as a composition of functions:
Following the gradient descent algorithm, the update rule is:
The loss function is represented as a composition of functions:
We need to differentiate the loss function using the chain rule to compute the gradients:
Following the gradient descent algorithm, the update rule is:
Following the gradient descent algorithm, the update rule is:
The resulting update rule after solving the partial derivatives:
The gradient descent algorithm is applied using this update rule.
Gradient Descent for Logistic Regression
The gradient of the loss function is:
Update rule for
Where
The perceptron is one of the earliest and simplest artificial neural network models for binary classification.
The perceptron was simulated by Frank Rosenblatt in 1957 on an IBM 704 machine as a model for biological neural networks. The neural network was invented in 1943 by McCulloch & Pitts.
The perceptron was simulated by Frank Rosenblatt in 1957 on an IBM 704 machine as a model for biological neural networks. The neural network was invented in 1943 by McCulloch & Pitts.
By 1962 Rosenblatt published the "Principles of Neurodynamics: Perceptrons and the Theory of Brain Mechanisms".
The perceptron was simulated by Frank Rosenblatt in 1957 on an IBM 704 machine as a model for biological neural networks. The neural network was invented in 1943 by McCulloch & Pitts.
By 1962 Rosenblatt published the "Principles of Neurodynamics: Perceptrons and the Theory of Brain Mechanisms".
The Perceptron machine is named Mark I. It is a special-purpose hardware that implemented the perceptron supervised learning for image recognition.
The Perceptron machine is named Mark I. It is a special-purpose hardware that implemented the perceptron supervised learning for image recognition.
The Perceptron machine is named Mark I. It is a special-purpose hardware that implemented the perceptron supervised learning for image recognition.
The perceptron is a linear classifier model (i.e., linear discriminant), with hypothesis space defined by all the functions of the form:
The perceptron is a linear classifier model (i.e., linear discriminant), with hypothesis space defined by all the functions of the form:
The function
The perceptron is a linear classifier model (i.e., linear discriminant), with hypothesis space defined by all the functions of the form:
The function
We want to find
We need to derive the perceptron criterion. We know we are seeking parameters vector
And for features in
Using
Using
The loss function is:
The update rule for a missclassified input is:
The perceptron learning algorithm is similar to the stochastic gradient descent.
Initialise weights w randomly
repeat
for each training example (x, y)
Compute prediction: y_pred = f(w·Φ(x))
if y_pred ≠ y then
Update weights: w = w + αΦ(x)y
until no misclassifications or max iterations
import numpy as np
class Perceptron:
def __init__(self, learning_rate=0.01, max_iterations=1000):
self.learning_rate = learning_rate
self.max_iterations = max_iterations
self.weights = None
self.bias = None
def fit(self, X, y):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
for _ in range(self.max_iterations):
misclassified = 0
for i in range(n_samples):
prediction = self.predict(X[i])
if prediction != y[i]:
self.weights += self.learning_rate * (y[i] - prediction) * X[i]
self.bias += self.learning_rate * (y[i] - prediction)
misclassified += 1
if misclassified == 0:
break
def predict(self, x):
return 1 if np.dot(self.weights, x) + self.bias >= 0 else 0
Python Implementation
XOR Problem:
The perceptron cannot learn the XOR function because it's not linearly separable.
Other limitations:
Hyperparameters defined in this lecture
Learning rate
Epochs: number of complete passes through the dataset
Batch size
Regularisation strength
Max iterations: stopping limit for the perceptron learning algorithm
_script: true
This script will only execute in HTML slides
_script: true