12
12
13
13
def mul (x : float , y : float ) -> float :
14
14
"$f(x, y) = x * y$"
15
- # TODO: Implement for Task 0.1.
16
15
return x * y
17
16
18
17
19
18
def id (x : float ) -> float :
20
19
"$f(x) = x$"
21
- # TODO: Implement for Task 0.1.
20
+
22
21
return x
23
22
24
23
25
24
def add (x : float , y : float ) -> float :
26
25
"$f(x, y) = x + y$"
27
- # TODO: Implement for Task 0.1.
26
+
28
27
return x + y
29
28
30
29
31
30
def neg (x : float ) -> float :
32
31
"$f(x) = -x$"
33
- # TODO: Implement for Task 0.1.
32
+
34
33
return - x
35
34
36
35
37
36
def lt (x : float , y : float ) -> float :
38
37
"$f(x) =$ 1.0 if x is less than y else 0.0"
39
- # TODO: Implement for Task 0.1.
38
+
40
39
return float (x < y )
41
40
42
41
43
42
def eq (x : float , y : float ) -> float :
44
43
"$f(x) =$ 1.0 if x is equal to y else 0.0"
45
- # TODO: Implement for Task 0.1.
44
+
46
45
return float (x == y )
47
46
48
47
49
48
def max (x : float , y : float ) -> float :
50
49
"$f(x) =$ x if x is greater than y else y"
51
- # TODO: Implement for Task 0.1.
50
+
52
51
return x if x >= y else y
53
52
54
53
55
54
def is_close (x : float , y : float ) -> float :
56
55
"$f(x) = |x - y| < 1e-2$"
57
- # TODO: Implement for Task 0.1.
56
+
58
57
if x >= y :
59
58
return x - y <= 1e-2
60
59
else :
@@ -73,7 +72,7 @@ def sigmoid(x: float) -> float:
73
72
74
73
for stability.
75
74
"""
76
- # TODO: Implement for Task 0.1.
75
+
77
76
return (1.0 / (1.0 + math .exp (- x ))) if x >= 0 else math .exp (x ) / (1 + math .exp (x ))
78
77
79
78
@@ -83,7 +82,7 @@ def relu(x: float) -> float:
83
82
84
83
(See https://en.wikipedia.org/wiki/Rectifier_(neural_networks) .)
85
84
"""
86
- # TODO: Implement for Task 0.1.
85
+
87
86
return x if x > 0.0 else 0
88
87
89
88
@@ -102,26 +101,26 @@ def exp(x: float) -> float:
102
101
103
102
def log_back (x : float , d : float ) -> float :
104
103
r"If $f = log$ as above, compute $d \times f'(x)$"
105
- # TODO: Implement for Task 0.1.
104
+
106
105
return d / x
107
106
108
107
109
108
def inv (x : float ) -> float :
110
109
"$f(x) = 1/x$"
111
- # TODO: Implement for Task 0.1.
110
+
112
111
if x != 0.0 :
113
112
return 1 / x
114
113
115
114
116
115
def inv_back (x : float , d : float ) -> float :
117
116
r"If $f(x) = 1/x$ compute $d \times f'(x)$"
118
- # TODO: Implement for Task 0.1.
117
+
119
118
return - (x ** (- 2 )) * d
120
119
121
120
122
121
def relu_back (x : float , d : float ) -> float :
123
122
r"If $f = relu$ compute $d \times f'(x)$"
124
- # TODO: Implement for Task 0.1.
123
+
125
124
return d * (x > 0.0 )
126
125
127
126
0 commit comments