UFLDL Exercise:Sparse Autoencoder

本文是作者初次尝试UFLDL教程的稀疏自编码器练习,通过实现sampleIMAGES、gradient checking和sparseAutoencoderCost代码,探讨了权重衰减和稀疏性在神经网络训练中的重要性。在训练过程中,作者发现不使用权重衰减和稀疏约束会导致较差的效果。
摘要由CSDN通过智能技术生成

之前虽然看了ufldl的教程,但是没去做他的练习。作为一个刚刚入门机器学习的学生,还是不能偷懒,所以趁今天有时间做了第一个练习题Sparse Autoencoder

下面贴下代码 还有讲下做的过程中发现的一些问题。

STEP 1: Implement sampleIMAGES

sampleIMAGES.m

function patches = sampleIMAGES()
% sampleIMAGES
% Returns 10000 patches for training

load IMAGES;    % load images from disk 

patchsize = 8;  % we'll use 8x8 patches 
numpatches = 10000;

% Initialize patches with zeros.  Your code will fill in this matrix--one
% column per patch, 10000 columns. 
patches = zeros(patchsize*patchsize, numpatches);

%% ---------- YOUR CODE HERE --------------------------------------
%  Instructions: Fill in the variable called "patches" using data 
%  from IMAGES.  
%  
%  IMAGES is a 3D array containing 10 images
%  For instance, IMAGES(:,:,6) is a 512x512 array containing the 6th image,
%  and you can type "imagesc(IMAGES(:,:,6)), colormap gray;" to visualize
%  it. (The contrast on these images look a bit off because they have
%  been preprocessed using using "whitening."  See the lecture notes for
%  more details.) As a second example, IMAGES(21:30,21:30,1) is an image
%  patch corresponding to the pixels in the block (21,21) to (30,30) of
%  Image 1
image_size = size(IMAGES); %图像大小
for i=1:numpatches
    x = randi(image_size(1) - patchsize); %随机得到patch的最小x坐标
    y = randi(image_size(2) - patchsize); %随机得到patch的最小y坐标
    patches(:,i) = reshape(IMAGES(x:x+patchsize-1,y:y+patchsize-1,randi(image_size(3))),patchsize*patchsize,1); %随机选择一个张图片用上面得到坐标进行sample得到patch
end




%% ---------------------------------------------------------------
% For the autoencoder to work well we need to normalize the data
% Specifically, since the output of the network is bounded between [0,1]
% (due to the sigmoid activation function), we have to make sure 
% the range of pixel values is also bounded between [0,1]
patches = normalizeData(patches);

end


%% ---------------------------------------------------------------
function patches = normalizeData(patches)

% Squash data to [0.1, 0.9] since we use sigmoid as the activation
% function in the output layer

% Remove DC (mean of images). 
patches = bsxfun(@minus, patches, mean(patches));

% Truncate to +/-3 standard deviations and scale to -1 to 1
pstd = 3 * std(patches(:));
patches = max(min(patches, pstd), -pstd) / pstd;

% Rescale from [-1,1] to [0.1,0.9]
patches = (patches + 1) * 0.4 + 0.1;

end

测试下sampleIMAGES.m

patches = sampleIMAGES;
display_network(patches(:,randi(size(patches,2),200,1)),8);
得到如下的图片,说明代码没问题 大笑
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值