-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_plot_BO.py
More file actions
1309 lines (1069 loc) · 47.4 KB
/
Copy pathfunctions_plot_BO.py
File metadata and controls
1309 lines (1069 loc) · 47.4 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import matplotlib.pyplot as plt
import itertools
import random
import time
from tqdm import tqdm
from scipy import integrate
from scipy.stats import multivariate_normal,norm
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from scipy import integrate
from scipy.stats import multivariate_normal,norm
from sklearn.datasets import load_iris
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.optimizers import Adam
from sklearn.datasets import fetch_openml
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
import torch
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, random_split
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
################################## Utilities ##################################
def mu(k, Sigma, y):
"""
Mean of the marginal distribution of f(x)
Parameters
----------
k : array_like
kernel vector w.r.t. the previous observations
Sigma : array_like
inverse of covariance matrix of the previous observations
y : array_like
previous observations
Returns
-------
float
mean of the marginal distribution of f(x)
"""
return k.T @ Sigma @ y
def generate_combinations(dictionary):
"""
Generate all combinations of values in a dictionary
"""
keys = list(dictionary.keys())
value_lists = [dictionary[key] for key in keys]
combinations = list(itertools.product(*value_lists))
return combinations
def sigma(Kx, k, Sigma):
"""
Variance of the marginal distribution of f(x)
Parameters
----------
Kx : array_like
kernel vector w.r.t. the new observation K(x, x)
k : array_like
kernel vector w.r.t. the previous observations
Sigma : array_like
inverse of covariance matrix of the previous observations
Returns
-------
float
variance of the marginal distribution of f(x)
"""
return Kx - k.T @ Sigma @ k
def kernel_M52(x1, x2, l=None, s=1):
"""
Matern 5/2 kernel
Parameters
----------
x1 : array_like
First input
x2 : array_like
Second input
l : array_like
Length scales
s : float
Signal variance
"""
if l is None:
l = np.ones(len(x1))
# check if l and x have the same dimension
if len(x1) != len(l):
raise ValueError('Dimension of x and l must be the same')
r2 = np.sum(((x1-x2)/l)**2)
return s*(1 + np.sqrt(5*r2) + 5/3*r2)*np.exp(-np.sqrt(5*r2))
# Expected improvement
def acq_func_EI(y_best, mu, sigma):
"""
Expected improvement acquisition function
Parameters
----------
y_best : float
Best observed function value
mu : float
Mean of the predictive distribution
sigma : float
Standard deviation of the predictive distribution
Returns
-------
float
Expected improvement acquisition function value
"""
# initialize standard normal distribution
stand_norm = multivariate_normal(mean=[0], cov=[[1]])
epsilon = 1e-6 # Small epsilon value to prevent division by zero
z = (y_best - mu) / (sigma + epsilon)
return (z*stand_norm.cdf(z) + stand_norm.pdf(z)) * sigma
def acq_func_PI(y_best, mu, sigma):
"""
Probability of Improvement acquisition function
Parameters
----------
y_best : float
Best observed function value
mu : float
Mean of the predictive distribution
sigma : float
Standard deviation of the predictive distribution
Returns
-------
float
Probability of Improvement acquisition function value
"""
gamma = y_best - mu
z = gamma / sigma
return norm.cdf(z)
def random_grid_search(X_train, X_val, y_train, y_val, hyperparameters, model, model_type="SVM", num_trials=10):
losses = []
best_losses = []
best_loss = float('inf')
best_params = None
first_params = None
execution_times = []
for _ in range(num_trials):
start_time = time.time()
param_dict = {param: random.choice(values) for param, values in hyperparameters.items()}
if model_type == "CNN":
cnn_model = model(X_train, y_train, **param_dict)
val_loss = evaluate_cnn(cnn_model, X_val, y_val)
elif model_type == "SVM":
clf = model(**param_dict)
clf.fit(X_train, y_train)
val_loss = 1 - clf.score(X_val, y_val)
else:
raise ValueError("Invalid model_type. Supported types are 'SVM' and 'CNN'.")
end_time = time.time()
execution_time = end_time - start_time
execution_times.append(execution_time)
losses.append(val_loss)
if first_params is None:
first_params = param_dict
if val_loss < best_loss:
best_loss = val_loss
best_params = param_dict
best_losses.append(best_loss)
return first_params, best_params, losses, best_losses, execution_times
def custom_random_grid_search_cifar(train_loader, val_loader, hyperparameters, model, num_random_samples=10):
losses = []
best_losses = []
best_loss = float('inf')
best_params = None
first_params = None
execution_times = []
for _ in range(num_random_samples):
start_time = time.time()
param_dict = {param: random.choice(values) for param, values in hyperparameters.items()}
cnn_model = model(train_loader, **param_dict)
val_loss = evaluate_NET(cnn_model, val_loader)
end_time = time.time()
execution_time = end_time - start_time
execution_times.append(execution_time)
losses.append(val_loss)
if not first_params:
first_params = param_dict
if val_loss < best_loss:
best_loss = val_loss
best_params = param_dict
best_losses.append(best_loss)
return first_params, best_params, losses, best_losses, execution_times
def custom_grid_search(X_train, X_val, y_train, y_val, hyperparameters, model, model_type="SVM"):
losses = []
best_losses = []
best_loss = float('inf')
best_params = None
first_params = None
execution_times = []
# Generate all combinations of hyperparameters
param_combinations = list(itertools.product(*hyperparameters.values()))
for idx, params in enumerate(param_combinations):
start_time = time.time()
param_dict = dict(zip(hyperparameters.keys(), params))
if model_type == "CNN":
cnn_model = model(X_train, y_train, **param_dict)
val_loss = evaluate_cnn(cnn_model, X_val, y_val)
elif model_type == "SVM":
clf = model(**param_dict)
clf.fit(X_train, y_train)
val_loss = 1 - clf.score(X_val, y_val)
else:
raise ValueError("Invalid model_type. Supported types are 'SVM' and 'CNN'.")
end_time = time.time()
execution_time = end_time - start_time
execution_times.append(execution_time)
losses.append(val_loss)
if idx == 0:
first_params = param_dict
if val_loss < best_loss:
best_loss = val_loss
best_params = param_dict
best_losses.append(best_loss)
return first_params, best_params, losses, best_losses, execution_times
def custom_grid_search_cifar(train_loader, val_loader, hyperparameters, model):
losses = []
best_losses = []
best_loss = float('inf')
best_params = None
first_params = None
execution_times = []
# Generate all combinations of hyperparameters
param_combinations = list(itertools.product(*hyperparameters.values()))
for idx, params in enumerate(param_combinations):
start_time = time.time()
param_dict = dict(zip(hyperparameters.keys(), params))
cnn_model = model(train_loader, **param_dict)
val_loss = evaluate_NET(cnn_model, val_loader)
end_time = time.time()
execution_time = end_time - start_time
execution_times.append(execution_time)
losses.append(val_loss)
if idx == 0:
first_params = param_dict
if val_loss < best_loss:
best_loss = val_loss
best_params = param_dict
best_losses.append(best_loss)
return first_params, best_params, losses, best_losses, execution_times
def plot_optimization_results(grid_search_best_losses, losses_bv, losses_bv_MCMC,
execution_times_gs, execution_times_bo, execution_times_MCMC):
# Calculate the minimum value found during grid search
grid_search_min = min(grid_search_best_losses)
# Plot optimization comparison
plt.figure(figsize=(10, 6))
plt.plot(losses_bv, label=f'Bayesian Optimization (y_best={np.round(losses_bv[-1], 3)})')
plt.plot(losses_bv_MCMC, label=f'Bayesian Optimization MCMC (y_best={np.round(losses_bv_MCMC[-1], 3)})')
plt.axhline(y=grid_search_min, color='r', linestyle='--', label=f'Minimum from Grid Search (value={np.round(grid_search_min, 3)})')
plt.xlabel('Number of iterations', fontsize=14)
plt.ylabel('f(x)', fontsize=14)
plt.legend(fontsize=12)
plt.title('Optimization Comparison', fontsize=16)
plt.show()
# Summing up the execution times for each method
total_time_gs = sum(execution_times_gs)
# total_time_r = sum(execution_times_r)
total_time_bo = sum(execution_times_bo)
total_time_MCMC = sum(execution_times_MCMC)
# Plotting the histogram
plt.figure(figsize=(10, 6))
plt.bar(['Custom Grid Search', 'Bayesian Optimization', 'Bayesian Optimization MCMC'], [total_time_gs, total_time_bo, total_time_MCMC], color=['blue', 'orange', 'green'])
plt.xlabel('Method', fontsize=14)
plt.ylabel('Total Execution Time (seconds)', fontsize=14)
plt.title('Total Execution Time for Each Method', fontsize=16)
plt.show()
def plot_results_SVM(grid_search_best_losses, losses_bv, losses_bv_MCMC, execution_times_bo, execution_times_MCMC):
fig, ax = plt.subplots(figsize=(12, 4), dpi=500)
# Calculate the minimum value found during grid search
grid_search_min = min(grid_search_best_losses)
# Plot the mean values and scatter points
ax.plot(np.mean(losses_bv, axis=0), label=f'y_GP_best={np.round(np.mean(losses_bv[:,-1]),3)}', color='orange')
ax.plot(np.mean(losses_bv_MCMC, axis=0), label=f'y_GP_MCMC_best={np.round(np.mean(losses_bv_MCMC[:,-1]),3)}', color='b')
# Plot axial line at grid_search_min
ax.axhline(y=grid_search_min, color='g', linestyle='--', label=f'Grid Search Minimum={np.round(grid_search_min, 3)}')
# Set labels and legend
ax.set_xlabel('Number of iterations')
ax.set_ylabel('f(x)')
ax.legend()
# Create zoom inset
axins = ax.inset_axes([0.33, 0.42, 0.6, 0.35])
# Adjust data ranges for zoom-in
zoom_in_x = np.arange(1, 13)
zoom_in_y_bv = np.mean(losses_bv, axis=0)[1:13] # Selecting data from index 1 to 12
zoom_in_y_bv_MCMC = np.mean(losses_bv_MCMC, axis=0)[1:13] # Selecting data from index 1 to 12
# Plot zoomed-in data
axins.plot(zoom_in_x, zoom_in_y_bv, color='orange')
axins.plot(zoom_in_x, zoom_in_y_bv_MCMC, color='b')
# Set zoom-in limits
axins.set_xlim(1, 12)
axins.set_ylim(-0.005, 0.005) # Set y-axis range from -0.005 to 0.005
# # Add scatter plot to the inset
# for i in range(len(losses_bv)):
# axins.scatter(np.arange(1, 13), losses_bv[i][:12], color='orange', alpha=0.1)
# axins.scatter(np.arange(1, 13), losses_bv_MCMC[i][:12], color='b', alpha=0.1)
# Add border around the zoom inset
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
# Sum the elements in each list
sum_execution_times_bo = np.sum(execution_times_bo, axis=1)
sum_execution_times_MCMC = np.sum(execution_times_MCMC, axis=1)
# Take the mean of the sums
mean_sum_execution_times_bo = np.mean(sum_execution_times_bo)
mean_sum_execution_times_MCMC = np.mean(sum_execution_times_MCMC)
# Plotting the histogram
plt.figure(figsize=(10, 6))
plt.bar(['Custom Grid Search', 'Bayesian Optimization', 'Bayesian Optimization MCMC'], [sum(execution_times_gs), mean_sum_execution_times_bo, mean_sum_execution_times_MCMC], color=['blue', 'orange', 'green'])
plt.xlabel('Method', fontsize=14)
plt.ylabel('Total Execution Time (seconds)', fontsize=14)
plt.title('Total Execution Time for Each Method', fontsize=16)
plt.show()
def plot_results(grid_search_best_losses, grid_search_best_losses_rgs, losses_bv_1, losses_bv_2, losses_bv_3, execution_times_gs, execution_times_rgs, execution_times_bo_1, execution_times_bo_2, execution_times_bo_3, s_or_t):
fig, ax = plt.subplots(figsize=(10, 5), dpi=500)
# Calculate the minimum value found during grid search
grid_search_min = min(grid_search_best_losses)
n_samples = len(losses_bv_1[0])
# Plot the mean values and scatter points
ax.plot(grid_search_best_losses_rgs, label=f'y_random_Grid={min(grid_search_best_losses_rgs)}', color='Green')
ax.plot(np.mean(losses_bv_1, axis=0), label=f'y_GP_{s_or_t}_one={np.round(np.mean(losses_bv_1[:,-1]),3)}', color='orange')
ax.plot(np.mean(losses_bv_2, axis=0), label=f'y_GP_{s_or_t}_zero_one={np.round(np.mean(losses_bv_2[:,-1]),3)}', color='b')
ax.plot(np.mean(losses_bv_3, axis=0), label=f'y_GP_{s_or_t}_ten={np.round(np.mean(losses_bv_3[:,-1]),3)}', color='r')
ax.scatter(np.arange(1, n_samples), np.mean(losses_bv_1, axis=0)[1:n_samples], color='orange', alpha=0.5)
ax.scatter(np.arange(1, n_samples), np.mean(losses_bv_2, axis=0)[1:n_samples], color='b', alpha=0.5)
ax.scatter(np.arange(1, n_samples), np.mean(losses_bv_3, axis=0)[1:n_samples], color='r', alpha=0.5)
# Calculate standard deviation of losses
std_losses_bv_1 = np.std(losses_bv_1, axis=0)
std_losses_bv_2 = np.std(losses_bv_2, axis=0)
std_losses_bv_3 = np.std(losses_bv_3, axis=0)
# Plot error bars for scatter points
ax.errorbar(np.arange(1, n_samples), np.mean(losses_bv_1, axis=0)[1:n_samples], yerr=std_losses_bv_1[1:n_samples], fmt='o', color='orange', alpha=0.5)
ax.errorbar(np.arange(1, n_samples), np.mean(losses_bv_2, axis=0)[1:n_samples], yerr=std_losses_bv_2[1:n_samples], fmt='o', color='b', alpha=0.5)
ax.errorbar(np.arange(1, n_samples), np.mean(losses_bv_3, axis=0)[1:n_samples], yerr=std_losses_bv_3[1:n_samples], fmt='o', color='r', alpha=0.5)
# Plot axial line at grid_search_min
ax.axhline(y=grid_search_min, color='g', linestyle='--', label=f'Grid Search Minimum={np.round(grid_search_min, 3)}')
# Set labels and legend
ax.set_xlabel('Number of iterations')
ax.set_ylabel('1 - accuracy')
ax.legend()
# Create zoom inset
axins = ax.inset_axes([0.33, 0.42, 0.6, 0.35])
# Adjust data ranges for zoom-in
zoom_in_x = np.arange(1, n_samples)
zoom_in_y_bv_1 = np.mean(losses_bv_1, axis=0)[1:n_samples] # Selecting data from index 1 to 12
zoom_in_y_bv_2 = np.mean(losses_bv_2, axis=0)[1:n_samples] # Selecting data from index 1 to 12
zoom_in_y_bv_3 = np.mean(losses_bv_3, axis=0)[1:n_samples] # Selecting data from index 1 to 12
# Plot zoomed-in data
axins.plot(zoom_in_x, zoom_in_y_bv_1, color='orange')
axins.plot(zoom_in_x, zoom_in_y_bv_2, color='b')
axins.plot(zoom_in_x, zoom_in_y_bv_3, color='r')
axins.axhline(y=grid_search_min, color='g', linestyle='--', label=f'Grid Search Minimum={np.round(grid_search_min, 3)}')
# Set zoom-in limits
axins.set_xlim(1, n_samples)
axins.set_ylim(0.01, 0.07) # Set y-axis range from -0.005 to 0.005
# Add scatter plot to the inset
axins.scatter(np.arange(1, n_samples), np.mean(losses_bv_1, axis=0)[1:n_samples], color='orange', alpha=0.5)
axins.scatter(np.arange(1, n_samples), np.mean(losses_bv_2, axis=0)[1:n_samples], color='b', alpha=0.5)
axins.scatter(np.arange(1, n_samples), np.mean(losses_bv_3, axis=0)[1:n_samples], color='r', alpha=0.5)
# Plot error bars in zoomed-in data
axins.errorbar(zoom_in_x, zoom_in_y_bv_1, yerr=std_losses_bv_1[1:n_samples], fmt='o', color='orange', alpha=1)
axins.errorbar(zoom_in_x, zoom_in_y_bv_2, yerr=std_losses_bv_2[1:n_samples], fmt='o', color='b', alpha=0.4)
axins.errorbar(zoom_in_x, zoom_in_y_bv_3, yerr=std_losses_bv_3[1:n_samples], fmt='o', color='r', alpha=0.6)
# Add border around the zoom inset
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
# Sum the elements in each list
sum_execution_times_bo_1 = np.sum(execution_times_bo_1, axis=1)
sum_execution_times_bo_2 = np.sum(execution_times_bo_2, axis=1)
sum_execution_times_bo_3 = np.sum(execution_times_bo_3, axis=1)
# Take the mean of the sums
mean_sum_execution_times_bo_1 = np.mean(sum_execution_times_bo_1)
mean_sum_execution_times_bo_2 = np.mean(sum_execution_times_bo_2)
mean_sum_execution_times_bo_3 = np.mean(sum_execution_times_bo_3)
# Calculate standard deviation of execution times
std_execution_times_bo_1 = np.std(sum_execution_times_bo_1)
std_execution_times_bo_2 = np.std(sum_execution_times_bo_2)
std_execution_times_bo_3 = np.std(sum_execution_times_bo_3)
# Plotting the bar chart with error bars
methods = ['Grid Search', 'Random Search', f'Bayes Opt {s_or_t} = 1', f'Bayes Opt {s_or_t} = 0.1', f'Bayes Opt {s_or_t} = 10']
means = [sum(execution_times_gs), sum(execution_times_rgs), mean_sum_execution_times_bo_1, mean_sum_execution_times_bo_2, mean_sum_execution_times_bo_3]
std_devs = [0, 0, std_execution_times_bo_1, std_execution_times_bo_2, std_execution_times_bo_3]
plt.figure(figsize=(10, 6))
plt.bar(methods, means, yerr=std_devs, capsize=5, color=['blue', 'orange', 'green', 'red'])
plt.xlabel('Method', fontsize=14)
plt.ylabel('Total Execution Time (seconds)', fontsize=14)
plt.title('Total Execution Time for Each Method', fontsize=16)
plt.show()
####################################### METROPOLIS #######################################
def likelihood(theta, x, y, kernel, loop_number):
"""
Likelihood function for the GP hyperparameters (function of theta, given data (x, y))
Parameters
----------
theta : array_like
Hyperparameters
y : array_like
Observations
x : array_like
Sample points (raws hyperparameters, columns observations)
kernel : function
Kernel function
loop_number : int
Number of the loop for which the likelihood is computed
Returns
-------
float
Likelihood
"""
# compute covariance matrix
l = loop_number
K = np.zeros((l, l))
for i in range(l):
for j in range(l):
# no negative s
K[i, j] = kernel(x[:, i], x[:, j], l=theta[1:], s=np.abs(theta[0])+1e-6)
like = multivariate_normal(mean=np.zeros(len(y)), cov=K)
return like.pdf(y)
def Metropolis(n_sample, cov, x0, burn_in, thinning, target, **kwargs):
"""
Metropolis algorithm with Gaussian proposal distribution
Parameters
----------
target : function
Target distribution
x0 : array_like
Initial point
n_sample : int
Number of samples
cov : array_like
Covariance matrix of the proposal distribution
burn_in : int
Number of burn-in samples
thinning : int
Thinning factor
**kwargs :
Additional parameters for the target distribution
Returns
-------
array_like
Samples from the target distribution
"""
# rows: dimensions, columns: samples (take into account thinning)
sample = np.zeros((len(x0), n_sample))
x_current = x0
for i in range(burn_in):
# propose new sample
proposal = multivariate_normal(mean=x_current, cov=cov)
x_prop = proposal.rvs()
# Acceptance probability
proposal_sym = multivariate_normal(mean=x_prop, cov=cov)
# Hastings
A = min(1, target(x_prop, **kwargs)*proposal_sym.pdf(x_current) / target(x_current, **kwargs)*proposal.pdf(x_prop))
#A = min(1, target(x_prop, **kwargs)/ target(x_current, **kwargs))
if np.random.rand() < A:
x_current = x_prop
for i in range(n_sample*thinning):
# propose new sample
proposal = multivariate_normal(mean=x_current, cov=cov)
x_prop = proposal.rvs()
proposal_sym = multivariate_normal(mean=x_prop, cov=cov)
# Acceptance probability
A = min(1, target(x_prop, **kwargs)*proposal_sym.pdf(x_current) / target(x_current, **kwargs)*proposal.pdf(x_prop))
if np.random.rand() < A:
x_current = x_prop
if i % thinning == 0:
sample[:, i//thinning] = x_current
return sample
##################################### TRAIN ML MODELS ########################################
################ SVM ################
def train_svm(X_train, y_train, C, gamma):
svm = SVC(C=C, gamma=gamma, random_state=42)
svm.fit(X_train, y_train)
return svm
def evaluate_svm(svm, X_val, y_val):
return 1.0 - svm.score(X_val, y_val)
################ CNN ################
def train_cnn(X_train, y_train, N_epochs, regularization_l2, dropout_rate, learning_rate, num_layers):
# Define the CNN architecture
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
for _ in range(int(num_layers) - 1):
model.add(Dense(128, activation='relu', kernel_regularizer='l2'))
model.add(Dropout(dropout_rate))
model.add(Dense(10, activation='softmax')) # Output layer with 10 units for 10 classes
# Compile the model
optimizer = Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, epochs=int(N_epochs), validation_split=0.2, batch_size=128, verbose=1)
return model
def evaluate_cnn(model, X_val, y_val):
_, accuracy = model.evaluate(X_val, y_val, verbose=0)
return 1.0 - accuracy
################ CNN REDUCED SPACE ################
def train_cnn_reduced(X_train, y_train, dropout_rate, learning_rate):
# Define the CNN architecture
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
for _ in range(2 - 1):
model.add(Dense(128, activation='relu', kernel_regularizer='l2'))
model.add(Dropout(dropout_rate))
model.add(Dense(10, activation='softmax')) # Output layer with 10 units for 10 classes
# Compile the model
optimizer = Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, epochs=int(3), validation_split=0.2, batch_size=128, verbose=1)
return model
def evaluate_cnn_reduced(model, X_val, y_val):
_, accuracy = model.evaluate(X_val, y_val, verbose=0)
return 1.0 - accuracy
################ CIFAR ######################
def train_NET_complex(trainloader, epochs, learning_rate, momentum_m, verbose=True):
# Define the neural network model
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = torch.flatten(x, 1) # flatten all dimensions except batch
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=momentum_m)
for epoch in range(int(epochs)): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if verbose and i % 2000 == 1999: # print every 2000 mini-batches
print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')
running_loss = 0.0
if verbose:
print(f'Epoch [{epoch + 1}/{epochs}] Loss: {running_loss / len(trainloader):.3f}')
print('Finished Training')
return net
def evaluate_NET_complex(net, validloader):
correct = 0
total = 0
with torch.no_grad():
for data in validloader:
inputs, labels = data
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = correct / total
return 1.0 - accuracy
def train_NET(trainloader,
activation_function,
learning_rate,
epochs = 5,
n_neurons=[[3072,512],[512,258],[258,10]],
criterion = nn.CrossEntropyLoss(),
verbose=True):
# Define the neural network model
class MLP(nn.Module):
def __init__(self, n_neurons, activation_function):
# Initialize the MLP model
super().__init__()
# List to store layers
layers = []
# Input layer
layers.append(nn.Linear(n_neurons[0][0], n_neurons[0][1]))
layers.append(activation_function)
# Hidden layers
for i in range(1, len(n_neurons)):
layers.append(nn.Linear(n_neurons[i][0], n_neurons[i][1]))
layers.append(activation_function)
# Create the Sequential container
self.linear_layers = nn.Sequential(*layers)
def forward(self, x):
batch_size = x.shape[0]
# Flatten x
x = x.view(batch_size, -1)
# Apply linear
out = self.linear_layers(x)
return out
net = MLP(n_neurons, activation_function)
#criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=learning_rate)
for epoch in range(int(epochs)): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if verbose and i % 2000 == 1999: # print every 2000 mini-batches
print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')
running_loss = 0.0
if verbose:
print(f'Epoch [{epoch + 1}/{epochs}] Loss: {running_loss / len(trainloader):.3f}')
print('Finished Training')
return net
def evaluate_NET(net, validloader):
correct = 0
total = 0
with torch.no_grad():
for data in validloader:
inputs, labels = data
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = correct / total
return 1.0 - accuracy
##################################### BAYESIAN OPTIMIZERS #####################################
def bayes_optimization(hyp_space, n_sample, n_wu, acq_function, kernel, l, s, eval_func, train_func, start_hyper_comb, X_train, X_val, y_train, y_val):
"""
Bayesian optimization for hyperparameter tuning
Parameters
----------
hyp_space : dict
hyperparameter dictionary
n_sample : int
number of samples to draw
n_wu : int
number of warmup samples
acq_function : function
acquisition function
kernel : function
kernel function
f : function
function to optimize
Returns
-------
hyp : array_like
hyperparameter values
"""
loss_best_vec = np.array([])
best_hyp = np.array([])
loss_sampled = np.array([])
hyp_name = list(hyp_space.keys())
loss_best = np.inf
acq_funcs = np.array([])
# all possible combinations of hyperparameters (each row is a combination)
hyp_space_combinations = np.array(generate_combinations(hyp_space))
# warm up
# random sampling from hyperparameter space
# matrix of sampled values (rows: hyperparameters, columns: samples)
x_sampled = np.zeros((len(hyp_name), n_sample+n_wu))
execution_times = []
for j in range(n_wu):
start_time = time.time()
# sample hyperparameters from the hyperparameter space
np.random.shuffle(hyp_space_combinations)
shape = hyp_space_combinations.shape
if shape[0] == 0:
break
else:
if j == 0:
x_sampled[:, j] = start_hyper_comb
hyp_space_combinations = np.delete(hyp_space_combinations, np.where((hyp_space_combinations == x_sampled[:, j]).all(axis=1))[0][0], axis=0)
else:
#sample a hyperparameter combination
x_sampled[:, j] = hyp_space_combinations[0]
# delete the sampled value from the hyperparameter combinations
hyp_space_combinations = np.delete(hyp_space_combinations, 0, axis=0)
# evaluate loss for training with those hyperparameters
trained_model = train_func(X_train, y_train, *x_sampled[:, j])
loss = eval_func(trained_model, X_val, y_val)
loss_sampled = np.append(loss_sampled, loss)
# update loss_best
if loss < loss_best:
loss_best = loss
best_hyp = x_sampled[:, j]
# append loss_best to loss_best_vec
loss_best_vec = np.append(loss_best_vec, loss_best)
end_time = time.time()
execution_times.append(end_time - start_time)
# sampling
for j in range(n_wu, n_sample+n_wu):
start_time = time.time()
acq_max = -np.inf
# update kernel matrix
if j == n_wu:
# Initialize covariance matrix between two sampled points and
# place values in the cov matrix using the kernel
K = np.zeros((n_wu, n_wu))
for i in range(n_wu):
for k in range(i):
# The kernel makes an assumption on the covariance
# Update every new sample
K[i, k] = kernel(x_sampled[:, i], x_sampled[:, k], l, s)
K[k, i] = K[i, k]
else:
# add new row and column to kernel matrix
K = np.pad(K, pad_width=(0,1), mode='constant', constant_values=0)
# update new row and column
for i in range(j-1):
K[i, j-1] = kernel(x_sampled[:, i], x_sampled[:, j-1], l, s)
K[j-1, i] = K[i, j-1]
# compute the acquisition function for each possible value of the hyperparameters vector, keeping only the maximum while updating the hyperparameters
# and sampling the new hyperparameters vector
K_inv = np.linalg.inv(K)
for xs in hyp_space_combinations:
# compute the kernel vector for each combination of hyperparameter
# with the newly chosen HP
k = np.array([kernel(x_sampled[:, r], xs, l, s) for r in range(j)])
acq = acq_function(loss_best, mu(k, K_inv, loss_sampled), sigma(kernel(xs, xs, l, s), k, K_inv))
# acq_funcs = np.append(acq_funcs, acq)
# check if the new value is the maximum
if acq > acq_max:
acq_max = acq
tmp_x_sampled = xs
# update x_sampled
x_sampled[:, j] = tmp_x_sampled
# delete the sampled value from the hyperparameter combinations
if x_sampled[:, j] in hyp_space_combinations:
hyp_space_combinations = np.delete(hyp_space_combinations, np.where((hyp_space_combinations == x_sampled[:, j]).all(axis=1))[0][0], axis=0)
else:
break
# evaluate function
trained_model = train_func(X_train, y_train, *x_sampled[:, j])
loss = eval_func(trained_model, X_val, y_val)
loss_sampled = np.append(loss_sampled, loss)
# update y_best
if loss < loss_best:
loss_best = loss
best_hyp = x_sampled[:, j]
# append y_best to y_best_vec
loss_best_vec = np.append(loss_best_vec, loss_best)
end_time = time.time()
execution_times.append(end_time - start_time)
return best_hyp, loss_best_vec, x_sampled, loss_sampled, execution_times
def bayes_optimization_MCMC(hyp_space, n_sample, n_wu, acq_function, kernel, eval_func, train_func, start_hyper_comb, X_train, X_val, y_train, y_val):
"""
Bayesian optimization for hyperparameter tuning
Parameters
----------
hyp_space : dict
hyperparameter dictionary
n_sample : int
number of samples to draw
n_wu : int
number of warmup samples
acq_function : function
acquisition function
kernel : function
kernel function
eval_func : function
function to evaluate the model
train_func : function
function to train the model
X_train : array_like
training data
X_val : array_like
validation data
y_train : array_like
training labels
y_val : array_like
validation labels
Returns
-------
hyp : array_like
hyperparameter values
"""
y_best_vec = np.array([])
best_hyp = np.array([])
y_sampled = np.array([])
hyp_name = list(hyp_space.keys())
y_best = np.inf
# all possible combinations of hyperparameters (each row is a combination)
hyp_space_combinations = np.array(generate_combinations(hyp_space))
# warm up
# random sampling from hyperparameter space
# matrix of sampled values (rows: hyperparameters, columns: samples)
n_hyper = len(hyp_name) # number of hyperparameters
x_sampled = np.zeros((n_hyper, n_sample+n_wu))
execution_times = []
for j in range(n_wu):
start_time = time.time()