view core/magnatagatune/tests_evals/rbm_subspace/gradient_ascent.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
line wrap: on
line source
function [Ws cr_] = gradient_ascent(d_12,d_13,lr,mm,cost)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% gradient descent                                                              %
% sontran2013                                                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pd_num = size(d_12,2);
Ws     = cell(1,pd_num);
DWs    = cell(1,pd_num);

for i = 1:pd_num
Ws{i} = 0.01*ones(1,size(d_12{i},2));
DWs{i} = zeros(size(Ws{i}));
end
plot_ = 0;
if plot_, h = plot(nan); end
max_ = 0;
eNum = 50;
for j=1:eNum
    cr_ = 0;    
    for i=1:pd_num
        if j>1
        if sum(sum(isinf(Ws{i}))) ~=0 || sum(sum(isnan(Ws{i}))) ~=0, break, end
        diff = sum(d_13{i}-d_12{i},1)/size(d_12{i},1);
        DWs{i} = lr*(diff-cost*Ws{i}) + mm*DWs{i};
        Ws{i} = Ws{i} + DWs{i};
        end
        cr_ = cr_ + sum(((d_13{i}-d_12{i})*Ws{i}' > 0))/size(d_12{i},1);        
    end
    cr_ = cr_/pd_num;
    if cr_ > max_
        max_ = cr_;
        W_max = Ws;
    end
    if plot_
        cr_plot(j) = cr_;
        axis([0 (eNum+1) 0 1]);
        set(h,'YData',cr_plot);
        drawnow;
    end
end
%max
Ws = W_max;
cr_ = max_;
end