generated from minitorch/Module-0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.py
213 lines (170 loc) · 4.81 KB
/
testing.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
# type: ignore
from typing import Callable, Generic, Iterable, Tuple, TypeVar
import minitorch.operators as operators
A = TypeVar("A")
class MathTest(Generic[A]):
@staticmethod
def neg(a: A) -> A:
"Negate the argument"
return -a
@staticmethod
def addConstant(a: A) -> A:
"Add contant to the argument"
return 5 + a
@staticmethod
def square(a: A) -> A:
"Manual square"
return a * a
@staticmethod
def cube(a: A) -> A:
"Manual cube"
return a * a * a
@staticmethod
def subConstant(a: A) -> A:
"Subtract a constant from the argument"
return a - 5
@staticmethod
def multConstant(a: A) -> A:
"Multiply a constant to the argument"
return 5 * a
@staticmethod
def div(a: A) -> A:
"Divide by a constant"
return a / 5
@staticmethod
def inv(a: A) -> A:
"Invert after adding"
return operators.inv(a + 3.5)
@staticmethod
def sig(a: A) -> A:
"Apply sigmoid"
return operators.sigmoid(a)
@staticmethod
def log(a: A) -> A:
"Apply log to a large value"
return operators.log(a + 100000)
@staticmethod
def relu(a: A) -> A:
"Apply relu"
return operators.relu(a + 5.5)
@staticmethod
def exp(a: A) -> A:
"Apply exp to a smaller value"
return operators.exp(a - 200)
@staticmethod
def explog(a: A) -> A:
return operators.log(a + 100000) + operators.exp(a - 200)
@staticmethod
def add2(a: A, b: A) -> A:
"Add two arguments"
return a + b
@staticmethod
def mul2(a: A, b: A) -> A:
"Mul two arguments"
return a * b
@staticmethod
def div2(a: A, b: A) -> A:
"Divide two arguments"
return a / (b + 5.5)
@staticmethod
def gt2(a: A, b: A) -> A:
return operators.lt(b, a + 1.2)
@staticmethod
def lt2(a: A, b: A) -> A:
return operators.lt(a + 1.2, b)
@staticmethod
def eq2(a: A, b: A) -> A:
return operators.eq(a, (b + 5.5))
@staticmethod
def sum_red(a: Iterable[A]) -> A:
return operators.sum(a)
@staticmethod
def mean_red(a: Iterable[A]) -> A:
return operators.sum(a) / float(len(a))
@staticmethod
def mean_full_red(a: Iterable[A]) -> A:
return operators.sum(a) / float(len(a))
@staticmethod
def complex(a: A) -> A:
return (
operators.log(
operators.sigmoid(
operators.relu(operators.relu(a * 10 + 7) * 6 + 5) * 10
)
)
/ 50
)
@classmethod
def _tests(
cls,
) -> Tuple[
Tuple[str, Callable[[A], A]],
Tuple[str, Callable[[A, A], A]],
Tuple[str, Callable[[Iterable[A]], A]],
]:
"""
Returns a list of all the math tests.
"""
one_arg = []
two_arg = []
red_arg = []
for k in dir(MathTest):
if callable(getattr(MathTest, k)) and not k.startswith("_"):
base_fn = getattr(cls, k)
# scalar_fn = getattr(cls, k)
tup = (k, base_fn)
if k.endswith("2"):
two_arg.append(tup)
elif k.endswith("red"):
red_arg.append(tup)
else:
one_arg.append(tup)
return one_arg, two_arg, red_arg
@classmethod
def _comp_testing(cls):
one_arg, two_arg, red_arg = cls._tests()
one_argv, two_argv, red_argv = MathTest._tests()
one_arg = [(n1, f2, f1) for (n1, f1), (n2, f2) in zip(one_arg, one_argv)]
two_arg = [(n1, f2, f1) for (n1, f1), (n2, f2) in zip(two_arg, two_argv)]
red_arg = [(n1, f2, f1) for (n1, f1), (n2, f2) in zip(red_arg, red_argv)]
return one_arg, two_arg, red_arg
class MathTestVariable(MathTest):
@staticmethod
def inv(a):
return 1.0 / (a + 3.5)
@staticmethod
def sig(x):
return x.sigmoid()
@staticmethod
def log(x):
return (x + 100000).log()
@staticmethod
def relu(x):
return (x + 5.5).relu()
@staticmethod
def exp(a):
return (a - 200).exp()
@staticmethod
def explog(a):
return (a + 100000).log() + (a - 200).exp()
@staticmethod
def sum_red(a):
return a.sum(0)
@staticmethod
def mean_red(a):
return a.mean(0)
@staticmethod
def mean_full_red(a):
return a.mean()
@staticmethod
def eq2(a, b):
return a == (b + 5.5)
@staticmethod
def gt2(a, b):
return a + 1.2 > b
@staticmethod
def lt2(a, b):
return a + 1.2 < b
@staticmethod
def complex(a):
return (((a * 10 + 7).relu() * 6 + 5).relu() * 10).sigmoid().log() / 50