Demo entry 6357406

matlab

   

Submitted by anonymous on Apr 22, 2017 at 10:59
Language: Matlab. Code size: 1.3 kB.

%向前计算值
sampleNum=size(data,2);%样本数量
hidden=W1*data+repmat(w1,1,sampleNum);%隐层计算值
active_hidden=sigmoid(hidden);%隐层激活函数
output=W2*active_hidden+repmat(w2,1,sampleNum);%输出层计算值
active_output=sigmoid(output);%输出层激活函数
%向后计算梯度
delta_active_output=(1/sampleNum)*(active_output-data);
delta_output=delta_active_output.*(active_output.*(1-active_output));
delta_W2=delta_output*active_hidden';
w2grad=delta_output*ones(size(output,2),1);
W2grad=delta_W2+lambda*W2;
%W2grad=ones(size(delta_W2+lambda*W2));
%w2grad=zeros(size(delta_output*ones(size(output,2),1)));

rho=mean(active_hidden,2);%均值
W1_grad_temp=(beta/sampleNum)*(-sparsityParam./rho+(1-sparsityParam)./(1-rho));
delta_hidden=(W2'*delta_output+repmat(W1_grad_temp,1,sampleNum)).*(active_hidden.*(1-active_hidden));
delta_W1=delta_hidden*data';
W1grad=delta_W1+lambda*W1;
w1grad=delta_hidden*ones(sampleNum,1);
%W1grad=ones(size(delta_W1));
%w1grad=zeros(size(delta_W1*ones(size(W1,2),1)));


%计算cost function
temp1=(active_output-data).^2*0.5;
cost1=sum(temp1(:))/sampleNum;%cost function第一部分
cost2=(lambda/2)*(sum(W1(:).^2)+sum(W2(:).^2));%cost function第二部分
temp3=(sparsityParam*log(sparsityParam./rho)+(1-sparsityParam)*log((1-sparsityParam)./(1-rho)));
cost3=beta*sum(temp3(:));%cost function第三部分
cost=cost1+cost2+cost3;

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).