-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit_functions.py
executable file
·251 lines (201 loc) · 9.24 KB
/
fit_functions.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from data_utils.functions import *
import torch
import numpy as np
import matplotlib.pyplot as plt
import pkbar
from models.Bayes_ReLUKAN import Bayes_ReLUKAN#,Bayes_ReLUKAN_EpiOnly
from loss_utils import Gaussian_likelihood, Student_t_likelihood
import os
from matplotlib.colors import LogNorm
import argparse
import random
def set_seeds_(seed=752022):
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
torch.cuda.manual_seed(seed)
class Experiment():
def __init__(self,generator,width,grid,k,method=None,lkld="Gauss",verbose=False,device='cuda',num_samples=100000,kl_scale=1.0,lr=1e-3):
self.generator = generator
self.width = width
self.grid = grid
self.k = k
self.num_samples = num_samples
self.method = method
self.x_range = [0,1]
self.verbose = verbose
y_ranges = [[-3,4],[-4,4],[-3,6]]
if "f1" in method:
self.y_range = y_ranges[0]
elif "f2" in method:
self.y_range = y_ranges[1]
else:
self.y_range = y_ranges[-1]
if "noise" in method:
print("Using aleatoric and epistemic uncertainty.")
self.bkan = Bayes_ReLUKAN(width,grid,k,aleatoric=True)
else:
print("Using only epistemic uncertainty.")
self.bkan =Bayes_ReLUKAN(width,grid,k,aleatoric=False)
self.kl_scale = kl_scale
self.lr = lr
self.device = device
self.lkld = lkld
self.out_path = "data_"+str(self.lkld)
if self.verbose:
if not os.path.exists(self.out_path):
print("Outputs will be placed in ", str(self.out_path))
os.makedirs(self.out_path)
else:
print("Found existing directory at ",self.out_path,". Overwriting outputs.")
print("Experiment on function: ",str(self.method))
self.input_size = width[0]
self.train_xs = np.random.random([self.num_samples,self.input_size,1])
self.train_ys = self.generator(self.train_xs)
mu_ = self.train_ys.mean()
self.val_xs = np.random.random([self.num_samples // 10,self.input_size,1])
self.val_ys = self.generator(self.val_xs)
self.train_xs = torch.tensor(self.train_xs)
self.train_ys = torch.tensor(self.train_ys)
self.val_xs = torch.tensor(self.val_xs)
self.val_ys = torch.tensor(self.val_ys)
self.train_loss = []
self.val_loss = []
self.test_loss = []
self.opt = torch.optim.Adam(lr=self.lr,params=self.bkan.parameters())
if self.lkld == "Gauss":
self.loss_fun = Gaussian_likelihood
elif self.lkld == "Student":
self.loss_fun = Student_t_likelihood
else:
raise ValueError("Likelihood not implemented. Select: Gauss or Student")
def trainer(self,num_epochs=1000):
epoch = 0
kbar = pkbar.Kbar(target=num_epochs, epoch=epoch, num_epochs=num_epochs, width=20, always_stateful=False)
for epoch in range(num_epochs):
loss,mse,kl_div = self.train()
val_loss,val_mse,val_kl_div = self.validate()
kbar.update(epoch, values=[("loss", loss.item()),("mse",mse.item()),("kl_loss",kl_div.item()),("val_loss", val_loss.item()),("val_mse",val_mse.item()),("val_kl_loss",val_kl_div.item())])
def train(self,):
self.bkan.to(self.device)
self.bkan.train()
self.opt.zero_grad()
if "noise" in self.method:
with torch.set_grad_enabled(True):
pred,log_devs2 = self.bkan(self.train_xs.to(self.device).float())
if self.lkld == "Student":
bnn_loss,mse = self.loss_fun(pred, log_devs2,self.bkan.nu, self.train_ys.to(self.device).float())
else:
bnn_loss,mse = self.loss_fun(pred, log_devs2, self.train_ys.to(self.device).float())
else:
with torch.set_grad_enabled(True):
pred = self.bkan(self.train_xs.to(self.device).float())
bnn_loss = torch.mean((pred - self.train_ys.to(self.device).float())**2)
mse = bnn_loss
kl_div = self.kl_scale * self.bkan.kl_div() / len(self.train_xs)
loss = bnn_loss + kl_div
loss.backward()
self.opt.step()
self.train_loss.append(loss.item())
return loss,mse,kl_div
def validate(self,):
self.bkan.eval()
if "noise" in self.method:
with torch.no_grad():
pred,log_devs2 = self.bkan(self.val_xs.to(self.device).float())
if self.lkld == "Student":
bnn_loss,mse = self.loss_fun(pred, log_devs2,self.bkan.nu,self.val_ys.to(self.device).float())
else:
bnn_loss,mse = self.loss_fun(pred, log_devs2,self.val_ys.to(self.device).float())
else:
with torch.no_grad():
pred = self.bkan(self.val_xs.to(self.device).float())
bnn_loss = torch.mean((pred - self.val_ys.to(self.device).float())**2)
mse = bnn_loss
kl_div = self.kl_scale * self.bkan.kl_div() / len(self.val_xs)
loss = bnn_loss + kl_div
self.test_loss.append(loss.item())
return loss,mse,kl_div
def plot_loss(self,name):
fig = plt.figure(figsize=(8,8))
#plt.title(f'${name}$ training process')
plt.xlabel('iterations')
plt.ylabel('MSE loss')
plt.plot(self.train_loss, '-', color='black', label='train')
plt.plot(self.test_loss, '--', color='black', label='test')
plt.legend()
plt.savefig(os.path.join(self.out_path,f'process_{name}.pdf'), dpi=600)
plt.close()
def plot_results(self, name, mode=1,num_test_samples=20000):
self.bkan.eval()
plt.xlabel('$x$',fontsize=24)
plt.ylabel('$f(x)$',fontsize=24)
xs = np.array([np.arange(0, num_test_samples) / num_test_samples]).T
sigma_=0.1
if 'noise' in name:
ncol = 2
ys,clean_ys = self.generator(xs,return_clean=True,sigma=sigma_)
plt.hist2d(xs.flatten(),ys.flatten(),bins=100,density=True,norm=LogNorm(),range=[self.x_range,self.y_range])
else:
ncol=1
ys = self.generator(xs)
plt.plot(xs,ys,color='k',label='Truth',lw=2)
xs = torch.tensor(xs).to('cuda').float()
if self.device == 'cuda':
torch.cuda.empty_cache()
with torch.no_grad():
if "noise" in self.method:
avg_pred,epistemic,aleatoric = self.bkan.sample(xs.to(self.device).float(),num_samples=10000)
else:
avg_pred,epistemic = self.bkan.sample(xs.to(self.device).float(),num_samples=10000)
if self.device == 'cuda':
torch.cuda.empty_cache()
plt.plot(xs.detach().cpu().numpy(),avg_pred,'--',color='red',label='Avg. Prediction')
plt.fill_between(xs.detach().cpu().numpy().flatten(),avg_pred - epistemic,avg_pred + epistemic, color='blue',alpha=0.3,label='Epistemic')
if "noise" in self.method:
print(" ")
lower = np.percentile(aleatoric,2.5)
upper = np.percentile(aleatoric,97.5)
print("Average Aleatoric: ",np.average(aleatoric)," - 95% Quantiles: ",upper,lower," Interval Length: ",upper - lower)
if self.lkld == "Student":
print("DOF: ",self.bkan.nu.exp().detach().cpu())
print(" ")
quad = np.sqrt(epistemic ** 2 + aleatoric ** 2)
plt.fill_between(xs.detach().cpu().numpy().flatten(),avg_pred - quad,avg_pred + quad, color='red',alpha=0.3,label='Quadrature')
plt.fill_between(xs.detach().cpu().numpy().flatten(),avg_pred - aleatoric,avg_pred + aleatoric, color='k',alpha=0.3,label='Aleatoric')
plt.legend(loc='best',fontsize=14,ncol=ncol)
plt.tick_params(axis='both', which='major', labelsize=14)
plt.ylim(self.y_range)
plt.xlim(self.x_range)
plt.savefig(os.path.join(self.out_path,f'effect_{name}.pdf'), bbox_inches="tight")
plt.close()
train_plan = {
'f1': (f1, [1, 1], 5, 3),
'f1_noise': (f1_noise, [1, 1], 5, 3),
'f2': (f2, [1, 1], 5, 3),
'f2_noise': (f2_noise, [1, 1], 5, 3),
'f3': (f3, [1, 1], 5, 3),
'f3_noise': (f3_noise, [1, 1], 5, 3),
}
if __name__=='__main__':
# PARSE THE ARGS
parser = argparse.ArgumentParser(description='Swin Training')
parser.add_argument('-l', '--lkd', default='Gauss',type=str,
help='Choice of likelihood. Gauss or Student.')
args = parser.parse_args()
LKLD_ = args.lkd
set_seeds_()
for i,f_name in enumerate(train_plan):
if i == 0:
verbose = True
else:
verbose = False
train = Experiment(*train_plan[f_name],method=f_name,lkld=LKLD_,verbose=verbose)
if "noise" in f_name:
num_epochs = 15000
else:
num_epochs = 10000
train.trainer(num_epochs)
train.plot_loss(f_name)
train.plot_results(f_name)
print(" ")