-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMlpBlock.py
More file actions
198 lines (156 loc) · 4.88 KB
/
MlpBlock.py
File metadata and controls
198 lines (156 loc) · 4.88 KB
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
from mxnet import nd, init
from mxnet.gluon import nn
class MLP(nn.Block):
# initialize model layer parameters, with two full connected layer
def __init__(self, **kwargs):
super(MLP, self).__init__(**kwargs)
self.hidden = nn.Dense(256, activation='relu')
self.output = nn.Dense(10)
# model forward calculation
def forward(self, x):
return self.output(self.hidden(x))
class MySequential(nn.Block):
def __init__(self, **kwargs):
super(MySequential, self).__init__(**kwargs)
def add(self, block):
self._children[block.name] = block
def forward(self, x):
for block in self._children.values():
x = block(x)
return x
class FancyMLP(nn.Block):
def __init__(self, **kwargs):
super(FancyMLP, self).__init__(**kwargs)
self.rand_weight = self.params.get_constant(
'rand_weight', nd.random.uniform(shape=(20, 20)))
self.dense = nn.Dense(20, activation='relu')
def forward(self, x):
x = self.dense(x)
x = nd.relu(nd.dot(x, self.rand_weight.data()) + 1)
x = self.dense(x)
while (x.norm().asscalar() > 1):
t = x.norm().asscalar()
# print('t', t)
x /= 2
if x.norm().asscalar() < 0.8:
x *= 10
return x.sum()
class NestMLP(nn.Block):
def __init__(self, **kwargs):
super(NestMLP, self).__init__(**kwargs)
self.net = nn.Sequential()
self.net.add(nn.Dense(64, activation='relu'),
nn.Dense(32, activation='relu'))
self.dense = nn.Dense(16, activation='relu')
def forward(self, x):
return self.dense(self.net(x))
class MyInit(init.Initializer):
def _init_weight(self, name, data):
print("Init", name, data.shape)
data[:] = nd.random.uniform(low=-10, high=10, shape=data.shape)
data *= data.abs() >= 5
class CneteredLayer(nn.Block):
def __init__(self, **kwargs):
super(CneteredLayer, self).__init__(**kwargs)
def forward(self, x):
return x - x.mean()
class MyDense(nn.Block):
# units is the number of output, in_units is the number of input
def __init__(self, units, in_units, **kwargs):
super(MyDense, self).__init__(**kwargs)
self.weight = self.params.get('weight', shape=(in_units, units))
self.bias = self.params.get('bias', shape=(units,))
def forward(self, x):
# print('x', x)
# print('weight', self.weight.data())
# print('bias', self.bias)
linear = nd.dot(x, self.weight.data()) + self.bias.data()
return nd.relu(linear)
def Method0():
x = nd.random.uniform(shape=(2, 20))
net = MLP()
net.initialize()
o = net(x)
print(o)
def Method1():
x = nd.random.uniform(shape=(2, 20))
net = MySequential()
net.add(nn.Dense(256, activation='relu'))
net.add(nn.Dense(10))
net.initialize()
o = net(x)
print(o)
def Method2():
x = nd.random.uniform(shape=(2, 20))
net = FancyMLP()
net.initialize()
o = net(x)
print(o)
def Method3():
x = nd.random.uniform(shape=(2, 20))
net = nn.Sequential()
net.add(NestMLP(), nn.Dense(20), FancyMLP())
net.initialize()
o = net(x)
print(o)
def Method4():
net = nn.Sequential()
net.add(nn.Dense(256, activation='relu'))
net.add(nn.Dense(10))
#net.initialize()
net.initialize(MyInit(), force_reinit=True)
x = nd.random.uniform(shape=(2, 20))
y = net(x)
net[0].weight.data()[0]
def Method5():
net = nn.Sequential()
shared = nn.Dense(8, activation='relu')
net.add(nn.Dense(8, activation='relu'),
shared,
nn.Dense(8, activation='relu', params=shared.params),
nn.Dense(10))
net.initialize()
x = nd.random.uniform(shape=(2, 20))
net(x)
print(net[1].weight.data()[0] == net[2].weight.data()[0])
def Method6():
net = nn.Sequential()
net.add(nn.Dense(256, activation='relu'),
nn.Dense(10))
net.initialize(init=MyInit()) # delay initialization
x = nd.random.uniform(shape=(2, 20))
y = net(x)
def Method7():
layer = CneteredLayer()
y = layer(nd.array([1, 2, 3, 4, 5]))
print(y)
net = nn.Sequential()
net.add(nn.Dense(128), CneteredLayer())
net.initialize()
y = net(nd.random.uniform(shape=(4, 8)))
y1 = y.mean().asscalar()
print(y1)
def Method8():
dense = MyDense(units=3, in_units=5)
print(dense.params)
dense.initialize()
y = dense(nd.random.uniform(shape=(2, 5)))
print(y)
net = nn.Sequential()
net.add(MyDense(8, in_units=64))
net.add(MyDense(1, in_units=8))
net.initialize()
y = net(nd.random.uniform(shape=(2, 64)))
print(y)
def main():
# Method0()
# Method1()
# Method2()
# Method3()
# Method4()
# Method5()
# Method6()
# Method7()
Method8()
if __name__=='__main__':
main()