-
Notifications
You must be signed in to change notification settings - Fork 0
/
z2z.py
189 lines (153 loc) · 6.24 KB
/
z2z.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
#%%
import torch
import torch.optim
import torch.nn.functional as F
import numpy as np
from tqdm import tqdm
import copy
import wandb
import importlib
from data import load_semi_MNIST
import model_class as mod
importlib.reload(mod)
#%%
config = {'input_dim' : 28*28,
'hidden_dim' : 500,
'latent_dim' : 50,
'batch_size' : 500,
'labelled_size' : 100,
'epochs' : 1000,
'lr' : 0.0003,
'best_loss' : 10**9,
'patience_limit' : 100}
# set seed
torch.manual_seed(23)
#%%
wb_log = True
if wb_log: wandb.init(project="ACC", name='z2z', config=config)
is_cuda = torch.cuda.is_available()
device = torch.device('cuda' if is_cuda else 'cpu')
print('Current cuda device is', device)
#%%
labelled, unlabelled, label_validation, unlabel_validation, test_loader = load_semi_MNIST(config['batch_size'],
config['labelled_size'],
seed_value=23)
#%%
def kld(mu, logvar):
kl = 0.5 * (mu**2 + logvar.exp() - logvar - 1)
return torch.sum(kl, dim=-1)
def log_prior(p):
prior = F.softmax(torch.ones_like(p), dim=-1)
prior.requires_grad = False
cross_entropy = -torch.sum(p * torch.log(prior), dim = -1)
return cross_entropy
def elbo(x, mu_de, logvar_de , mu, logvar, label):
kl_div = kld(mu, logvar)
reconst_loss = 0.5*torch.sum(((x-mu_de)**2)/torch.exp(logvar_de)+logvar_de+1.84, dim = -1)
prior = log_prior(label)
L = kl_div + reconst_loss + prior
return L
def onehot(digit):
vector = torch.zeros(10)
vector[digit] = 1
return vector
def loss_function(x, label, u, model):
# labelled data loss
x_reconst, mu_de, logvar_de, mu, logvar = model(x, label)
L = torch.mean(elbo(x, mu_de, logvar_de , mu, logvar, label))
# unlabelled data loss
u_prob = model.classify(u)
temp_label = torch.cat([F.one_hot(torch.zeros(len(u)).long() + i, num_classes=10) for i in range(10)], dim=0).float().to(device)
extend_u = u.repeat(10, 1)
u_reconst, u_mu_de, u_logvar_de, u_mu, u_logvar = model(extend_u, temp_label)
u_elbo = elbo(extend_u, u_mu_de, u_logvar_de, u_mu, u_logvar, temp_label)
u_elbo = u_elbo.view_as(u_prob.t()).t()
U = torch.sum(torch.mul(u_prob, u_elbo), dim = -1)
H = torch.sum(torch.mul(u_prob, torch.log(u_prob + 1e-8)), dim = -1)
J = L + torch.mean(U + H)
#Classification loss
prob = model.classify(x)
classification_loss = -torch.sum(label * torch.log(prob + 1e-8), dim=1).mean()*0.1*config['labelled_size']
loss = J + classification_loss
return loss
#%%
M1 = torch.jit.load('M1.pt')
M1 = M1.to(device)
M1.eval()
model = mod.VAE123(x_dim=50, h_dim = config['hidden_dim'], z_dim = config['latent_dim']).to(device)
# optimizer = torch.optim.RMSprop(model.parameters(), lr = config['lr'], momentum=0.1)
optimizer = torch.optim.Adam(model.parameters(), lr=3e-4, betas=(0.9, 0.999))
#%%
img_size = config['input_dim']
best_acc = 0
patience_limit = config['patience_limit']
patience_check = 0 # 현재 몇 epoch 연속으로 loss 개선이 안되는지를 기록
val = []
for epoch in tqdm(range(config['epochs'])):
model.train()
train_loss = 0
for (x, target), (u, _) in zip(labelled, unlabelled):
# data processing
label = torch.stack([onehot(i) for i in target]).to(device)
x = x.view(-1, img_size).to(device)
u = u.view(-1, img_size).to(device)
x, _, _ = M1.encoder(x)
u, _, _ = M1.encoder(u)
loss = loss_function(x, label, u, model)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss += loss.item()
print('Epoch: {} Train_Loss: {} :'.format(epoch, train_loss/len(labelled)))
model.eval()
val_loss = 0
with torch.no_grad():
for (x, target), (u, _) in zip(label_validation, unlabel_validation):
label = torch.stack([onehot(i) for i in target]).to(device)
x = x.view(-1, img_size).to(device)
u = u.view(-1, img_size).to(device)
x, _, _ = M1.encoder(x)
u, _, _ = M1.encoder(u)
loss = loss_function(x, label, u, model)
val_loss += loss/len(label_validation)
val.append(val_loss)
print(epoch, val_loss)
# if abs(val_loss - best_loss) < 1e-3: # loss가 개선되지 않은 경우
# patience_check += 1
# if patience_check >= patience_limit: # early stopping 조건 만족 시 조기 종료
# print("Learning End. Best_Loss:{:6f}".format(best_loss))
# break
# else: # loss가 개선된 경우
# best_loss = val_loss
# best_model = copy.deepcopy(model)
# patience_check = 0
accuracy = 0
for x, label in test_loader:
x = x.view(-1, img_size).to(device)
x, _, _ = M1.encoder(x)
pred_idx = torch.argmax(model.classify(x), dim=-1)
accuracy += torch.mean((pred_idx.data.to(device) == label.to(device)).float())
print(f'{accuracy.item()/len(test_loader)*100:.2f}%')
if wb_log: wandb.log({'train_loss':train_loss/len(labelled), 'valid_loss': val_loss, 'Accuracy': accuracy.item()/len(test_loader)*100})
if accuracy < best_acc: # loss가 개선되지 않은 경우
patience_check += 1
if patience_check >= patience_limit: # early stopping 조건 만족 시 조기 종료
print("Learning End. Best_ACC:{:6f}".format(best_acc.item()/len(test_loader)*100))
# break
else: # loss가 개선된 경우
best_acc = accuracy
best_model = copy.deepcopy(model)
patience_check = 0
# %%
with torch.no_grad():
model = best_model.to('cpu')
M1 = M1.to('cpu')
accuracy = 0
for x, label in test_loader:
x = x.view(-1, img_size)
x, _, _ = M1.encoder(x)
pred_idx = torch.argmax(model.classify(x), dim=-1)
accuracy += torch.mean((pred_idx.data == label).float())
print(f'{accuracy.item()/len(test_loader)*100:.2f}%')
if wb_log: wandb.log({"Accuracy": accuracy.item()/len(test_loader)*100})
# %%