Skip to content

Commit

Permalink
Merge branch 'epfml:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit-Muller authored Nov 5, 2022
2 parents b1075c2 + 09432d0 commit eca083d
Show file tree
Hide file tree
Showing 53 changed files with 36,514 additions and 23 deletions.
42 changes: 42 additions & 0 deletions labs/ex02/solution/costs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
"""a function used to compute the loss."""

import numpy as np


### SOLUTION
def calculate_mse(e):
"""Calculate the mse for vector e."""
return 1 / 2 * np.mean(e**2)


def calculate_mae(e):
"""Calculate the mae for vector e."""
return np.mean(np.abs(e))


### TEMPLATE
### END SOLUTION


def compute_loss(y, tx, w):
"""Calculate the loss using either MSE or MAE.
Args:
y: shape=(N, )
tx: shape=(N,2)
w: shape=(2,). The vector of model parameters.
Returns:
the value of the loss (a scalar), corresponding to the input parameters w.
"""
### SOLUTION
e = y - tx.dot(w)
return calculate_mse(e)
### TEMPLATE
# # ***************************************************
# # INSERT YOUR CODE HERE
# # TODO: compute loss by MSE
# # ***************************************************
# raise NotImplementedError
### END SOLUTION
2,266 changes: 2,266 additions & 0 deletions labs/ex02/solution/ex02.ipynb

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions labs/ex02/solution/gradient_descent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-
"""Problem Sheet 2.
Gradient Descent
"""
### SOLUTION
from costs import calculate_mse

### TEMPLATE
### END SOLUTION


def compute_gradient(y, tx, w):
"""Computes the gradient at w.
Args:
y: shape=(N, )
tx: shape=(N,2)
w: shape=(2, ). The vector of model parameters.
Returns:
An array of shape (2, ) (same shape as w), containing the gradient of the loss at w.
"""
### SOLUTION
err = y - tx.dot(w)
grad = -tx.T.dot(err) / len(err)
return grad, err

### TEMPLATE
# # ***************************************************
# # INSERT YOUR CODE HERE
# # TODO: compute gradient vector
# # ***************************************************
# raise NotImplementedError
### END SOLUTION


def gradient_descent(y, tx, initial_w, max_iters, gamma):
"""The Gradient Descent (GD) algorithm.
Args:
y: shape=(N, )
tx: shape=(N,2)
initial_w: shape=(2, ). The initial guess (or the initialization) for the model parameters
max_iters: a scalar denoting the total number of iterations of GD
gamma: a scalar denoting the stepsize
Returns:
losses: a list of length max_iters containing the loss value (scalar) for each iteration of GD
ws: a list of length max_iters containing the model parameters as numpy arrays of shape (2, ), for each iteration of GD
"""
# Define parameters to store w and loss
ws = [initial_w]
losses = []
w = initial_w
for n_iter in range(max_iters):
### SOLUTION
# compute loss, gradient
grad, err = compute_gradient(y, tx, w)
loss = calculate_mse(err)
# update w by gradient descent
w = w - gamma * grad

### TEMPLATE
# # ***************************************************
# # INSERT YOUR CODE HERE
# # TODO: compute gradient and loss
# # ***************************************************
# raise NotImplementedError
# # ***************************************************
# # INSERT YOUR CODE HERE
# # TODO: update w by gradient
# # ***************************************************
# raise NotImplementedError
### END SOLUTION

# store w and loss
ws.append(w)
losses.append(loss)
print(
"GD iter. {bi}/{ti}: loss={l}, w0={w0}, w1={w1}".format(
bi=n_iter, ti=max_iters - 1, l=loss, w0=w[0], w1=w[1]
)
)

return losses, ws
42 changes: 42 additions & 0 deletions labs/ex02/solution/grid_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
"""Exercise 2.
Grid Search
"""

import numpy as np
from costs import compute_loss


def generate_w(num_intervals):
"""Generate a grid of values for w0 and w1."""
w0 = np.linspace(-100, 200, num_intervals)
w1 = np.linspace(-150, 150, num_intervals)
return w0, w1


def get_best_parameters(w0, w1, losses):
"""Get the best w from the result of grid search."""
min_row, min_col = np.unravel_index(np.argmin(losses), losses.shape)
return losses[min_row, min_col], w0[min_row], w1[min_col]


### SOLUTION
def grid_search(y, tx, w0, w1):
"""Algorithm for grid search."""
losses = np.zeros((len(w0), len(w1)))
# compute loss for each combination of w0 and w1.
for ind_row, row in enumerate(w0):
for ind_col, col in enumerate(w1):
w = np.array([row, col])
losses[ind_row, ind_col] = compute_loss(y, tx, w)
return losses


### TEMPLATE
# # ***************************************************
# # INSERT YOUR CODE HERE
# # TODO: Paste your implementation of grid_search
# # here when it is done.
# # ***************************************************
### END SOLUTION
Loading

0 comments on commit eca083d

Please sign in to comment.