-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMeanDerivatives.m
More file actions
62 lines (53 loc) · 1.67 KB
/
MeanDerivatives.m
File metadata and controls
62 lines (53 loc) · 1.67 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
function [gmu,Hmu,nll] = MeanDerivatives(dat,mu,Wa,Wv,noise,s)
% Return derivatives w.r.t. appearance basis functions
% FORMAT [nll,gmu,Hmu] = MeanDerivatives(dat,mu,Wa,Wv,noise,s)
%
% dat - Structure containing various information about each image.
% Fields for each image n are:
% dat(n).f - Image data.
% dat(n).z - Expectations of latent variables.
% dat(n).S - Covariances of latent variables.
% mu - Mean
% Wa - Appearance basis functions
% Wv - Shape basis functions
% noise - Noise information
% s - Settings. Uses s.likelihood and s.batchsize.
%
% gmu - Gradients
% Hmu - Hessians
% nll - Negative log-likelihood
%
%__________________________________________________________________________
% Copyright (C) 2017 Wellcome Trust Centre for Neuroimaging
% John Ashburner
% $Id$
if isempty(dat), gmu = []; Hmu = []; nll = 0; return; end
d = [size(mu), 1,1];
d = d(1:4);
switch lower(s.likelihood)
case {'normal','gaussian','binomial','binary'}
d4 = d(4);
case {'multinomial','categorical'}
d4 = d(4)*(d(4)+1)/2;
end
batchsize = 1;
if isfield(s,'batchsize'), batchsize = s.batchsize; end
gmu = zeros( d(1:4) ,'single');
Hmu = zeros([d(1:3) d4],'single');
nll = 0;
for n1=1:batchsize:numel(dat)
nn = n1:min(n1+(batchsize-1),numel(dat));
z = {dat(nn).z};
cell1 = GetV0(z,Wv);
cell2 = GetA0(z,Wa,mu);
dat1 = dat(nn);
parfor n=1:numel(nn)
psi = GetPsi(cell1{n},s);
a0 = cell2{n};
f = GetDat(dat1(n),s);
[ll,g,H] = LikelihoodDerivatives(f,a0,psi,noise,s);
nll = nll - ll;
gmu = gmu + g;
Hmu = Hmu + H;
end
end