Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions homeworks/assignment03_derivatives/derivatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ def mae(X, Y, w):
Comment: If Y is two-dimentional, average the error over both dimentions.
"""

# YOUR CODE HERE
return
pred = X.dot(w)
if Y ndim == 2:
return np.mean(np.abs(pred - Y))
else:
return np.mean(np.abs(pred - Y))

@staticmethod
def l2_reg(w):
Expand All @@ -47,7 +50,7 @@ def l2_reg(w):
"""

# YOUR CODE HERE
return
return np.sum(w**2)

@staticmethod
def l1_reg(w):
Expand All @@ -61,7 +64,7 @@ def l1_reg(w):
"""

# YOUR CODE HERE
return
return np.sum(np.abs(w))

@staticmethod
def no_reg(w):
Expand All @@ -86,8 +89,13 @@ def mse_derivative(X, Y, w):
dimension as well, so you need to consider that fact in derivative implementation.
"""

# YOUR CODE HERE
return
n_observations, target_dim = Y.shape
pred = X.dot(w)
if Y.ndim == 2:
error = 2 * (pred - Y) / (n_observations * target_dim)
else:
error = 2 * (pred - Y) / n_observations
return X.T.dot(error)

@staticmethod
def mae_derivative(X, Y, w):
Expand All @@ -104,9 +112,12 @@ def mae_derivative(X, Y, w):
Please mention, that in case `target_dimentionality` > 1 the error is averaged along this
dimension as well, so you need to consider that fact in derivative implementation.
"""

# YOUR CODE HERE
return
pred = X.dot(w)
if Y.ndim == 2:
error = np.sign(pred - Y) / (n_observations * target_dim)
else:
error = np.sign(pred - Y) /n_observations
return X.T.dot(error)

@staticmethod
def l2_reg_derivative(w):
Expand All @@ -119,7 +130,7 @@ def l2_reg_derivative(w):
"""

# YOUR CODE HERE
return
return 2*w

@staticmethod
def l1_reg_derivative(w):
Expand All @@ -133,7 +144,7 @@ def l1_reg_derivative(w):
"""

# YOUR CODE HERE
return
return np.sign(w)

@staticmethod
def no_reg_derivative(w):
Expand Down