forked from minitorch/Module-0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.py
243 lines (170 loc) · 4.22 KB
/
operators.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"""
Collection of the core mathematical operators used throughout the code base.
"""
import math
# ## Task 0.1
from typing import Callable, Iterable, Optional
#
# Implementation of a prelude of elementary functions.
# Mathematical functions:
# - mul
# - id
# - add
# - neg
# - lt
# - eq
# - max
# - is_close
# - sigmoid
# - relu
# - log
# - exp
# - log_back
# - inv
# - inv_back
# - relu_back
#
# For sigmoid calculate as:
# $f(x) = \frac{1.0}{(1.0 + e^{-x})}$ if x >=0 else $\frac{e^x}{(1.0 + e^{x})}$
# For is_close:
# $f(x) = |x - y| < 1e-2$
# TODO: Implement for Task 0.1.
def mul(x: float, y: float) -> float:
"""
Scalar multiplication.
Args:
x: A float value
y: A float value
Returns:
A float value x multiplied by y
"""
return x * y
def id(x: float) -> float:
"""
Identity function.
Args:
x: A float value.
Returns:
The float input unchanged.
"""
return x
def add(x: float, y: float) -> float:
"""
Addition function.
Args:
x: A float value.
y: A float value.
Returns:
A float value x added to y
"""
return x + y
def neg(x: float) -> float:
"""
Negation function.
Args:
x: A float value.
Returns:
A float value x multiplied by -1.0
"""
return -x
def lt(x: float, y: float) -> bool:
"""
Compares 2 float values.
Args:
x: A float value.
y: A float value.
Returns:
A boolean value. True if x is less than y.
"""
return x < y
def eq(x: float, y: float) -> bool:
"""
Equality function.
Args:
x: A float value.
y: A float value.
Returns:
A boolean value. True if x is equal to y.
"""
return x == y
def max(x: float, y: float) -> float:
"""
Max function.
Args:
x: A float value.
y: A float value.
Returns:
The larger value between x and y.
"""
return x if x > y else y
def is_close(
x: float, y: float, atol: Optional[float] = 1e-8, rtol: Optional[float] = 1e-5
) -> bool:
"""
Checks if x is close to y. Obtained equation from https://pytorch.org/docs/stable/generated/torch.isclose.html.
Args:
x: A float value.
y: A float value.
atol: Absolute tolerance. Default: 1e-8
rtol: Relative tolerance. Default: 1e-5
Returns:
A boolean value indicating if x is close to y
"""
return math.fabs(x - y) <= (atol + rtol * math.fabs(y))
def sigmoid(x: float) -> float:
"""
Sigmoid function.
Args:
x: A float value
Returns:
A float value 1 / (1 + e^-x)
"""
return 1.0 / (1.0 + math.exp(-x))
def relu(x: float) -> float:
return 0.0 if x <= 0.0 else x
def log(x: float) -> float:
return math.log(x)
def exp(x: float) -> float:
return math.exp(x)
def inv(x: float) -> float:
return 1.0 / x
def log_back(x: float, y: float) -> float:
return y / x
def inv_back(x: float, y: float) -> float:
return -y / x**2
def relu_back(x: float, y: float) -> float:
return 0.0 if x <= 0 else y
# ## Task 0.3
# Small practice library of elementary higher-order functions.
# Implement the following core functions
# - map
# - zipWith
# - reduce
#
# Use these to implement
# - negList : negate a list
# - addLists : add two lists together
# - sum: sum lists
# - prod: take the product of lists
# TODO: Implement for Task 0.3.
def map(func: Callable[[float], float], xs: Iterable[float]) -> Iterable[float]:
return [func(x) for x in xs]
def zipWith(
func: Callable[[float, float], float], xs: Iterable[float], ys: Iterable[float]
) -> Iterable[float]:
return [func(x, y) for x, y in zip(xs, ys)]
def reduce(func: Callable[[float, float], float], xs: Iterable[float]) -> float:
if len(xs) == 0:
return 0.0
acc = xs[0]
for x in xs[1:]:
acc = func(acc, x)
return acc
def negList(xs: Iterable[float]) -> Iterable[float]:
return map(neg, xs)
def addLists(xs: Iterable[float], ys: Iterable[float]) -> Iterable[float]:
return zipWith(add, xs, ys)
def sum(xs: Iterable[float]) -> float:
return reduce(add, xs)
def prod(xs: Iterable[float]) -> float:
return reduce(mul, xs)