-
Notifications
You must be signed in to change notification settings - Fork 1
/
mlp_tacthmc.py
140 lines (129 loc) · 6.73 KB
/
mlp_tacthmc.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 platform
print('python_version ==', platform.python_version())
import torch
print('torch.__version__ ==', torch.__version__)
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
import time
import argparse
import numpy as np
from tacthmc import *
from evaluation import *
import os
from model_zoo import *
'''set up hyperparameters of the experiments'''
parser = argparse.ArgumentParser(description='tacthmc on MLP tested on Extended MNIST appending noise')
parser.add_argument('--train-batch-size', type=int, default=128)
parser.add_argument('--test-batch-size', type=int, default=18800)
parser.add_argument('--num-burn-in', type=int, default=30000)
parser.add_argument('--num-epochs', type=int, default=2000)
parser.add_argument('--evaluation-interval', type=int, default=30)
parser.add_argument('--eta-theta', type=float, default=1.7e-8)
parser.add_argument('--eta-xi', type=float, default=1.7e-10)
parser.add_argument('--c-theta', type=float, default=0.1)
parser.add_argument('--c-xi', type=float, default=0.1)
parser.add_argument('--gamma-theta', type=float, default=1)
parser.add_argument('--gamma-xi', type=float, default=1)
parser.add_argument('--prior-precision', type=float, default=1e-3)
parser.add_argument('--permutation', type=float, default=0.3)
parser.add_argument('--enable-cuda', action='store_true')
parser.add_argument('--device-num', type=int, default=7)
parser.add_argument('--tempering-model-type', type=int, default=1)
parser.add_argument('--load-tempering-model', action='store_true')
parser.add_argument('--save-tempering-model', action='store_true')
parser.add_argument('--tempering-model-path')
args = parser.parse_args()
print (args)
if torch.cuda.is_available():
torch.cuda.set_device(args.device_num)
'''load dataset'''
train_loader = torch.utils.data.DataLoader(
datasets.EMNIST('./emnist-dataset', split='balanced', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=args.train_batch_size, shuffle=True, drop_last=True)
test_loader = torch.utils.data.DataLoader(
datasets.EMNIST('./emnist-dataset', split='balanced', train=False,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=args.test_batch_size, shuffle=False, drop_last=True)
if __name__ == '__main__':
model = MLP()
cuda_availability = args.enable_cuda and torch.cuda.is_available()
N = len(train_loader.dataset)
num_labels = model.outputdim
if args.tempering_model_type == 1:
temper_model_name = 'Metadynamics'
elif args.tempering_model_type == 2:
temper_model_name = 'ABF'
sampler = TACTHMC(model, N, args.eta_theta, args.eta_xi, args.c_theta, args.c_xi, args.gamma_theta, args.gamma_xi, cuda_availability, temper_model=temper_model_name)
if cuda_availability:
model.cuda()
sampler.resample_momenta()
print(model)
nIter = 0
tStart = time.time()
estimator = FullyBayesian((len(test_loader.dataset), num_labels),\
model,\
test_loader,\
cuda_availability)
acc = 0
if args.save_tempering_model:
path = args.tempering_model_path
if path[-1] != '/':
path = path+'/'
try:
os.mkdir(path)
except:
print ('There exists a folder or no tempering model path is set up!')
if args.load_tempering_model:
try:
sampler.temper_model.loader(path+temper_model_name+'.pt', args.enable_cuda)
except:
print ('No saved model or no tempering model path is found!')
for epoch in range(1, 1 + args.num_epochs):
print ("#######################################################################################")
print ("This is the epoch: ", epoch)
print ("#######################################################################################")
if epoch%(0.1*args.num_epochs) == 0:
if temper_model_name == 'Metadynamics':
sampler.temper_model.offset()
if args.save_tempering_model:
try:
sampler.temper_model.saver(path)
except:
print ('No tempering model path is set up!')
for i, (x, y) in enumerate(train_loader):
batch_size = x.data.size(0)
if args.permutation > 0.0:
y = y.clone()
y.data[:int(args.permutation*batch_size)] = torch.LongTensor(np.random.choice(num_labels, int(args.permutation*batch_size)))
if cuda_availability:
x, y = x.cuda(), y.cuda()
model.zero_grad()
yhat = model(x)
loss = F.cross_entropy(yhat, y)
for param in model.parameters():
loss += args.prior_precision * torch.sum(param**2)
loss.backward()
'''update position and momentum'''
sampler.update(loss)
nIter += 1
if nIter%args.evaluation_interval == 0:
print ('xi:{:+7.4f}; fU:{:+.3E}; r_xi:{:+.3E}; loss:{:6.4f}; thermostats_param:{:6.3f}; thermostats_xi:{:6.3f}; tElapsed:{:6.3f}'.format(sampler.model.xi.item(),\
sampler.fU.item(),\
sampler.model.r_xi.item(),\
loss.data.item(),\
sampler.get_z_u(),\
sampler.get_z_xi(),\
time.time() - tStart))
if abs(sampler.model.xi.item()) <= 0.85*sampler.standard_interval and nIter >= args.num_burn_in:
acc = estimator.evaluation()
print ('This is the accuracy:%{:6.2f}'.format(acc))
sampler.resample_momenta()
tStart = time.time()