forked from epfml/ML_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4108d27
commit 4181eec
Showing
43 changed files
with
35,839 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.