-
Notifications
You must be signed in to change notification settings - Fork 268
/
operators.py
195 lines (125 loc) · 4.24 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
import math
## Task 0.1
## Mathematical operators
def mul(x, y):
":math:`f(x, y) = x * y`"
# TODO: Implement for Task 0.1.
raise NotImplementedError('Need to implement for Task 0.1')
def id(x):
":math:`f(x) = x`"
# TODO: Implement for Task 0.1.
raise NotImplementedError('Need to implement for Task 0.1')
def add(x, y):
":math:`f(x, y) = x + y`"
# TODO: Implement for Task 0.1.
raise NotImplementedError('Need to implement for Task 0.1')
def neg(x):
":math:`f(x) = -x`"
# TODO: Implement for Task 0.1.
raise NotImplementedError('Need to implement for Task 0.1')
def lt(x, y):
":math:`f(x) =` 1.0 if x is greater than y else 0.0"
# TODO: Implement for Task 0.1.
raise NotImplementedError('Need to implement for Task 0.1')
def eq(x, y):
":math:`f(x) =` 1.0 if x is equal to y else 0.0"
# TODO: Implement for Task 0.1.
raise NotImplementedError('Need to implement for Task 0.1')
def max(x, y):
":math:`f(x) =` x if x is greater than y else y"
# TODO: Implement for Task 0.1.
raise NotImplementedError('Need to implement for Task 0.1')
def sigmoid(x):
r"""
:math:`f(x) = \frac{1.0}{(1.0 + e^{-x})}`
(See `<https://en.wikipedia.org/wiki/Sigmoid_function>`_ .)
Calculate as
:math:`f(x) = \frac{1.0}{(1.0 + e^{-x})}` if x >=0 else :math:`\frac{e^x}{(1.0 + e^{x})}`
for stability.
"""
# TODO: Implement for Task 0.1.
raise NotImplementedError('Need to implement for Task 0.1')
def relu(x):
"""
:math:`f(x) =` x if x is greater than 0, else 0
(See `<https://en.wikipedia.org/wiki/Rectifier_(neural_networks)>`_ .)
"""
# TODO: Implement for Task 0.1.
raise NotImplementedError('Need to implement for Task 0.1')
def relu_back(x, y):
":math:`f(x) =` y if x is greater than 0 else 0"
# TODO: Implement for Task 0.1.
raise NotImplementedError('Need to implement for Task 0.1')
EPS = 1e-6
def log(x):
":math:`f(x) = log(x)`"
return math.log(x + EPS)
def exp(x):
":math:`f(x) = e^{x}`"
return math.exp(x)
def log_back(a, b):
return b / (a + EPS)
def inv(x):
":math:`f(x) = 1/x`"
return 1.0 / x
def inv_back(a, b):
return -(1.0 / a ** 2) * b
## Task 0.3
## Higher-order functions.
def map(fn):
"""
Higher-order map.
.. image:: figs/Ops/maplist.png
See `<https://en.wikipedia.org/wiki/Map_(higher-order_function)>`_
Args:
fn (one-arg function): process one value
Returns:
function : a function that takes a list and applies `fn` to each element
"""
# TODO: Implement for Task 0.3.
raise NotImplementedError('Need to implement for Task 0.3')
def negList(ls):
"Use :func:`map` and :func:`neg` to negate each element in `ls`"
return map(neg)(ls)
def zipWith(fn):
"""
Higher-order zipwith (or map2).
.. image:: figs/Ops/ziplist.png
See `<https://en.wikipedia.org/wiki/Map_(higher-order_function)>`_
Args:
fn (two-arg function): combine two values
Returns:
function : takes two equally sized lists `ls1` and `ls2`, produce a new list by
applying fn(x, y) one each pair of elements.
"""
# TODO: Implement for Task 0.3.
raise NotImplementedError('Need to implement for Task 0.3')
def addLists(ls1, ls2):
"Add the elements of `ls1` and `ls2` using :func:`zipWith` and :func:`add`"
return zipWith(add)(ls1, ls2)
def reduce(fn, start):
r"""
Higher-order reduce.
.. image:: figs/Ops/reducelist.png
Args:
fn (two-arg function): combine two values
start (float): start value :math:`x_0`
Returns:
function : function that takes a list `ls` of elements
:math:`x_1 \ldots x_n` and computes the reduction :math:`fn(x_3, fn(x_2,
fn(x_1, x_0)))`
"""
# TODO: Implement for Task 0.3.
raise NotImplementedError('Need to implement for Task 0.3')
def sum(ls):
"""
Sum up a list using :func:`reduce` and :func:`add`.
"""
# TODO: Implement for Task 0.3.
raise NotImplementedError('Need to implement for Task 0.3')
def prod(ls):
"""
Product of a list using :func:`reduce` and :func:`mul`.
"""
# TODO: Implement for Task 0.3.
raise NotImplementedError('Need to implement for Task 0.3')