-
Notifications
You must be signed in to change notification settings - Fork 1
/
ars_multiprocess.py
141 lines (99 loc) · 4.06 KB
/
ars_multiprocess.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
import random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from copy import deepcopy
from threading import Lock
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0, 1, 2, 3"
lock = Lock()
batch_size = 64
epochs = 100
lr_rate = 1e-3
import torch.multiprocessing as _mp
mp = _mp.get_context('spawn')
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5, 1)
self.conv2 = nn.Conv2d(20, 50, 5, 1)
self.fc1 = nn.Linear(4*4*50, 500)
self.fc2 = nn.Linear(500, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2, 2)
x = x.view(-1, 4*4*50)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
kwargs = {'num_workers': 1, 'pin_memory': True}
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=True, **kwargs)
prev = np.ones(len(train_loader)) * 10.0
def calculate_loss(model, features, targets):
output = model(features)
return F.nll_loss(output, targets)
def train(checkpoint, index):
global prev
global train_loader
dd = (index % 3) + 1
model = Network().cuda(dd).half()
model.load_state_dict(checkpoint.state_dict())
noise_model = Network().cuda(dd).half()
with torch.no_grad():
selected_batches = np.random.multinomial(int(len(train_loader)*0.5), prev/np.sum(prev))
tsum_loss = 0.0
model.eval()
for i, (batch_features, batch_targets) in enumerate(train_loader):
if (selected_batches[i] == 0): continue
features = batch_features.cuda(dd, non_blocking=True).half()
targets = batch_targets.cuda(dd, non_blocking=True)
for model_param, noise_param in zip(model.parameters(), noise_model.parameters()):
noise = torch.randn(model_param.size()).cuda(dd).half()
#noise_norm = noise.norm()
#if noise_norm != 0.0: noise /= noise_norm
velo = lr_rate * noise
noise_param.data.copy_(velo)
for model_param, noise_param in zip(model.parameters(), noise_model.parameters()):
model_param.add_(noise_param.data)
add_loss = calculate_loss(model, features, targets)
for model_param, noise_param in zip(model.parameters(), noise_model.parameters()):
model_param.sub_(noise_param.data * 2.0)
sub_loss = calculate_loss(model, features, targets)
for model_param, noise_param in zip(model.parameters(), noise_model.parameters()):
model_param.add_(noise_param.data * (1.0 + sub_loss - add_loss))
loss = calculate_loss(model, features, targets)
lock.acquire()
if prev[i] > loss:
checkpoint = deepcopy(model)
prev[i] = loss
print(np.mean(prev))
lock.release()
tsum_loss = tsum_loss + loss.item()
torch.cuda.empty_cache()
train(checkpoint, index)
if __name__ == '__main__':
checkpoint = Network().half().share_memory()
torch.backends.cudnn.benchmark = True
processes = []
for i in range(8):
p = mp.Process(target=train, args=(checkpoint, i))
p.start()
processes.append(p)
for p in processes: p.join()