-
Notifications
You must be signed in to change notification settings - Fork 2
/
nn.py
231 lines (181 loc) · 6.69 KB
/
nn.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
"""The module.
"""
from typing import List, Callable, Any
from typing_extensions import Required
from needle.autograd import Tensor
from needle import ops
import needle.init as init
import numpy as np
class Parameter(Tensor):
"""A special kind of tensor that represents parameters."""
def _unpack_params(value: object) -> List[Tensor]:
if isinstance(value, Parameter):
return [value]
elif isinstance(value, Module):
return value.parameters()
elif isinstance(value, dict):
params = []
for k, v in value.items():
params += _unpack_params(v)
return params
elif isinstance(value, (list, tuple)):
params = []
for v in value:
params += _unpack_params(v)
return params
else:
return []
def _child_modules(value: object) -> List["Module"]:
if isinstance(value, Module):
modules = [value]
modules.extend(_child_modules(value.__dict__))
return modules
if isinstance(value, dict):
modules = []
for k, v in value.items():
modules += _child_modules(v)
return modules
elif isinstance(value, (list, tuple)):
modules = []
for v in value:
modules += _child_modules(v)
return modules
else:
return []
class Module:
def __init__(self):
self.training = True
def parameters(self) -> List[Tensor]:
"""Return the list of parameters in the module."""
return _unpack_params(self.__dict__)
def _children(self) -> List["Module"]:
return _child_modules(self.__dict__)
def eval(self):
self.training = False
for m in self._children():
m.training = False
def train(self):
self.training = True
for m in self._children():
m.training = True
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
class Identity(Module):
def forward(self, x):
return x
class Linear(Module):
def __init__(self, in_features, out_features, bias=True, device=None, dtype="float32"):
super().__init__()
self.in_features = in_features
self.out_features = out_features
### BEGIN YOUR SOLUTION
self.weight = Parameter(init.kaiming_uniform(in_features, out_features, dtype=dtype))
if bias:
self.bias = Parameter(init.kaiming_uniform(out_features, 1, dtype=dtype).reshape((1, out_features)))
else:
self.bias = None
### END YOUR SOLUTION
def forward(self, X: Tensor) -> Tensor:
### BEGIN YOUR SOLUTION
X_out = X @ self.weight
if self.bias:
return X_out + self.bias.broadcast_to(X_out.shape)
return X_out
### END YOUR SOLUTION
class Flatten(Module):
def forward(self, X):
### BEGIN YOUR SOLUTION
return X.reshape((X.shape[0], -1))
### END YOUR SOLUTION
class ReLU(Module):
def forward(self, x: Tensor) -> Tensor:
### BEGIN YOUR SOLUTION
return ops.relu(x)
### END YOUR SOLUTION
class Sequential(Module):
def __init__(self, *modules):
super().__init__()
self.modules = modules
def forward(self, x: Tensor) -> Tensor:
### BEGIN YOUR SOLUTION
for module in self.modules:
x = module(x)
return x
### END YOUR SOLUTION
class SoftmaxLoss(Module):
def forward(self, logits: Tensor, y: Tensor):
### BEGIN YOUR SOLUTION
exp_sum = ops.logsumexp(logits, axes=(1, )).sum()
z_y_sum = (logits * init.one_hot(logits.shape[1], y)).sum()
return (exp_sum - z_y_sum) / logits.shape[0]
### END YOUR SOLUTION
class BatchNorm1d(Module):
def __init__(self, dim, eps=1e-5, momentum=0.1, device=None, dtype="float32"):
super().__init__()
self.dim = dim
self.eps = eps
self.momentum = momentum
### BEGIN YOUR SOLUTION
self.weight = Parameter(init.ones(self.dim),requires_grad=True)
self.bias = Parameter(init.zeros(self.dim),requires_grad=True)
self.running_mean = init.zeros(self.dim)
self.running_var = init.ones(self.dim)
### END YOUR SOLUTION
def forward(self, x: Tensor) -> Tensor:
### BEGIN YOUR SOLUTION
batch_size = x.shape[0]
feature_size = x.shape[1]
# running estimates
mean = x.sum(axes=(0,)) / batch_size
x_minus_mean = x - mean.broadcast_to(x.shape)
print(mean)
print(mean.broadcast_to(x.shape))
var = (x_minus_mean ** 2).sum(axes=(0, )) / batch_size
if self.training:
self.running_mean = (1 - self.momentum) * self.running_mean + self.momentum * mean.data
self.running_var = (1 - self.momentum) * self.running_var + self.momentum * var.data
x_std = ((var + self.eps) ** 0.5).broadcast_to(x.shape)
normed = x_minus_mean / x_std
return normed * self.weight.broadcast_to(x.shape) + self.bias.broadcast_to(x.shape)
else:
normed = (x - self.running_mean) / (self.running_var + self.eps) ** 0.5
return normed * self.weight.broadcast_to(x.shape) + self.bias.broadcast_to(x.shape)
### END YOUR SOLUTION
class LayerNorm1d(Module):
def __init__(self, dim, eps=1e-5, device=None, dtype="float32"):
super().__init__()
self.dim = dim
self.eps = eps
### BEGIN YOUR SOLUTION
self.weight = Parameter(init.ones(dim),requires_grad=True)
self.bias = Parameter(init.zeros(dim),requires_grad=True)
### END YOUR SOLUTION
def forward(self, x: Tensor) -> Tensor:
### BEGIN YOUR SOLUTION
batch_size = x.shape[0]
feature_size = x.shape[1]
mean = x.sum(axes=(1, )).reshape((batch_size, 1)) / feature_size
x_minus_mean = x - mean.broadcast_to(x.shape)
x_std = ((x_minus_mean ** 2).sum(axes=(1, )).reshape((batch_size, 1)) / feature_size + self.eps) ** 0.5
normed = x_minus_mean / x_std.broadcast_to(x.shape)
return self.weight.broadcast_to(x.shape) * normed + self.bias.broadcast_to(x.shape)
### END YOUR SOLUTION
class Dropout(Module):
def __init__(self, p = 0.5):
super().__init__()
self.p = p
def forward(self, x: Tensor) -> Tensor:
### BEGIN YOUR SOLUTION
if self.training:
return x * (init.randb(*x.shape, p=(1 - self.p))) / (1- self.p)
else:
return x
### END YOUR SOLUTION
class Residual(Module):
def __init__(self, fn: Module):
super().__init__()
self.fn = fn
def forward(self, x: Tensor) -> Tensor:
### BEGIN YOUR SOLUTION
return x + self.fn(x)
### END YOUR SOLUTION