-
Notifications
You must be signed in to change notification settings - Fork 1
/
CIFARtraining.py
148 lines (112 loc) · 5.06 KB
/
CIFARtraining.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
import torch
import torch.nn.functional as F
from itertools import chain
from tqdm.autonotebook import tqdm
import numpy as np
from torch import optim
from device import CUDADEVICE
import pandas as pd
from scipy.stats import loguniform
def sample(mu, logvar):
std = torch.exp(0.5*logvar) # e^(1/2 * log(std^2))
eps = torch.randn_like(std) # random ~ N(0, 1)
return eps.mul(std).add_(mu)
def trainBVAEFixedCIFAR(trainloader, encoder, decoder, beta=1, epochs=100, training_dir='./trainingResults/CIFAR10FixedWeights.csv', enc_dir='./weights/encCIFAR10FixedWeights.weights', dec_dir='./weights/decCIFAR10FixedWeights.weights'):
# parameters
beta = beta
nEpoch = epochs
KL_loss = []
recon_loss = []
full_loss = []
# construct the encoder, decoder and optimiser
enc = encoder.to(CUDADEVICE)
dec = decoder.to(CUDADEVICE)
optimizer = optim.Adam(chain(enc.parameters(), dec.parameters()), lr=1e-4, weight_decay=1e-5)
for epoch in range(nEpoch):
losses = []
trainloader = tqdm(trainloader)
for i, data in enumerate(trainloader, 0):
inputs, _ = data
inputs, _ = inputs.to(CUDADEVICE), _.to(CUDADEVICE)
optimizer.zero_grad()
mu, log_sigma2 = enc(inputs)
z = sample(mu, log_sigma2)
outputs = dec(z)
recon = F.binary_cross_entropy(outputs, inputs, reduction='sum') / inputs.shape[0]
kl_diverge = 0.5 * torch.mean(
torch.pow(mu, 2) + torch.pow(log_sigma2, 2) - torch.log(torch.pow(log_sigma2, 2)) - 1)
loss = recon + beta * kl_diverge
loss.backward()
optimizer.step()
# keep track of the loss and update the stats
losses.append(loss.item())
trainloader.set_postfix(loss=np.mean(losses), epoch=epoch)
KL_loss.append(kl_diverge.data)
recon_loss.append(recon.data)
full_loss.append(loss.data)
KL_numpy = []
recon_np = []
full_np = []
for i in range(len(KL_loss)):
KL_numpy.append(KL_loss[i].data.cpu().detach().numpy())
recon_np.append(recon_loss[i].data.cpu().detach().numpy())
full_np.append(full_loss[i].data.cpu().detach().numpy())
data = {'KL_numpy': KL_numpy, 'recon_np': recon_np, 'full_np': full_np}
# save the training loss values
df = pd.DataFrame.from_dict(data)
df.to_csv(training_dir, index=False)
# save the model weights
torch.save(enc.state_dict(), enc_dir)
torch.save(dec.state_dict(), dec_dir)
return KL_numpy, recon_np, full_np
def trainBVAEYotoCIFAR(trainloader, encoder, decoder, epochs=100, training_dir='./trainingResults/CIFAR10Yoto.csv', enc_dir='./weights/encCIFAR10Yoto.weights', dec_dir='./weights/decCIFAR10Yoto.weights'):
# parameters
nEpoch = epochs
# construct the encoder, decoder and optimiser
enc = encoder.to(CUDADEVICE)
dec = decoder.to(CUDADEVICE)
optimizer = optim.Adam(chain(enc.parameters(), dec.parameters()), lr=1e-4, weight_decay=1e-5)
KL_loss = []
recon_loss = []
full_loss = []
for epoch in range(nEpoch):
losses = []
trainloader = tqdm(trainloader)
for i, data in enumerate(trainloader, 0):
inputs, _ = data
inputs, _ = inputs.to(CUDADEVICE), _.to(CUDADEVICE)
optimizer.zero_grad()
beta_initial = loguniform.rvs(0.125, 512, size=1)
beta = np.float32(beta_initial[0]).tolist()
beta2 = torch.tensor([1 * beta], requires_grad=False)
beta2 = torch.broadcast_to(beta2, (1, 256)).to(CUDADEVICE)
mu, log_sigma2 = enc(inputs, beta2)
z = sample(mu, log_sigma2)
outputs = dec(z, beta2)
recon = F.binary_cross_entropy(outputs, inputs, reduction='sum') / inputs.shape[0]
kl_diverge = 0.5 * torch.mean(
torch.pow(mu, 2) + torch.pow(log_sigma2, 2) - torch.log(torch.pow(log_sigma2, 2)) - 1)
loss = recon + beta * kl_diverge
loss.backward()
optimizer.step()
# keep track of the loss and update the stats
losses.append(loss.item())
trainloader.set_postfix(loss=np.mean(losses), epoch=epoch)
KL_loss.append(kl_diverge.data)
recon_loss.append(recon.data)
full_loss.append(loss.data)
KL_numpy = []
recon_np = []
full_np = []
for i in range(len(KL_loss)):
KL_numpy.append(KL_loss[i].data.cpu().detach().numpy())
recon_np.append(recon_loss[i].data.cpu().detach().numpy())
full_np.append(full_loss[i].data.cpu().detach().numpy())
data = {'KL_numpy': KL_numpy, 'recon_np': recon_np, 'full_np': full_np}
#save the training loss values
df = pd.DataFrame.from_dict(data)
df.to_csv(training_dir, index=False)
#save the model weights
torch.save(enc.state_dict(), enc_dir)
torch.save(dec.state_dict(), dec_dir)
return KL_numpy, recon_np, full_np