-
Notifications
You must be signed in to change notification settings - Fork 16
/
visualize_mit.m
146 lines (120 loc) · 5.01 KB
/
visualize_mit.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
% Implicit Mixture of Conditional Restricted Boltzmann Machines
% Version 1.000
%
% Code provided by Graham Taylor
%
% For more information, see:
% http://www.uoguelph.ca/~gwtaylor/publications/cvpr2010/
%
% Permission is granted for anyone to copy, use, modify, or distribute this
% program and accompanying programs and documents for any purpose, provided
% this copyright notice is retained and prominently displayed, along with
% a note saying that the original programs are available from our
% web page.
% The programs and documents are distributed without any warranty, express or
% implied. As the programs were written for research purposes only, they have
% not been tested to the degree that would be advisable in any important
% application. All use of these programs is entirely at the user's own risk.
%
%
% visualization for learning an implicit mixture of crbms
% on the mit walk & jog data
% show hiddens
plotindex = [101:300 2101:2300]; %frames of batchdata that we will plot
plottemp = 1; % temperature used for plotting
nc = length(plotindex);
data = single(batchdata(plotindex,:));
past = zeros(nc,nt*numdims,'single');
%Easiest way to build past is by a loop
%Past looks like [ [data time t-nt] ... [data time t-1] ]
for hh=nt:-1:1 %note reverse order
past(:,numdims*(nt-hh)+1:numdims*(nt-hh+1)) = batchdata(plotindex-hh,:);
end
%Note that we will re-use the effective visible, hidden biases several
%times so we compute them here (per-component) and keep them around
effvisbiases = zeros(nc,numdims,numcomp,'single');
effhidbiases = zeros(nc,numhid,numcomp,'single');
%we calculate free-energy per-point, per-component
fe = zeros(nc,numcomp); %let fe be double
% iterate through each discrete component
% compute the free energy of input, past
for cc=1:numcomp
bistar = past*pastvis(:,:,cc);
bjstar = past*pasthid(:,:,cc);
effvisbiases(:,:,cc) = repmat(visbiases(cc,:),nc,1) + bistar;
effhidbiases(:,:,cc) = repmat(hidbiases(cc,:),nc,1) + bjstar;
fe(:,cc) = crbmfe(data,vishid(:,:,cc), ...
effhidbiases(:,:,cc),effvisbiases(:,:,cc));
end
%note that adding a constant to all terms
%does not change the distribution (this prevents overflow)
fe = bsxfun(@minus,fe,min(fe,[],2)); %careful to take min over cols
expfe = exp(-fe/plottemp);
probcomp = bsxfun(@rdivide,expfe,sum(expfe,2)); %normalize
% sample a component
% sample_vector comes from Tom Minka's lightspeed toolbox:
% http://research.microsoft.com/en-us/um/people/minka/software/lightspeed/
asm = sample_vector(probcomp'); %returns row vector of assignments
figure(32); clf
imagesc(probcomp'); colormap gray; %axis off
title('discrete component posterior')
xlabel('frame')
ylabel('component')
%Posteriors/recon under each component
figure(33); clf; figure(34); clf
%this will hold the posteriors
%where we have selected different assignments
%for different frames
poshidprobs = zeros(nc,numhid,'single');
segnegdata = zeros(nc,numdims,'single');
for cc=1:numcomp
%ALL DATA%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
eta = (data./gsd)*vishid(:,:,cc) + ...
effhidbiases(:,:,cc);
hposteriors = 1./(1 + exp(-eta)); %logistic
figure(33);
subplot(numcomp,1,cc);
imagesc(hposteriors'); colormap gray; %axis off
ylabel('hidden unit')
xlabel('frame')
title(sprintf('component %d', cc))
topdown = gsd.*(hposteriors*vishid(:,:,cc)');
%This is the mean of the Gaussian
%Instead of properly sampling, negdata is just the mean
%If we want to sample from the Gaussian, we would add in
%gsd.*randn(numcases,numdims);
negdata = topdown + ... %top down connections
effvisbiases(:,:,cc);
% arbitrarily select dimensions 7, 18 to show recon
figure(34);
subplot(numcomp,2,2*(cc-1)+1);
plot(data(:,7,1)); hold on; plot(negdata(:,7),'r');
title(sprintf('component %d reconstruction (red) vs. true (blue)', cc))
subplot(numcomp,2,2*(cc-1)+2);
plot(data(:,18,1)); hold on; plot(negdata(:,18),'r');
title(sprintf('component %d reconstruction (red) vs. true (blue)', cc))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%SEGMENTED DATA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idx = find(asm==cc); %indexes cases assigned to component cc
%pass through logistic
poshidprobs(idx,:) = 1./(1 + exp(-(data(idx,:)./gsd)* vishid(:,:,cc) ...
- effhidbiases(idx,:,cc)));
topdown = gsd.*(poshidprobs(idx,:)*vishid(:,:,cc)');
%reconstruct mean-field using the selected component
segnegdata(idx,:) = topdown + ...
effvisbiases(idx,:,cc);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
figure(35); clf
imagesc(poshidprobs'); colormap gray; %axis off
ylabel('hidden')
xlabel('frame')
title('hidden posteriors using selected component')
figure(36); clf
subplot(2,1,1);
plot(data(:,7,1)); hold on; plot(segnegdata(:,7),'r');
title('mixture reconstruction (red) vs. true (blue)')
subplot(2,1,2);
plot(data(:,18,1)); hold on; plot(segnegdata(:,18),'r');
title('mixture reconstruction (red) vs. true (blue)')
drawnow;