wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % Experiment code templat % wolffd@0: % Project: sub-euclidean distance for music similarity % wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% Load features wolffd@0: feature_file = 'rel_music_raw_features.mat'; wolffd@0: vars = whos('-file', feature_file); wolffd@0: A = load(feature_file,vars(1).name,vars(2).name,vars(3).name,vars(4).name); wolffd@0: raw_features = A.(vars(1).name); wolffd@0: indices = A.(vars(2).name); wolffd@0: tst_inx = A.(vars(3).name); wolffd@0: trn_inx = A.(vars(4).name); wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % Define directory to save parameters & results wolffd@0: % dir = '/home/funzi/Documents/'; wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: dmr = [0 5 10 20 30 50]; % dimension reduction by PCA wolffd@0: ws = [0 5 10 20 30 50 70]; % window size wolffd@0: % parameters of rbm (if it is used for extraction) wolffd@0: hidNum = [30 50 100 500]; wolffd@0: lr_1 = [0.05 0.1 0.5]; wolffd@0: lr_2 = [0.1 0.5 0.7]; wolffd@0: mmt = [0.02 0.05 0.1]; wolffd@0: cost = [0.00002 0.01 0.1]; wolffd@0: wolffd@0: %% Select parameters (if grid-search is not applied) wolffd@0: di = 1; wolffd@0: wi = 1; wolffd@0: hi = 1; wolffd@0: l1i = 1; wolffd@0: l2i = 1; wolffd@0: mi = 1; wolffd@0: ci = 1; wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % If grid search is define wolffd@0: % log_file = strcat(dir,'exp_.mat'); wolffd@0: % inx = resume_from_grid(log_file,8); wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% Feature extraction wolffd@0: EXT_TYPE = 2; wolffd@0: switch (EXT_TYPE) wolffd@0: case 1 % Using PCA wolffd@0: assert(~exist('OCTAVE_VERSION'),'This script cannot run in octave'); wolffd@0: coeff = princomp(raw_features); wolffd@0: coeff = coeff(:,1:end-dmr(di)); % Change value of dmr(di) to reduce the dimensionality wolffd@0: features = raw_features*coeff; wolffd@0: % normalizing wolffd@0: mm = minmax(features')'; wolffd@0: inn= (find(mm(1,:)~=mm(2,:))); wolffd@0: mm = mm(:,inn); wolffd@0: features = features(:,inn); wolffd@0: 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: case 2 % Using rbm wolffd@0: conf.hidNum = hidNum(hi); wolffd@0: conf.eNum = 100; wolffd@0: conf.sNum = size(raw_features,1); wolffd@0: conf.bNum = 1; wolffd@0: conf.gNum = 1; wolffd@0: conf.params = [lr_1(l1i) lr_2(l2i) mmt(mi) cost(ci)]; wolffd@0: conf.N = 50; wolffd@0: conf.MAX_INC = 10; wolffd@0: W1 = zeros(0,0); wolffd@0: [W1 vB1 hB1] = training_rbm_(conf,W1,raw_features); wolffd@0: features = raw_features*W1 + repmat(hB1,conf.sNum,1); wolffd@0: end wolffd@0: wolffd@0: %% Sub-euclidean computation wolffd@0: num_case = size(trn_inx,1); wolffd@0: trnd_12 = cell(1,num_case); wolffd@0: trnd_13 = cell(1,num_case); wolffd@0: tstd_12 = cell(1,num_case); wolffd@0: tstd_13 = cell(1,num_case); wolffd@0: wolffd@0: w = ws(wi); wolffd@0: wolffd@0: % w = subspace window size wolffd@0: if w == 0 % trnd_12 = d(a,b) , trnd_13= d(a,c) wolffd@0: for i = 1:num_case % over all cross-validation folds (num_case) wolffd@0: [trnd_12{i} trnd_13{i}] = simple_dist(trn_inx{i},features,indices); wolffd@0: [tstd_12{i} tstd_13{i}] = simple_dist(tst_inx{i},features,indices); wolffd@0: end wolffd@0: else wolffd@0: for i = 1:num_case % for w > 1 wolffd@0: [trnd_12{i} trnd_13{i}] = conv_euclidean_dist(trn_inx{i},features,indices,w,1); %% normalize is better than no normalize wolffd@0: [tstd_12{i} tstd_13{i}] = conv_euclidean_dist(tst_inx{i},features,indices,w,1); wolffd@0: end wolffd@0: end wolffd@0: %% Data preparation wolffd@0: trn_dat1 = cell(1,num_case); wolffd@0: trn_dat2 = cell(1,num_case); wolffd@0: tst_dat1 = cell(1,num_case); wolffd@0: tst_dat2 = cell(1,num_case); wolffd@0: wolffd@0: for i=1:num_case wolffd@0: %=> Compute hypothesis wolffd@0: trn_dat1{i} = trnd_13{i} - trnd_12{i}; wolffd@0: trn_dat2{i} = trnd_12{i} - trnd_13{i}; wolffd@0: tst_dat1{i} = tstd_13{i} - tstd_12{i}; wolffd@0: tst_dat2{i} = tstd_12{i} - tstd_13{i}; wolffd@0: wolffd@0: % => Normalize using logistic (lost the range) wolffd@0: % trn_dat1{i} = logistic(trn_dat1{i}); wolffd@0: % trn_dat2{i} = logistic(trn_dat2{i}); wolffd@0: % tst_dat1{i} = logistic(tst_dat1{i}); wolffd@0: % tst_dat2{i} = logistic(tst_dat2{i}); wolffd@0: wolffd@0: %=> Normalize using min-max wolffd@0: % mm = minmax(trn_dat1{i}')'; wolffd@0: % inn= find(mm(1,:)~=mm(2,:)); wolffd@0: % mm = mm(:,inn); wolffd@0: % trn_dat1{i} = wolffd@0: % (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: % mm = minmax(tst_dat1{i}')'; wolffd@0: % inn= find(mm(1,:)~=mm(2,:)); wolffd@0: % mm = mm(:,inn); wolffd@0: % 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: % wolffd@0: % mm = minmax(trn_dat2{i}')'; wolffd@0: % inn= find(mm(1,:)~=mm(2,:)); wolffd@0: % mm = mm(:,inn); wolffd@0: % 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); wolffd@0: wolffd@0: % mm = minmax(tst_dat2{i}')'; wolffd@0: % inn= find(mm(1,:)~=mm(2,:)); wolffd@0: % mm = mm(:,inn); wolffd@0: % 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: wolffd@0: % => normalize from [-1 1] to [0 1] wolffd@0: trn_dat1{i} = (trn_dat1{i}+1)/2; wolffd@0: trn_dat2{i} = (trn_dat2{i}+1)/2; wolffd@0: tst_dat1{i} = (tst_dat1{i}+1)/2; wolffd@0: tst_dat2{i} = (tst_dat2{i}+1)/2; wolffd@0: end wolffd@0: correct = 0; % correct rate wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% CODE HERE %% wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: fprintf('Correct = %f\n',correct); wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % Using the logging function to save paramters wolffd@0: % and the result for plotting or in grid search wolffd@0: % logging(log_file,[i1 i2 i3 i4 i5 correct]); wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: clear;