Exercise:Vectorization 代码示例
练习参考Vectorization
需要在练习Sparse Autoencoder的基础上做。可将Sparse Autoencoder拷贝一份并重命名为Vectorization。下载教程提供的数据,解压缩后将train-images-idx3-ubyte、train-labels-idx1-ubyte、loadMNISTImages.m和loadMNISTLabels.m文件放在train.m文件同一级目录下。
修改train.m为:
visibleSize = 28*28;
hiddenSize = 196;
sparsityParam = 0.1;
lambda = 3e-3;
beta = 3;
images = loadMNISTImages('train-images-idx3-ubyte');
labels = loadMNISTLabels('train-labels-idx1-ubyte');
display_network(images(:,1:100));
disp(labels(1:10));
patches=images(:,1:10000);
theta = initializeParameters(hiddenSize, visibleSize);
[cost, grad] = sparseAutoencoderCost(theta, visibleSize, hiddenSize, lambda, ...
sparsityParam, beta, patches);
addpath minFunc/
options.Method = 'lbfgs';
options.maxIter = 400;
options.display = 'on';
[opttheta, cost] = minFunc( @(p) sparseAutoencoderCost(p, ...
visibleSize, hiddenSize, ...
lambda, sparsityParam, ...
beta, patches), ...
theta, options);
W1 = reshape(opttheta(1:hiddenSize*visibleSize), hiddenSize, visibleSize);
display_network(W1', 12);
运行train.m,等待若干分钟,运算得到可视化结果如教程所示。