机器学习向量化练习
機器學習向量化練習
在先前的練習里面,我們已經通過對自然圖像完成了一個稀疏自編碼的練習。在這次我們將通過向量化來使我們運行速度更快,并且我們將把它應用到手寫數字里面。
數據下載
- MNIST Dataset (Training Images)
- MNIST Dataset (Training Labels)
- Support functions for loading MNIST in Matlab
第一步:向量化你的稀疏自編碼
這一步我已經完成,具體可以看我上一次的博客。
第二步:學習手寫數字的特征
1.我們先把train-images-idx3-ubyte.gz和mnistHelper這個兩個文件先解壓開,然后把文件放到我們上次的稀疏自編碼的地方。
2.接下去我們就需要進入上次我們圖片采樣sampleIMAGES.m的地方,并把代碼改成
function patches = sampleIMAGES()
% sampleIMAGES
% Returns 10000 patches for training
%load IMAGES; ? ?% load images from disk?
%use mnist data
patchsize = 28; ?% 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
%select 2000 patches from image1
%select 2000 patches from image2
%.....
% for k=1:4
% ? ? for i=1:50
% ? ? ? ? for j=1:50
% ? ? ? ? ? ? patch=IMAGES(8*i-7:8*i,j*8-7:8*j,k);
% ? ? ? ? ? ? patches(:,2500*(k-1)+50*(i-1)+j)=reshape(patch,64,1);
% ? ? ? ? end
% ? ? end
% end
images = loadMNISTImages('train-images-idx3-ubyte');
patches=images(:,1:10000);
然后接著再進去train.m文件
把模型的參數改成這樣
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 ? ? ??
%%---------------------------------------------------------------
% 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
然后其他的參數不變。
接著我們就可以運行train了。
最后,在400次迭代后,你的稀疏自編碼應該學會了筆畫特征。換句話說,我們的程序將會學習圖片里面的筆畫的特征。我們在程序結束后可以看到這樣一幅圖
如果你的自編碼是有問題的,那你可能得到以下的圖
如果你的圖片像這樣,請你檢查你的代碼和參數。
總結
- 上一篇: 台式电脑怎么看计算机型号,怎么查看台式机
- 下一篇: java saxexception_SA