您的位置:首页 > 其它

UFLDL教程 Exercise:Sparse Autoencoder(答案)

2014-10-11 16:17 351 查看
注:本人刚刚着手DL,所以博文也仅属于学习笔记范畴,每篇博文我都会将内容摘抄地址附在最上面,如果冒犯原作者,请见谅哈~))

参考链接:

/article/5196566.html

网页教程参考:

Exercise:Sparse Autoencoder

Exercise:Sparse Autoencoder

Step1 generate training set

在sampleIMAGES.m文件中完成生成训练集的代码,如下,tic和toc用来计时的:

tic
image_size=size(IMAGES);
i=randi(image_size(1)-patchsize+1,1,numpatches);
j=randi(image_size(2)-patchsize+1,1,numpatches);
k=randi(image_size(3),1,numpatches);
for num=1:numpatches
patches(:,num)=reshape(IMAGES(i(num):i(num)+patchsize-1,j(num):j(num)+patchsize-1,k(num)),1,patchsize*patchsize);
end
toc


2. Step2 Sparse autoencoder objective

在sparseAutoencoderCost.m文件中完成前向传播和后向传播等相关代码,如下:

%1.forward propagation
data_size=size(data);
active_value2=repmat(b1,1,data_size(2));
active_value3=repmat(b2,1,data_size(2));
active_value2=sigmoid(W1*data+active_value2);
active_value3=sigmoid(W2*active_value2+active_value3);
%2.computing error term and cost
ave_square=sum(sum((active_value3-data).^2)./2)/data_size(2);
weight_decay=lambda/2*(sum(sum(W1.^2))+sum(sum(W2.^2)));

p_real=sum(active_value2,2)./data_size(2);
p_para=repmat(sparsityParam,hiddenSize,1);
sparsity=beta.*sum(p_para.*log(p_para./p_real)+(1-p_para).*log((1-p_para)./(1-p_real)));
cost=ave_square+weight_decay+sparsity;

delta3=(active_value3-data).*(active_value3).*(1-active_value3);
average_sparsity=repmat(sum(active_value2,2)./data_size(2),1,data_size(2));
default_sparsity=repmat(sparsityParam,hiddenSize,data_size(2));
sparsity_penalty=beta.*(-(default_sparsity./average_sparsity)+((1-default_sparsity)./(1-average_sparsity)));
delta2=(W2'*delta3+sparsity_penalty).*((active_value2).*(1-active_value2));
%3.backword propagation
W2grad=delta3*active_value2'./data_size(2)+lambda.*W2;
W1grad=delta2*data'./data_size(2)+lambda.*W1;
b2grad=sum(delta3,2)./data_size(2);
b1grad=sum(delta2,2)./data_size(2);

3. Step3 Gradient checking

梯度检查,在computeNumericalGradient.m文件中完成梯度检查的相关代码,如下:
EPSILON=0.0001;
for i=1:size(theta)
theta_plus=theta;
theta_minu=theta;
theta_plus(i)=theta_plus(i)+EPSILON;
theta_minu(i)=theta_minu(i)-EPSILON;
numgrad(i)=(J(theta_plus)-J(theta_minu))/(2*EPSILON);
end

4. Step4 Train the sparse autoencoder
执行train.m文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: