comparison core/magnatagatune/tests_evals/rbm_subspace/Exp_template.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 % Experiment code templat %
3 % Project: sub-euclidean distance for music similarity %
4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 %% Load features
6 feature_file = 'rel_music_raw_features.mat';
7 vars = whos('-file', feature_file);
8 A = load(feature_file,vars(1).name,vars(2).name,vars(3).name,vars(4).name);
9 raw_features = A.(vars(1).name);
10 indices = A.(vars(2).name);
11 tst_inx = A.(vars(3).name);
12 trn_inx = A.(vars(4).name);
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 % Define directory to save parameters & results
15 % dir = '/home/funzi/Documents/';
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17 dmr = [0 5 10 20 30 50]; % dimension reduction by PCA
18 ws = [0 5 10 20 30 50 70]; % window size
19 % parameters of rbm (if it is used for extraction)
20 hidNum = [30 50 100 500];
21 lr_1 = [0.05 0.1 0.5];
22 lr_2 = [0.1 0.5 0.7];
23 mmt = [0.02 0.05 0.1];
24 cost = [0.00002 0.01 0.1];
25
26 %% Select parameters (if grid-search is not applied)
27 di = 1;
28 wi = 1;
29 hi = 1;
30 l1i = 1;
31 l2i = 1;
32 mi = 1;
33 ci = 1;
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 % If grid search is define
36 % log_file = strcat(dir,'exp_.mat');
37 % inx = resume_from_grid(log_file,8);
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %% Feature extraction
40 EXT_TYPE = 2;
41 switch (EXT_TYPE)
42 case 1 % Using PCA
43 assert(~exist('OCTAVE_VERSION'),'This script cannot run in octave');
44 coeff = princomp(raw_features);
45 coeff = coeff(:,1:end-dmr(di)); % Change value of dmr(di) to reduce the dimensionality
46 features = raw_features*coeff;
47 % normalizing
48 mm = minmax(features')';
49 inn= (find(mm(1,:)~=mm(2,:)));
50 mm = mm(:,inn);
51 features = features(:,inn);
52 features = (features-repmat(mm(1,:),size(features,1),1))./(repmat(mm(2,:),size(features,1),1)-repmat(mm(1,:),size(features,1),1));
53 case 2 % Using rbm
54 conf.hidNum = hidNum(hi);
55 conf.eNum = 100;
56 conf.sNum = size(raw_features,1);
57 conf.bNum = 1;
58 conf.gNum = 1;
59 conf.params = [lr_1(l1i) lr_2(l2i) mmt(mi) cost(ci)];
60 conf.N = 50;
61 conf.MAX_INC = 10;
62 W1 = zeros(0,0);
63 [W1 vB1 hB1] = training_rbm_(conf,W1,raw_features);
64 features = raw_features*W1 + repmat(hB1,conf.sNum,1);
65 end
66
67 %% Sub-euclidean computation
68 num_case = size(trn_inx,1);
69 trnd_12 = cell(1,num_case);
70 trnd_13 = cell(1,num_case);
71 tstd_12 = cell(1,num_case);
72 tstd_13 = cell(1,num_case);
73
74 w = ws(wi);
75
76 % w = subspace window size
77 if w == 0 % trnd_12 = d(a,b) , trnd_13= d(a,c)
78 for i = 1:num_case % over all cross-validation folds (num_case)
79 [trnd_12{i} trnd_13{i}] = simple_dist(trn_inx{i},features,indices);
80 [tstd_12{i} tstd_13{i}] = simple_dist(tst_inx{i},features,indices);
81 end
82 else
83 for i = 1:num_case % for w > 1
84 [trnd_12{i} trnd_13{i}] = conv_euclidean_dist(trn_inx{i},features,indices,w,1); %% normalize is better than no normalize
85 [tstd_12{i} tstd_13{i}] = conv_euclidean_dist(tst_inx{i},features,indices,w,1);
86 end
87 end
88 %% Data preparation
89 trn_dat1 = cell(1,num_case);
90 trn_dat2 = cell(1,num_case);
91 tst_dat1 = cell(1,num_case);
92 tst_dat2 = cell(1,num_case);
93
94 for i=1:num_case
95 %=> Compute hypothesis
96 trn_dat1{i} = trnd_13{i} - trnd_12{i};
97 trn_dat2{i} = trnd_12{i} - trnd_13{i};
98 tst_dat1{i} = tstd_13{i} - tstd_12{i};
99 tst_dat2{i} = tstd_12{i} - tstd_13{i};
100
101 % => Normalize using logistic (lost the range)
102 % trn_dat1{i} = logistic(trn_dat1{i});
103 % trn_dat2{i} = logistic(trn_dat2{i});
104 % tst_dat1{i} = logistic(tst_dat1{i});
105 % tst_dat2{i} = logistic(tst_dat2{i});
106
107 %=> Normalize using min-max
108 % mm = minmax(trn_dat1{i}')';
109 % inn= find(mm(1,:)~=mm(2,:));
110 % mm = mm(:,inn);
111 % trn_dat1{i} =
112 % (trn_dat1{i}(:,inn)-repmat(mm(1,:),size(trn_dat1{i},1),1))./repmat(mm(2,:)-mm(1,:),size(trn_dat1{i},1),1);
113 % mm = minmax(tst_dat1{i}')';
114 % inn= find(mm(1,:)~=mm(2,:));
115 % mm = mm(:,inn);
116 % tst_dat1{i} = (tst_dat1{i}(:,inn)-repmat(mm(1,:),size(tst_dat1{i},1),1))./repmat(mm(2,:)-mm(1,:),size(tst_dat1{i},1),1);
117 %
118 % mm = minmax(trn_dat2{i}')';
119 % inn= find(mm(1,:)~=mm(2,:));
120 % mm = mm(:,inn);
121 % trn_dat2{i} = (trn_dat2{i}(:,inn)-repmat(mm(1,:),size(trn_dat2{i},1),1))./repmat(mm(2,:)-mm(1,:),size(trn_dat2{i},1),1);
122
123 % mm = minmax(tst_dat2{i}')';
124 % inn= find(mm(1,:)~=mm(2,:));
125 % mm = mm(:,inn);
126 % tst_dat2{i} = (tst_dat2{i}(:,inn)-repmat(mm(1,:),size(tst_dat2{i},1),1))./repmat(mm(2,:)-mm(1,:),size(tst_dat2{i},1),1);
127
128 % => normalize from [-1 1] to [0 1]
129 trn_dat1{i} = (trn_dat1{i}+1)/2;
130 trn_dat2{i} = (trn_dat2{i}+1)/2;
131 tst_dat1{i} = (tst_dat1{i}+1)/2;
132 tst_dat2{i} = (tst_dat2{i}+1)/2;
133 end
134 correct = 0; % correct rate
135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 %% CODE HERE %%
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138
139 fprintf('Correct = %f\n',correct);
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % Using the logging function to save paramters
142 % and the result for plotting or in grid search
143 % logging(log_file,[i1 i2 i3 i4 i5 correct]);
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145
146 clear;