-
Notifications
You must be signed in to change notification settings - Fork 139
/
main.py
130 lines (103 loc) · 4.07 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
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from tqdm import tqdm
from torch.optim import Adam
from torchvision.datasets import MNIST
from torchvision.transforms import Compose, ToTensor, Normalize, Lambda
from torch.utils.data import DataLoader
def MNIST_loaders(train_batch_size=50000, test_batch_size=10000):
transform = Compose([
ToTensor(),
Normalize((0.1307,), (0.3081,)),
Lambda(lambda x: torch.flatten(x))])
train_loader = DataLoader(
MNIST('./data/', train=True,
download=True,
transform=transform),
batch_size=train_batch_size, shuffle=True)
test_loader = DataLoader(
MNIST('./data/', train=False,
download=True,
transform=transform),
batch_size=test_batch_size, shuffle=False)
return train_loader, test_loader
def overlay_y_on_x(x, y):
"""Replace the first 10 pixels of data [x] with one-hot-encoded label [y]
"""
x_ = x.clone()
x_[:, :10] *= 0.0
x_[range(x.shape[0]), y] = x.max()
return x_
class Net(torch.nn.Module):
def __init__(self, dims):
super().__init__()
self.layers = []
for d in range(len(dims) - 1):
self.layers += [Layer(dims[d], dims[d + 1]).cuda()]
def predict(self, x):
goodness_per_label = []
for label in range(10):
h = overlay_y_on_x(x, label)
goodness = []
for layer in self.layers:
h = layer(h)
goodness += [h.pow(2).mean(1)]
goodness_per_label += [sum(goodness).unsqueeze(1)]
goodness_per_label = torch.cat(goodness_per_label, 1)
return goodness_per_label.argmax(1)
def train(self, x_pos, x_neg):
h_pos, h_neg = x_pos, x_neg
for i, layer in enumerate(self.layers):
print('training layer', i, '...')
h_pos, h_neg = layer.train(h_pos, h_neg)
class Layer(nn.Linear):
def __init__(self, in_features, out_features,
bias=True, device=None, dtype=None):
super().__init__(in_features, out_features, bias, device, dtype)
self.relu = torch.nn.ReLU()
self.opt = Adam(self.parameters(), lr=0.03)
self.threshold = 2.0
self.num_epochs = 1000
def forward(self, x):
x_direction = x / (x.norm(2, 1, keepdim=True) + 1e-4)
return self.relu(
torch.mm(x_direction, self.weight.T) +
self.bias.unsqueeze(0))
def train(self, x_pos, x_neg):
for i in tqdm(range(self.num_epochs)):
g_pos = self.forward(x_pos).pow(2).mean(1)
g_neg = self.forward(x_neg).pow(2).mean(1)
# The following loss pushes pos (neg) samples to
# values larger (smaller) than the self.threshold.
loss = torch.log(1 + torch.exp(torch.cat([
-g_pos + self.threshold,
g_neg - self.threshold]))).mean()
self.opt.zero_grad()
# this backward just compute the derivative and hence
# is not considered backpropagation.
loss.backward()
self.opt.step()
return self.forward(x_pos).detach(), self.forward(x_neg).detach()
def visualize_sample(data, name='', idx=0):
reshaped = data[idx].cpu().reshape(28, 28)
plt.figure(figsize = (4, 4))
plt.title(name)
plt.imshow(reshaped, cmap="gray")
plt.show()
if __name__ == "__main__":
torch.manual_seed(1234)
train_loader, test_loader = MNIST_loaders()
net = Net([784, 500, 500])
x, y = next(iter(train_loader))
x, y = x.cuda(), y.cuda()
x_pos = overlay_y_on_x(x, y)
rnd = torch.randperm(x.size(0))
x_neg = overlay_y_on_x(x, y[rnd])
for data, name in zip([x, x_pos, x_neg], ['orig', 'pos', 'neg']):
visualize_sample(data, name)
net.train(x_pos, x_neg)
print('train error:', 1.0 - net.predict(x).eq(y).float().mean().item())
x_te, y_te = next(iter(test_loader))
x_te, y_te = x_te.cuda(), y_te.cuda()
print('test error:', 1.0 - net.predict(x_te).eq(y_te).float().mean().item())