wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % Experiment with gradient ascent % 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: feature_file = 'rel_music_raw_features+simdata_ISMIR12.mat'; wolffd@0: 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: %% Params setting 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 = 0; wolffd@0: lr_1 = 0; wolffd@0: lr_2 = 0; wolffd@0: mmt = 0; wolffd@0: cost = 0; wolffd@0: %% Select parameters (if grid-search is not applied) wolffd@0: di = 1; wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % If grid search is define wolffd@0: % Define directory to save parameters & results wolffd@0: if ~isempty(findstr('WIN',computer())) wolffd@0: dir = 'C:\Pros\Experiments\ISMIR_2013\grad\'; % In windows platform wolffd@0: dlm = '\'; wolffd@0: elseif ~isempty(findstr('linux',computer())) || ~isempty(findstr('LNX',computer())) wolffd@0: dir = '/home/funzi/Documents/Experiments/ISMIR_2013/grad/'; % In lunix platform wolffd@0: dlm = '/'; wolffd@0: end wolffd@0: wolffd@0: EXT_TYPE = 2; wolffd@0: switch (EXT_TYPE) wolffd@0: case 1 wolffd@0: dir = strcat(dir,'pca',dlm); wolffd@0: case 2 wolffd@0: dir = strcat(dir,'rbm',dlm); wolffd@0: wolffd@0: hidNum = [100 500 1000 1200]; wolffd@0: lr_1 = [0.5 0.7]; wolffd@0: lr_2 = [0.7]; wolffd@0: mmt = [0.1]; wolffd@0: cost = [0.00002]; wolffd@0: otherwise wolffd@0: dir = strcat(dir,'none',dlm); wolffd@0: end wolffd@0: wolffd@0: w_num = size(ws,2); wolffd@0: wolffd@0: for iiii = 1:200 % set the higher range to search for better features in case of ext using rbm wolffd@0: log_file = strcat(dir,'exp',num2str(iiii),'.mat') wolffd@0: inx = resume_from_grid(log_file,8 + w_num); wolffd@0: if inx(end-w_num+1:end)==ones(1,w_num) wolffd@0: max_= zeros(1,w_num); wolffd@0: else wolffd@0: max_ = inx(end-w_num+1:end); wolffd@0: end wolffd@0: wolffd@0: results = zeros(1,w_num); wolffd@0: W_max = cell(1,w_num); wolffd@0: vB_max = cell(1,w_num); wolffd@0: hB_max = cell(1,w_num); wolffd@0: Ws_max = cell(1,w_num); wolffd@0: wolffd@0: for hi = inx(1):size(hidNum,2) wolffd@0: for l1i = inx(2):size(lr_1,2) wolffd@0: % for l1i = inx(3):size(lr_2,2) wolffd@0: for mi = inx(4):size(mmt,2) wolffd@0: for ci = inx(5):size(cost,2) wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% Feature extraction wolffd@0: features = raw_features; 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:6); % best = 6 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_1(l1i) 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 = logistic(raw_features*W1 + repmat(hB1,conf.sNum,1)); wolffd@0: otherwise 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: end wolffd@0: wolffd@0: for wi = inx(6):w_num wolffd@0: %% Sub-euclidean computation wolffd@0: w = ws(wi); % w = subspace window size wolffd@0: num_case = size(trn_inx,1); wolffd@0: [trnd_12 trnd_13] = subspace_distances(trn_inx,features,indices,w,0); wolffd@0: [tstd_12 tstd_13] = subspace_distances(tst_inx,features,indices,w,0); wolffd@0: cr_ = 0; % correct rate for training wolffd@0: cr = 0; % correct rate for testing wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% CODE HERE %% wolffd@0: [Ws cr_] = gradient_ascent(trnd_12,trnd_13,0.1,0.1,0.00002); wolffd@0: wolffd@0: for i = 1:num_case wolffd@0: cr = cr + sum((tstd_13{i}-tstd_12{i})*Ws{i}' > 0, 1)/size(tstd_12{i},1); wolffd@0: end wolffd@0: cr = cr/num_case; wolffd@0: if cr_>max_(wi) wolffd@0: max_(wi) = cr_; wolffd@0: results(wi) = cr; wolffd@0: if EXT_TYPE==2 wolffd@0: W_max{wi} = W1; wolffd@0: vB_max{wi} = vB1; wolffd@0: hB_max{wi} = hB1; wolffd@0: Ws_max{wi} = Ws; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: fprintf('[window|train|test]= %2d |%f |%f\n',w,cr_,cr); wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % Using the logging function to save paramters wolffd@0: % and the result for plotting or in grid search wolffd@0: switch EXT_TYPE wolffd@0: case 1 wolffd@0: % logging(log_file,[100 100 100 100 100 wi cr_ cr max_]); wolffd@0: case 2 wolffd@0: logging(log_file,[hi l1i l1i mi ci wi cr_ cr max_ conf.hidNum conf.eNum conf.params]); wolffd@0: otherwise wolffd@0: logging(log_file,[100 100 100 100 100 wi cr_ cr max_]); wolffd@0: end wolffd@0: end wolffd@0: inx(6)=1; wolffd@0: end wolffd@0: inx(5) = 1; wolffd@0: end wolffd@0: inx(4) = 1; wolffd@0: end wolffd@0: inx(2) = 1; wolffd@0: end wolffd@0: inx(1) = 1; wolffd@0: %% Test on best features wolffd@0: wolffd@0: save(strcat(dir,'res_',num2str(iiii),'.mat'),'max_','results','W_max','vB_max','hB_max','Ws_max','ws'); wolffd@0: [dummy pos] = max(max_); wolffd@0: fprintf('Accuracy (RBM best fts): w = %d train = %f test = %f\n',ws(pos),max_(pos),results(pos)); wolffd@0: clc; wolffd@0: end wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: clear;