annotate core/magnatagatune/tests_evals/rbm_subspace/Exp_normalise_deltas.m @ 0:e9a9cd732c1e tip

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