-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·144 lines (109 loc) · 3.78 KB
/
main.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
#!/usr/bin/env python3
import logging
from tqdm import tqdm
import numpy as np
import torch
import torch.nn.functional as F
import torch.nn as nn
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import argparse
logger = logging.getLogger(__name__)
class Activation(nn.Module):
def forward(self, x):
return F.relu6(x*10)/6
class Hippocampus(nn.Module):
def hook(self, i, m, inputs, outputs):
x = outputs[0].detach().cpu().numpy().flatten()
if self.neurons[i] is None:
self.neurons[i] = x
else:
self.neurons[i] += x
def reset(self):
for neuron in self.neurons:
neuron.fill(0.)
def __init__(self, *layers):
super(Hippocampus, self).__init__()
def hook_wrapper(module, i):
def do(*args):
return self.hook(i, *args)
module.register_forward_hook(do)
return None
for layer in layers:
layer.weight.data = torch.from_numpy(
np.random.choice((0., 1.), layer.weight.shape, p=(0.9, 0.1))).type(torch.FloatTensor)
self.neurons = [hook_wrapper(module, i)
for i, module in enumerate(layers[1:])]
self.modules = [l for layer in layers for l in [
layer, Activation()]]
def forward(self, x):
for module in self.modules:
x = module(x)
return x
def predict(model, data, neuron_groups):
model.reset()
model(data)
neurons = np.concatenate(model.neurons)
activations = np.array([neurons[neuron_group].sum()
for neuron_group in neuron_groups])
return np.argsort(-activations)
class Tops:
def __init__(self):
self.tops = [0]*10
def __add__(self, arg):
(predictions, gt) = arg
for i, prediction in enumerate(predictions):
if prediction == gt:
self.tops[i] += 1
return self
def __str__(self):
total = sum(self.tops)
return f'error: {(1.-self.tops[0]/total)*100:2f}%'
def main(args):
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
logger.info('Downloading')
training_dataset = datasets.MNIST(
'./data', train=True, download=True, transform=transform)
testing_dataset = datasets.MNIST(
'./data', train=False, transform=transform)
train_loader = torch.utils.data.DataLoader(
training_dataset, shuffle=False, batch_size=1)
test_loader = torch.utils.data.DataLoader(
testing_dataset, shuffle=False, batch_size=1)
model = Hippocampus(
nn.Conv2d(1, 32, 3, 1),
nn.Conv2d(32, 32, 3, 1),
nn.Conv2d(32, 32, 3, 1),
nn.Conv2d(32, 32, 3, 1),
nn.Conv2d(32, 32, 3, 1),
nn.Conv2d(32, 32, 3, 1),
)
# Train
neuron_groups = []
for i in range(10):
for data, target in tqdm(train_loader, leave=False, desc=f'Digit: {i}'):
if i != target.item():
continue
model(data)
neurons = np.concatenate(model.neurons)
p = np.quantile(neurons, args.quantile)
neuron_groups.append(np.argwhere(neurons > p))
model.reset()
# test
tops = Tops()
for data, target in tqdm(test_loader, leave=False, desc='Testing'):
p = predict(model, data, neuron_groups)
tops += (p, target.item())
print(tops)
print(tops.tops)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--quantile', type=float, default=0.95)
loggers = [logging.getLogger(name)
for name in logging.root.manager.loggerDict]
for logger in loggers:
logger.setLevel(logging.INFO)
main(parser.parse_args())