-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtrain_crossover_weight_space.py
More file actions
146 lines (96 loc) · 3.54 KB
/
train_crossover_weight_space.py
File metadata and controls
146 lines (96 loc) · 3.54 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
from random import uniform
import torch
from torch import nn, tensor, randn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from torch.optim import Adam
import torchvision
import torchvision.transforms as T
from einops.layers.torch import Rearrange
from einops import repeat, rearrange
from evolutionary_policy_optimization.experimental import PopulationWrapper
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def divisible_by(num, den):
return (num % den) == 0
#data
class MnistDataset(Dataset):
def __init__(self, train):
self.mnist = torchvision.datasets.MNIST('./data/mnist', train = train, download = True)
def __len__(self):
return len(self.mnist)
def __getitem__(self, idx):
pil, labels = self.mnist[idx]
digit_tensor = T.PILToTensor()(pil)
return (digit_tensor / 255.).float().to(device), tensor(labels, device = device)
batch = 32
train_dataset = MnistDataset(train = True)
dl = DataLoader(train_dataset, batch_size = batch, shuffle = True, drop_last = True)
eval_dataset = MnistDataset(train = False)
eval_dl = DataLoader(eval_dataset, batch_size = batch, shuffle = True, drop_last = True)
def cycle(dl):
while True:
for batch in dl:
yield batch
# network
net = nn.Sequential(
Rearrange('... c h w -> ... (c h w)'),
nn.Linear(784, 64, bias = False),
nn.ReLU(),
nn.Linear(64, 10, bias = False),
).to(device)
# regular gradient descent
optim = Adam(net.parameters(), lr = 1e-3)
iter_train_dl = cycle(dl)
iter_eval_dl = cycle(eval_dl)
for i in range(1000):
data, labels = next(iter_train_dl)
logits = net(data)
loss = F.cross_entropy(logits, labels)
loss.backward()
print(f'{i}: {loss.item():.3f}')
optim.step()
optim.zero_grad()
if divisible_by(i + 1, 100):
with torch.no_grad():
eval_data, labels = next(iter_eval_dl)
logits = net(eval_data)
eval_loss = F.cross_entropy(logits, labels)
total = labels.shape[0]
correct = (logits.argmax(dim = -1) == labels).long().sum().item()
print(f'{i}: eval loss: {eval_loss.item():.3f}')
print(f'{i}: accuracy: {correct} / {total}')
# periodic crossover from genetic algorithm on population of networks
# pop stands for population
pop_size = 100
learning_rate = 3e-4
pop_net = PopulationWrapper(
net,
pop_size = pop_size,
num_selected = 25,
tournament_size = 5,
learning_rate = 1e-3
)
optim = Adam(pop_net.parameters(), lr = learning_rate)
for i in range(1000):
pop_net.train()
data, labels = next(iter_train_dl)
losses = pop_net(data, labels = labels)
losses.sum(dim = 0).mean().backward()
print(f'{i}: loss: {losses.mean().item():.3f}')
optim.step()
optim.zero_grad()
# evaluate
if divisible_by(i + 1, 100):
with torch.no_grad():
pop_net.eval()
eval_data, labels = next(iter_eval_dl)
eval_loss, logits = pop_net(eval_data, labels = labels, return_logits_with_loss = True)
total = labels.shape[0] * pop_size
correct = (logits.argmax(dim = -1) == labels).long().sum().item()
print(f'{i}: eval loss: {eval_loss.mean().item():.3f}')
print(f'{i}: accuracy: {correct} / {total}')
# genetic algorithm on population
fitnesses = 1. / eval_loss
pop_net.genetic_algorithm_step_(fitnesses)
# new optim
optim = Adam(pop_net.parameters(), lr = learning_rate)