-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnistExampleNNSAE.py
210 lines (175 loc) · 7.31 KB
/
mnistExampleNNSAE.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
import os
import argparse
import math
import NNSAE as nn
import torch
from torchvision import datasets, transforms
from plotImagesOnGrid import plotImagesOnGrid, plotPaperPlot
from numpy.matlib import zeros
import numpy as np
fileName = 'mnistExampleNNSAE.pt'
def set_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
def regression(args):
print(args)
use_cuda = args.cuda and torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
batch_size = args.batch_size
test_batch_size = args.batch_size
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor()
])),
batch_size=batch_size, shuffle=True, drop_last=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, transform=transforms.Compose([
transforms.ToTensor()
])), batch_size=test_batch_size, shuffle=True, drop_last=True, **kwargs)
numEpochs = args.epochs # number of sweeps through data for learning
lrateRO = args.l_rate # learning rate for synaptic plasticity of the read-out layer
lrateIP = args.ip_l_rate # learning rate for intrinsic plasticity
meanIP = args.mean_ip
width = 28 # mnist
inpDim = width**2
netDim = args.hidden_dim
# network creation
net = nn.Nnsae(inpDim, netDim, batch_size)
net.to(device)
bpdc = nn.BackpropagationDecoralation(
[net.weights], [net.h], lrateRO=lrateRO
)
bpdc
loss_fkt = torch.nn.modules.MSELoss(reduction='mean')
net.lrateRO = lrateRO
net.lrateIP = lrateIP
numBatches = len(train_loader)
if not os.path.isfile(fileName) or not args.only_eval:
# training
for e in range(1, numEpochs+1):
gl_loss = 0
numSamples = 0
for i, data in enumerate(train_loader):
# bpdc.zero_grad()
with torch.no_grad():
inp = data[0].view(data[0].shape[0], width**2).to(device)
numSamples += data[0].shape[0]
# forward path
out = net(inp.t()).t()
# calculate loss
loss = loss_fkt(inp, out)
# loss.backward()
# bpdc.step()
net.bpdc((inp-out).t())
# non negative constraint
net.weights.data[net.weights < 0] = 0
# intrinsic plasticity
net.ip()
# log loss
gl_loss += loss.item()
# print(f'epoch ({e}\{numEpochs}) loss {gl_loss/numSamples}')
print('epoch ({}\{}) loss {}'.format(e, numEpochs, gl_loss/numSamples))
net.save_state_dict(fileName)
else:
net.load_state_dict(torch.load(fileName))
print(net)
################## Evaluation ###########################
# evaluation of basis images
threshold = 0.1 # parameter for analysis of weights
# sort basis images for visualization
cnt = 0
unused = []
w = net.weights.t().detach().to('cpu').numpy()
v = zeros((w.shape))
for i in range(netDim):
if w[i, :].max() > threshold: # this basis image is "used"
v[cnt, :] = w[i, :]
cnt = cnt + 1
else:
unused.append(i)
for i in range(len(unused)):
v[cnt+i, :] = w[unused[i], :]
print('used neurons = {}/{}'.format(cnt, netDim))
################## Plotting ############################
# plotting
numCols = 5
if netDim >= 50:
numCols = 10
if not args.no_plot:
plotImagesOnGrid(v, int(math.ceil(netDim/numCols)), numCols, width,
width, range(netDim), './fig/NNSAE-bars-%d-basis.png' % (netDim))
gl_loss = 0
numSamples = 0
net.eval()
orig = []
approx = []
activations = []
with torch.no_grad():
num = 0
for i, data in enumerate(test_loader):
inp = data[0].view(data[0].shape[0], width**2).to(device)
label = data[1]
numSamples += data[0].shape[0]
# forward path
out = net(inp.t()).t()
# calculate loss
loss = loss_fkt(inp, out)
label_list = label.tolist()
while num in label_list:
num_index = label_list.index(num)
orig.append(inp[num_index, :].to('cpu'))
approx.append(out[num_index, :].to('cpu'))
activations.append(net.h.t()[num_index, :].to('cpu'))
num += 1
gl_loss += loss.item()
print('Evaluation loss MSE: {}'.format(gl_loss/numSamples))
if not args.no_plot:
plotPaperPlot(orig, approx, activations, width, width)
def add_model_args(parser):
"""model arguments."""
group = parser.add_argument_group('model', 'model configurations')
group.add_argument('-hid', '--hidden-dim', type=int, default=100,
metavar='N',
help='number of neurons in hidden layer (default: 100')
return parser
def add_training_args(parser):
"""Training arguments."""
group = parser.add_argument_group('train', 'training configurations')
group.add_argument('-lr', '--l_rate', type=float, default=0.01,
metavar='FLOAT', help='synaptic learning rate (default: 0.01)')
group.add_argument('-ip_lr', '--ip_l_rate', type=float, default=0.0001,
metavar='FLOAT', help='intrinsic plasticity learning rate (default: 0.0001)')
group.add_argument('-mip', '--mean_ip', type=float, default=0.2,
metavar='FLOAT', help='mean activity of one hidden node (default: 0.2)')
group.add_argument('--batch-size', type=int, default=64, metavar='N',
help='input batch size for training (default: 64)')
group.add_argument('-e', '--epochs', type=int, default=100, metavar='N',
help='number of epochs to train (default: 100)')
return parser
def get_args():
parser = argparse.ArgumentParser(description='PyTorch NNSAE Model')
group = parser.add_argument_group('setup', 'setup configurations')
group.add_argument('--seed', type=int, default=1234,
metavar='N',
help='Random seed for reproducability.')
group.add_argument('--cuda', action='store_true', default=False,
help='Run on GPU cuda accelerated')
group.add_argument('--gpu', type=int, default=0,
help='used gpu cuda:?', metavar='ID')
group.add_argument('--no-plot', action='store_true', default=False,
help='Show plot in interactive mode.')
group.add_argument('--only-eval', action='store_true', default=False,
help='Use saved Net to reproduce plots and evaluation.')
parser = add_model_args(parser)
parser = add_training_args(parser)
args = parser.parse_args()
return args
def execute_training():
args = get_args()
set_seed(args.seed)
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu)
regression(args)
if __name__ == "__main__":
execute_training()