-
Notifications
You must be signed in to change notification settings - Fork 0
/
examplesMM.m
256 lines (184 loc) · 7.54 KB
/
examplesMM.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
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
% Examples for using the Library
% Note for this entire library uses Two important variable/ structure for
% the specification of the multimodal architecture.
% 1) cvMultiModalArch : This is a cell array. A cell here (cvMultiModalArch{i}) represents the
% the architecture (number of hidden usnits in each layer)
% used for a (i th) modality.
% 2) vMainModelArch : This is a vectore which represent the architecture
% (number of hidden usnits in each layer) for the model
% on top of the multimodal structure.
% Data: we load 'MNISTtraining.mat' for the MNIST tarining dataset
% Images are stored in 'trainImg' variable and corresponding labels in
% 'trainLabel' variable.
clear;clc;
load('MNISTtraining.mat');
vMainModelArch = [100 50]; % architecture for the model on top of each Modality
cvMultiModalArch{1} = [784 70]; % architecture for first Modality
cvMultiModalArch{2} = [10 50]; % architecture for second Modality
X = [trainImg trainLabel];
%%%%%%%%%%%% unsupervisedMM/ DBN %%%%%%%%%%%%%%
opts.numepochs = 2;
opts.batchsize = 100;
opts.momentum = 0;
opts.alpha = 1;
dbnMM = dbnMMsetup(vMainModelArch, cvMultiModalArch, opts);
modality = 1;
% imshow(reshape(dbnMM.dbn{modality}.rbm{1}.W(1,:), 28, 28));
% figure; imshow(reshape(dbnMM.dbn{modality}.rbm{1}.W(2,:), 28, 28));
dbnMM = dbnMMpretrainAndLoad(dbnMM, modality, trainImg, opts);
% figure; imshow(reshape(dbnMM.dbn{modality}.rbm{1}.W(1,:), 28, 28));
% figure; imshow(reshape(dbnMM.dbn{modality}.rbm{1}.W(2,:), 28, 28));
dbnMM = dbnMMtrain(dbnMM, X, opts);
fillModality = 1;
sampleCount = 2;
Xincomp = [rand(size(trainImg)) trainLabel];
% Xincomp = [zeros(size(trainImg)) trainLabel];
generatedModality = dbnGenerateModality(dbnMM, Xincomp, fillModality, sampleCount);
% i = 10
%generatedModality = dbnGenerateModality(dbnMM, Xincomp(i,:), fillModality, sampleCount);
%Xincomp(i,end-9:end)
%imshow(reshape(generatedModality,28,28))
testModality = 1;
Xtest = [zeros(size(trainImg)) trainLabel];
Xmissing = trainImg;
sampleCount = 5;
bMultiLabel = true;
repeatCount = 1;
errorCount = dbnMMtest(dbnMM, Xtest, Xmissing, testModality, sampleCount, bMultiLabel, repeatCount);
testModality = 2;
Xtest = [trainImg zeros(size(trainLabel))];
Xmissing = trainLabel;
sampleCount = 5;
bMultiLabel = false;
repeatCount = 1;
errorCount = dbnMMtest(dbnMM, Xtest, Xmissing, testModality, sampleCount, bMultiLabel, repeatCount);
%%%%%%%%%%%% unsupervisedMM/ SAE %%%%%%%%%%%%%%
% Note: SAE are just used for pretraing and not as generative models.
saeMM = saeMMsetup(vMainModelArch, cvMultiModalArch);
% check: saeMM.sae{i}.ae{1}
opts.activation_function = 'sigm';
opts.learningRate = 1;
opts.inputZeroMaskedFraction = 0.5;
opts.momentum = 0;
opts.numepochs = 1;
opts.batchsize = 100;
modality = 1;
% imshow(reshape(saeMM.sae{1}.ae{1}.W{1}(1,2:end), 28, 28));
% figure; imshow(reshape(saeMM.sae{1}.ae{1}.W{1}(2,2:end), 28, 28));
saeMM = saeMMpretrainAndLoad(saeMM, modality, trainImg, opts);
% figure; imshow(reshape(saeMM.sae{1}.ae{1}.W{1}(1,2:end), 28, 28));
% figure; imshow(reshape(saeMM.sae{1}.ae{1}.W{1}(2,2:end), 28, 28));
saeMM = saeMMtrain(saeMM, X, opts);
%%%%%%%%%%%% unsupervisedMM/ DAE %%%%%%%%%%%%%%
daeMM = daeMMsetup(vMainModelArch, cvMultiModalArch);
opts.numepochs = 1;
opts.batchsize = 100;
daeMM.activation_function = 'sigm';
daeMM.learningRate = 1;
modality = 1;
% imshow(reshape(daeMM.W{1}{modality}(1,2:end), 28, 28));
% figure; imshow(reshape(daeMM.W{1}{modality}(1,2:end), 28, 28));
daeMM = daeMMpretrainAndLoad(daeMM, modality, trainImg, opts);
% figure; imshow(reshape(daeMM.W{1}{modality}(1,2:end), 28, 28));
% figure; imshow(reshape(daeMM.W{1}{modality}(1,2:end), 28, 28));
opts.numepochs = 2;
daeMM = daeMMtrain(daeMM, X, opts);
fillModality = 1;
sampleCount = 2;
Xincomp = [rand(size(trainImg)) trainLabel];
% Xincomp = [zeros(size(trainImg)) trainLabel];
generatedModality = daeGenerateModality(daeMM, Xincomp, fillModality, sampleCount);
% i = 20
%generatedModality = daeGenerateModality(daeMM, Xincomp(i,:), fillModality, sampleCount);
%Xincomp(i,end-9:end)
%imshow(reshape(generatedModality,28,28))
testModality = 1;
Xtest = [zeros(size(trainImg)) trainLabel];
Xmissing = trainImg;
sampleCount = 5;
bMultiLabel = true;
repeatCount = 1;
errorCount = daeMMtest(daeMM, Xtest, Xmissing, testModality, sampleCount, bMultiLabel, repeatCount);
testModality = 2;
Xtest = [trainImg zeros(size(trainLabel))];
Xmissing = trainLabel;
sampleCount = 5;
bMultiLabel = false;
repeatCount = 1;
errorCount = daeMMtest(daeMM, Xtest, Xmissing, testModality, sampleCount, bMultiLabel, repeatCount);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;clc;
load('MNISTtraining.mat');
vMainModelArch = [100 10]; % architecture for the model on top of each Modality
cvMultiModalArch{1} = [784 70]; % architecture for first Modality
cvMultiModalArch{2} = [10 50]; % architecture for second Modality
X = [trainImg trainLabel];
labels = rand(size(X,1), vMainModelArch(end)); % Random labels created to show the use of the function.
%%%%%%%%%%%% supervisedMM/ MMNN %%%%%%%%%%%%%%
% Ex. 1: No pretraning
nnMM = nnsetupMM(vMainModelArch, cvMultiModalArch);
opts.batchsize = 1000;
opts.numepochs = 2;
nnMM.activation_function = 'sigm';
nnMM = nntrainMM(nnMM, X, labels, opts);
bMultiLabel = true;
errorCount = nntestMM(nnMM, X, labels, bMultiLabel);
% Ex. 2: DBN pretraining
opts.numepochs = 2;
opts.batchsize = 100;
opts.momentum = 0;
opts.alpha = 1;
dbnMM = dbnMMsetup(vMainModelArch, cvMultiModalArch, opts);
modality = 1;
% Both the lines below are optional, any (or both) of these can be used for
% better initialization of the network
dbnMM = dbnMMpretrainAndLoad(dbnMM, modality, trainImg, opts);
dbnMM = dbnMMtrain(dbnMM, X, opts);
nnMM = nnsetupMM(vMainModelArch, cvMultiModalArch);
nnMM = loadPretrainModels(nnMM, dbnMM);
opts.batchsize = 150;
opts.numepochs = 2;
nnMM.activation_function = 'sigm';
nnMM = nntrainMM(nnMM, X, labels, opts);
bMultiLabel = true;
errorCount = nntestMM(nnMM, X, labels, bMultiLabel);
% Ex. 3: SAE pretraining
saeMM = saeMMsetup(vMainModelArch, cvMultiModalArch);
% check: saeMM.sae{i}.ae{1}
opts.activation_function = 'sigm';
opts.learningRate = 1;
opts.inputZeroMaskedFraction = 0.5;
opts.momentum = 0;
opts.numepochs = 1;
opts.batchsize = 100;
modality = 1;
% Both the lines below are optional, any (or both) of these can be used for
% better initialization of the network
saeMM = saeMMpretrainAndLoad(saeMM, modality, trainImg, opts);
saeMM = saeMMtrain(saeMM, X, opts);
nnMM = nnsetupMM(vMainModelArch, cvMultiModalArch);
nnMM = loadPretrainModels(nnMM, saeMM);
opts.batchsize = 150;
opts.numepochs = 2;
nnMM.activation_function = 'sigm';
nnMM = nntrainMM(nnMM, X, labels, opts);
bMultiLabel = true;
errorCount = nntestMM(nnMM, X, labels, bMultiLabel);
% Ex. 4: DAE pretraining
daeMM = daeMMsetup(vMainModelArch, cvMultiModalArch);
opts.numepochs = 1;
opts.batchsize = 100;
daeMM.activation_function = 'sigm';
modality = 1;
% Both the lines below are optional, any (or both) of these can be used for
% better initialization of the network
daeMM = daeMMpretrainAndLoad(daeMM, modality, trainImg, opts);
daeMM = daeMMtrain(daeMM, X, opts);
nnMM = nnsetupMM(vMainModelArch, cvMultiModalArch);
nnMM = loadPretrainModels(nnMM, daeMM);
opts.batchsize = 1000;
opts.numepochs = 2;
nnMM.activation_function = 'sigm';
nnMM = nntrainMM(nnMM, X, labels, opts);
bMultiLabel = true;
errorCount = nntestMM(nnMM, X, labels, bMultiLabel);