-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake_figures_fneval_local.m
212 lines (187 loc) · 5.77 KB
/
make_figures_fneval_local.m
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
function make_figures_fneval_local(LeapSize,epsilon,beta)
HOME=getenv('HOME');
whos
clc;
close all;
opts_init = [];
% the energy function and gradient for the circle distribution in the arXiv
% TODO: I think I should have used a very long narrow Gaussian instead!
% opts_init.E = @E_circle;
opts_init.E = @E_gauss;
% opts_init.dEdX = @dEdX_circle;
opts_init.dEdX = @dEdX_gauss;
% make this 1 for more output
opts_init.Debug = 0;
% step size for HMC
if nargin<1
opts_init.LeapSize = 10;
else
LeapSize=str2num(LeapSize);
opts_init.LeapSize = LeapSize
end
if nargin<2
opts_init.epsilon = 1.3;
else
epsilon =str2num(epsilon);
opts_init.epsilon = epsilon
end
if nargin<3
opts_init.beta = .35;
else
beta =str2num(beta);
opts_init.beta = beta
end
%Model Name
FEVAL_MAX = 5000000
% modelname='100dCircle100'
modelname='2D'
Nsamp = 10000;
opts_init.BatchSize = 100;
opts_init.DataSize = 10;
opts_init.DataSize = 2;
savestr = strcat('ModelName-',modelname,'-LeapSize-',int2str(opts_init.LeapSize),...
'-epsilon-',int2str(opts_init.epsilon*10),'-Beta-',int2str(opts_init.beta*100)...
,'-fevals-',int2str(FEVAL_MAX),'-Nsamp-',int2str(Nsamp)...
,'-BS-',int2str(opts_init.BatchSize),'-DS-',int2str(opts_init.DataSize));
savepath = strcat(HOME,'/Data/HMC_reducedflip/10d-MOG-20/',savestr);
figpath1 = strcat(HOME,'/Data/HMC_reducedflip/10d-MOG-20/figures/',savestr,'autocor');
figpath2 = strcat(HOME,'/Data/HMC_reducedflip/10d-MOG-20/figures/',savestr,'autocor-fevals');
mkdir(savepath);
mkdir(figpath1);
mkdir(figpath2);
opts_init.funcevals = 0;
% % % % scaling factor for energy function
% % % % theta = [1,0;0,1e-6];
% % % % % theta = 100; %%circle
% % % %%Crazy ass MOG with crazy ass everything
% % % x=4;b=-6;
% % % for ii=1:opts_init.DataSize
% % % rng(ii);
% % % J{ii}=diag(exp(linspace(log(1e-6), log(1), opts_init.DataSize)).*rand(1,opts_init.DataSize));
% % % Mu{ii} = [zeros(opts_init.DataSize-1,1);b+(ii)*x];
% % % % % Mu{ii}=randn(opts_init.DataSize,1)*(rand(1));
% % % end
% % % % %
% % %%2D MOG with n mixtures
% % % x=4;b=-6;
% %
% % % for ii=1:MOG
% % % J{ii}=diag(ones(opts_init.DataSize,1));%Unit covariances
% % % Mu{ii}=[0;b + (ii)*x];%Means
% % % end
%logalpha = zeros(opts_init.DataSize,1);
%W = eye(opts_init.DataSize);
%theta = [W, logalpha];
theta = diag(exp(linspace(log(1e-5), log(1), opts_init.DataSize)));
opts_init.Xinit = sqrtm(inv(theta))*randn( opts_init.DataSize, opts_init.BatchSize );
%Initalize Options
ii = 1
names{ii} = 'standard'
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 0;
opts{ii}.beta = 1;
%Initialize States
states{ii} = [];
% arrays to keep track of the samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = []
ii = ii + 1
names{ii} = 'persist'
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 0;
%Initialize States
states{ii} = [];
% arrays to keep track of the samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = []
ii = ii + 1
names{ii} = 'reduced flip'
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 1;
%Initialize States
states{ii} = [];
% arrays to keep track of the samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = []
ii = ii + 1
names{ii} = 'forever forward'
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 3;
%Initialize States
states{ii} = [];
% arrays to keep track of the samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = []
ii = ii + 1
names{ii} = 'default + ff'
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 3;
opts{ii}.beta = 1;
%Initialize States
states{ii} = [];
%arrays to keep track of samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = []
if 0
ii = ii + 1
names{ii} = 'two momentum'
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 2;
%Initialize States
states{ii} = [];
% arrays to keep track of the samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = []
end
RUN_FLAG=1;
ttt = tic();
ii=1;
% call the sampling algorithm Nsamp times
while (ii <=Nsamp && RUN_FLAG == 1)
for jj = 1:length(names)
if ii == 1 || states{jj}.funcevals < FEVAL_MAX
[Xloc, statesloc] = rf2vHMC( opts{jj}, states{jj},theta);
states{jj} = statesloc;
if ii > 1
X{jj} = cat(3,X{jj}, Xloc);
else
X{jj} = Xloc;
end
fevals{jj}(ii,1) = states{jj}.funcevals;
assert(opts_init.BatchSize == size(Xloc,2));
else
RUN_FLAG = 0;
break;
end
end
%Display + Saving
if (mod( ii, 500 ) == 0) || (ii == Nsamp) || RUN_FLAG == 0
fprintf('%d / %d in %f sec (%f sec remaining)\n', ii, Nsamp, toc(ttt), toc(ttt)*Nsamp/ii - toc(ttt) );
for jj = 1:length(names)
disp(names{jj})
states{jj}
states{jj}.steps
states{jj}.steps.leap'
end
%Calculate average fevals by taking total fevals at this point
%and dividing it by the number of samples we have acquired
sprintf('calculating average fevals')
for jj=1:length(names)
avg_fevals{jj}=fevals{jj}(end,1)/size(X{jj},3);
end
if exist('Mu','var')
[h1,h2]=plot_autocorr_samples(X, names,avg_fevals,Mu);
else
[h1,h2]=plot_autocorr_samples(X, names,avg_fevals);
end
disp('Autocorr plot completed')
% h2=plot_fevals(fevals, names);
disp('Fevals plot completed')
disp(savestr)
saveas(h1,figpath1,'pdf');
saveas(h2,figpath2,'pdf');
save(savepath);
end
ii = ii + 1;
end
ttt = toc(ttt);