forked from girafe-ai/ml-course
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloss_and_derivatives.py
More file actions
143 lines (106 loc) · 4.49 KB
/
Copy pathloss_and_derivatives.py
File metadata and controls
143 lines (106 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import numpy as np
class LossAndDerivatives:
@staticmethod
def mse(X, Y, w):
"""
X : numpy array of shape (`n_observations`, `n_features`)
Y : numpy array of shape (`n_observations`, `target_dimentionality`) or (`n_observations`,)
w : numpy array of shape (`n_features`, `target_dimentionality`) or (`n_features`,)
Return : float
single number with MSE value of linear model (X.dot(w)) with no bias term
on the selected dataset.
Comment: If Y is two-dimentional, average the error over both dimentions.
"""
return np.mean((X.dot(w) - Y)**2)
@staticmethod
def mae(X, Y, w):
"""
X : numpy array of shape (`n_observations`, `n_features`)
Y : numpy array of shape (`n_observations`, `target_dimentionality`) or (`n_observations`,)
w : numpy array of shape (`n_features`, `target_dimentionality`) or (`n_features`,)
Return: float
single number with MAE value of linear model (X.dot(w)) with no bias term
on the selected dataset.
Comment: If Y is two-dimentional, average the error over both dimentions.
"""
# YOUR CODE HERE
return
@staticmethod
def l2_reg(w):
"""
w : numpy array of shape (`n_features`, `target_dimentionality`) or (`n_features`,)
Return: float
single number with sum of squared elements of the weight matrix ( \sum_{ij} w_{ij}^2 )
Computes the L2 regularization term for the weight matrix w.
"""
# YOUR CODE HERE
return
@staticmethod
def l1_reg(w):
"""
w : numpy array of shape (`n_features`, `target_dimentionality`)
Return : float
single number with sum of the absolute values of the weight matrix ( \sum_{ij} |w_{ij}| )
Computes the L1 regularization term for the weight matrix w.
"""
# YOUR CODE HERE
return
@staticmethod
def no_reg(w):
"""
Simply ignores the regularization
"""
return 0.
@staticmethod
def mse_derivative(X, Y, w):
"""
X : numpy array of shape (`n_observations`, `n_features`)
Y : numpy array of shape (`n_observations`, `target_dimentionality`) or (`n_observations`,)
w : numpy array of shape (`n_features`, `target_dimentionality`) or (`n_features`,)
Return : numpy array of same shape as `w`
Computes the MSE derivative for linear regression (X.dot(w)) with no bias term
w.r.t. w weight matrix.
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
@staticmethod
def mae_derivative(X, Y, w):
"""
X : numpy array of shape (`n_observations`, `n_features`)
Y : numpy array of shape (`n_observations`, `target_dimentionality`) or (`n_observations`,)
w : numpy array of shape (`n_features`, `target_dimentionality`) or (`n_features`,)
Return : numpy array of same shape as `w`
Computes the MAE derivative for linear regression (X.dot(w)) with no bias term
w.r.t. w weight matrix.
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
@staticmethod
def l2_reg_derivative(w):
"""
w : numpy array of shape (`n_features`, `target_dimentionality`) or (`n_features`,)
Return : numpy array of same shape as `w`
Computes the L2 regularization term derivative w.r.t. the weight matrix w.
"""
# YOUR CODE HERE
return
@staticmethod
def l1_reg_derivative(w):
"""
Y : numpy array of shape (`n_observations`, `target_dimentionality`) or (`n_observations`,)
w : numpy array of shape (`n_features`, `target_dimentionality`) or (`n_features`,)
Return : numpy array of same shape as `w`
Computes the L1 regularization term derivative w.r.t. the weight matrix w.
"""
# YOUR CODE HERE
return
@staticmethod
def no_reg_derivative(w):
"""
Simply ignores the derivative
"""
return np.zeros_like(w)