您的位置:首页 > 编程语言

Deep Learning 2_深度学习UFLDL教程:矢量化编程(斯坦福大学深度学习教程)

2015-10-29 15:52 981 查看

1前言

本节主要是让人用矢量化编程代替效率比较低的for循环。

在前一节的Sparse Autoencoder练习中已经实现了矢量化编程,所以与前一节的区别只在于本节训练集是用MINIST数据集,而上一节训练集用的是从10张图片中随机选择的8*8的10000张小图块。综上,只需要在前一节的代码中稍微修改一下就可。

2练习步骤

1.下载数据集及UFLDL提供的加载数据集的函数,并把他们和上节程序放在同一文件夹中。要注意的是UFLDL提供的加载数据集的函数中程序用的数据集名称是train-images-idx3-ubyte,要把他改为train-images.idx3-ubyte。可用如下程序检查MINIST数据集是否可加载成功。

% Change the filenames if you've saved the files under different names
% On some platforms, the files might be saved as
% train-images.idx3-ubyte / train-labels.idx1-ubyte
images = loadMNISTImages('train-images.idx3-ubyte');
labels = loadMNISTLabels('train-labels.idx1-ubyte');

% We are using display_network from the autoencoder code
display_network(images(:,1:100)); % Show the first 100 images
disp(labels(1:10));


  运行之后得到如下结果就表示已经可以正确加载:



2.矢量化Sparse Autoencoder程序,即上一节程序,因上节已实现,故此步骤可免去。

3.学习手写数字库的特征。

前言中已经说了,本步只需要在上节中稍微修改一下即可,具体如下:

①修改初始参数,把train.m文件中把step0里面的各个参数调整成这样:

visibleSize = 28*28;   % number of input units 输入层单元数
hiddenSize = 196;     % number of hidden units隐藏层单元数
sparsityParam = 0.1;   % desired average activation of the hidden units.稀疏值
% (This was denoted by the Greek alphabet rho, which looks like a lower-case "p",
%  in the lecture notes).
lambda = 3e-3;     % weight decay parameter 权重衰减系数
beta = 3;            % weight of sparsity penalty term稀疏值惩罚项的权重


②修改训练集,把step1里面的patches的产生改为:

%% STEP 1: Implement sampleIMAGES  第1步:实现图片采样
%
%  实现图片采样后,函数display_network从训练集中随机显示200张
%  After implementing sampleIMAGES, the display_network command should
%  display a random sample of 200 patches from the dataset
images = loadMNISTImages('train-images.idx3-ubyte');
patches = images(:,1:10000);

% patches = sampleIMAGES;
display_network(patches(:,randi(size(patches,2),200,1)),8);%从10000张中随机选择200张显示

%  Obtain random parameters theta初始化参数向量theta
theta = initializeParameters(hiddenSize, visibleSize);


 4.其他一切不变,但是为了提高效率,可把train.m中的 STEP 3: Gradient Checking这步注释掉,因为在本例中训练集更大,梯度检查会比较慢。然后运行train.m可得到可视化结果为:

Elapsed time is 365.887537 seconds.



……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: