-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmuon.py
More file actions
186 lines (145 loc) · 7.36 KB
/
muon.py
File metadata and controls
186 lines (145 loc) · 7.36 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
from mxnet import gluon,nd,autograd,metric
import matplotlib.pyplot as plt
import numpy as np
class CreateModel(gluon.nn.Block):
def __init__(self,layer,ctx,precision, **kwargs):
super(CreateModel, self).__init__(**kwargs)
self.layer=layer
self.precision=precision
self.ctx=ctx
self.layer.cast(self.precision)
def grad_check_first_layer(self):
print( self.layer[0].weight )
print( self.layer[0].weight.grad().sum() )
def forward(self,x):
#print(x)
return self.layer(x)
def fit(self,train_gen,test_gen,epochs,print_every,
loss_with_softmax,optimizer):
trainer=gluon.Trainer(params=self.collect_params(),
optimizer=optimizer)
# Initialize some objects for the metrics
acc=metric.Accuracy()
train_acc_records=[]
test_acc_records=[]
loss_records=[]
for e in range(epochs):
for i,(data,label) in enumerate(train_gen):
data=data.as_in_context(self.ctx).astype(self.precision)
label=label.as_in_context(self.ctx).astype(np.float32)
with autograd.record():
label_linear=self.layer(data)
label_linear=label_linear.astype(np.float32) # Improve accuracy, as suggested in nVIDIA's SDK.
loss=loss_with_softmax(label_linear,label)
loss.backward()
trainer.step(batch_size=128)
# Print the metrics every several iterations.
if (i%print_every==0): # print metrics for train (current batch) & test data.
label_pred = nd.argmax( nd.softmax(label_linear ), axis=1)
acc.reset()
acc.update(preds=label_pred, labels=label)
train_acc=acc.get()[1]
test_acc =self.evaluate_accuracy(test_gen, self.layer)
train_acc_records.append(train_acc)
test_acc_records.append(test_acc)
curr_loss = nd.mean(loss).asscalar()
loss_records.append(curr_loss)
print("epoch=%2s, iter=%5d, loss=%10f, train acc=%10f, test_acc=%10f"%(e,i,curr_loss,train_acc,test_acc))
# Visialize the calculated metrics of accuracy during of training.
self.viz_training(train_acc_records,test_acc_records,loss_records)
def evaluate_accuracy(self,data_iterator, net):
'''Given model and data, the model accuracy will be calculated.'''
acc = metric.Accuracy()
for i, (data, label) in enumerate(data_iterator):
data = data.as_in_context(self.ctx).astype(self.precision)
label = label.as_in_context(self.ctx).astype(self.precision)
output = net(data)
predictions = nd.argmax(output, axis=1)
acc.update(preds=predictions, labels=label)
return acc.get()[1]
def viz_training(self,train_acc_records,test_acc_records,loss_records):
"""show how the metrics such as loss and model accuracy varies in the progress of training"""
fig,axes=plt.subplots(1,2,figsize=(18,6),dpi=120)
axes[0].plot(train_acc_records,ms=5,marker='o',label='train acc',ls='--')
axes[0].plot(test_acc_records,ms=5,marker='o',label='val acc',ls='--')
axes[0].legend()
axes[1].plot(loss_records,ms=5,marker='o',label='train loss',ls='--')
axes[1].legend()
for idx,ax in enumerate(axes):
ax.set_xlabel('Epoch')
if idx==0:
ax.set_ylabel('Accuracy')
else:
ax.set_ylabel('Loss')
plt.show()
class CreateHybridModel(gluon.nn.HybridBlock):
def __init__(self,layer,ctx,precision, **kwargs):
super(CreateHybridModel, self).__init__(**kwargs)
self.layer=layer
self.precision=precision
self.ctx=ctx
self.layer.cast(self.precision)
def grad_check_first_layer(self):
print( self.layer[0].weight )
print( self.layer[0].weight.grad().sum() )
def hybrid_forward(self,F,x):
#print(x)
return self.layer(x)
def fit(self,train_gen,test_gen,epochs,print_every,
loss_with_softmax,optimizer):
trainer=gluon.Trainer(params=self.collect_params(),
optimizer=optimizer)
# Initialize some objects for the metrics
acc=metric.Accuracy()
train_acc_records=[]
test_acc_records=[]
loss_records=[]
for e in range(epochs):
for i,(data,label) in enumerate(train_gen):
data=data.as_in_context(self.ctx).astype(self.precision)
label=label.as_in_context(self.ctx).astype(np.float32)
with autograd.record():
label_linear=self.layer(data)
label_linear=label_linear.astype(np.float32) # Improve accuracy, as suggested in nVIDIA's SDK.
loss=loss_with_softmax(label_linear,label)
loss.backward()
trainer.step(batch_size=128)
# Print the metrics every several iterations.
if (i%print_every==0): # print metrics for train (current batch) & test data.
label_pred = nd.argmax( nd.softmax(label_linear ), axis=1)
acc.reset()
acc.update(preds=label_pred, labels=label)
train_acc=acc.get()[1]
test_acc =self.evaluate_accuracy(test_gen, self.layer)
train_acc_records.append(train_acc)
test_acc_records.append(test_acc)
curr_loss = nd.mean(loss).asscalar()
loss_records.append(curr_loss)
print("epoch=%2s, iter=%5d, loss=%10f, train acc=%10f, test_acc=%10f"%(e,i,curr_loss,train_acc,test_acc))
# Visialize the calculated metrics of accuracy during of training.
self.viz_training(train_acc_records,test_acc_records,loss_records)
def evaluate_accuracy(self,data_iterator, net):
'''Given model and data, the model accuracy will be calculated.'''
acc = metric.Accuracy()
for i, (data, label) in enumerate(data_iterator):
data = data.as_in_context(self.ctx).astype(self.precision)
label = label.as_in_context(self.ctx).astype(self.precision)
output = net(data)
predictions = nd.argmax(output, axis=1)
acc.update(preds=predictions, labels=label)
return acc.get()[1]
def viz_training(self,train_acc_records,test_acc_records,loss_records):
"""show how the metrics such as loss and model accuracy varies in the progress of training"""
fig,axes=plt.subplots(1,2,figsize=(18,6),dpi=120)
axes[0].plot(train_acc_records,ms=5,marker='o',label='train acc',ls='--')
axes[0].plot(test_acc_records,ms=5,marker='o',label='val acc',ls='--')
axes[0].legend()
axes[1].plot(loss_records,ms=5,marker='o',label='train loss',ls='--')
axes[1].legend()
for idx,ax in enumerate(axes):
ax.set_xlabel('Epoch')
if idx==0:
ax.set_ylabel('Accuracy')
else:
ax.set_ylabel('Loss')
plt.show()