本实验是用线性解码器的sparse autoencoder来训练stl-10数据库图片中8*8大小RGB patch块的特征。之前的试验中,我们的训练图像都是灰度图像,对于RGB图像可以采用相同的方法,只需把RGB图像的三个通道向量按照rgb的顺序排列更长的向量即可。
1、 线性解码器简介
线性解码器和 稀疏自动编码的整体结构都是类似的,只是线性解码器的输出层的激励函数为 恒等式f(z) = z,而稀疏自动编码为 非线性函数,比如sigmoid函数、tanh函数等,那为什么为出现两者不同的激励函数呢。我们以三层神经网络为例,输出层的计算公式如下:
其中a(3)是第二层的输出,在自编码器中,我们希望a(3)近似重构了输入x=a(1)。
我们在稀疏自动编码采用sigmoid激励函数,把输出数据控制在[0,1]范围,如果输入数据能够方便缩放到 [0,1] 中(MNIST手写数字数据集),那稀疏自动编码可以满足要求。但是,有些数据很难把数据缩放到[0,1]之间(比如PCA 白化处理的输入),如果此时输入数据的范围为[0,10],仍然使用sigmoid激励函数保证输出为[0,1],此时不能得到输出重构输入的要求,在这种情况下,线性解码器的优势就体现出来了。
2、 神经网络结构
实验中为三层神经网络,输入层8*8*3个neuron,隐含层为400个neuron(都不包括bias结点),输出层为8*8*3个neuron。
3、 数据
实验中的数据采用的100,000个8 x 8的RGB patch块,这些patch是从STL-10图像集中随机采样得到的。STL-10数据中由5000个训练数据和8000个训练数据组成,每一个数据是大小为96x96标注的彩色图像,这数据属于airplane, bird, car, cat, deer, dog, horse, monkey, ship, truck十个类中的一类。
4、 预处理(ZCA白化)
本实验中采用来预处理,降低输入数据的冗余性,从而降低特征之间相关性,以及使所有特征具有相同的方差。
下图是把训练数据中的前100个patch进行显示。
原始数据 ZCA白化处理后
5、 实验结果
下图是通过线性解码器从STL-10数据集中学习到的特征。
6、 代码
源代码下载sparseAutoencoderLinearCost.m文件
function [cost,grad] = sparseAutoencoderLinearCost(theta, visibleSize, hiddenSize, ... lambda, sparsityParam, beta, data) % visibleSize: the number of input units (probably 64) % hiddenSize: the number of hidden units (probably 25) % lambda: weight decay parameter % sparsityParam: The desired average activation for the hidden units (denoted in the lecture % notes by the greek alphabet rho, which looks like a lower-case "p"). % beta: weight of sparsity penalty term % data: Our 64x10000 matrix containing the training data. So, data(:,i) is the i-th training example. % The input theta is a vector (because minFunc expects the parameters to be a vector). % We first convert theta to the (W1, W2, b1, b2) matrix/vector format, so that this % follows the notation convention of the lecture notes. W1 = reshape(theta(1:hiddenSize*visibleSize), hiddenSize, visibleSize); W2 = reshape(theta(hiddenSize*visibleSize+1:2*hiddenSize*visibleSize), visibleSize, hiddenSize); b1 = theta(2*hiddenSize*visibleSize+1:2*hiddenSize*visibleSize+hiddenSize); b2 = theta(2*hiddenSize*visibleSize+hiddenSize+1:end); % Cost and gradient variables (your code needs to compute these values). % Here, we initialize them to zeros. cost = 0; W1grad = zeros(size(W1)); W2grad = zeros(size(W2)); b1grad = zeros(size(b1)); b2grad = zeros(size(b2)); %% ---------- YOUR CODE HERE -------------------------------------- % Instructions: Compute the cost/optimization objective J_sparse(W,b) for the Sparse Autoencoder, % and the corresponding gradients W1grad, W2grad, b1grad, b2grad. % % W1grad, W2grad, b1grad and b2grad should be computed using backpropagation. % Note that W1grad has the same dimensions as W1, b1grad has the same dimensions % as b1, etc. Your code should set W1grad to be the partial derivative of J_sparse(W,b) with % respect to W1. I.e., W1grad(i,j) should be the partial derivative of J_sparse(W,b) % with respect to the input parameter W1(i,j). Thus, W1grad should be equal to the term % [(1/m) \Delta W^{(1)} + \lambda W^{(1)}] in the last block of pseudo-code in Section 2.2 % of the lecture notes (and similarly for W2grad, b1grad, b2grad). % % Stated differently, if we were using batch gradient descent to optimize the parameters, % the gradient descent update to W1 would be W1 := W1 - alpha * W1grad, and similarly for W2, b1, b2. % % data = [ones(1,size(data,2)); data]; [n m] = size(data); z2 = W1* data + repmat(b1,1,m); a2 = sigmoid(z2); z3 = W2 * a2 + repmat(b2,1,m); a3 = z3; %J = trace((data - a3)' * (data - a3)) / (m*2); J = sum(sum((a3-data).^2)) / (m*2); regu = (W1(:)'*W1(:) + W2(:)'*W2(:))/2; phat = mean(a2,2); p = repmat(sparsityParam, size(phat)); sparse = p .* log(p ./ phat) + (1-p) .* log((1-p) ./ (1-phat)); cost = J + lambda*regu + beta * sum(sparse); delta3 = -1* (data-a3); delta2 = (W2'*delta3+beta*repmat(-p./phat+(1-p)./(1-phat),1,size(data,2))).*a2.*(1-a2); W2grad = delta3*a2'/m; b2grad = mean(delta3,2); W1grad = delta2*data'/m; b1grad = mean(delta2,2); W2grad = W2grad + lambda*W2; W1grad = W1grad + lambda*W1; %------------------------------------------------------------------- % After computing the cost and gradient, we will convert the gradients back % to a vector format (suitable for minFunc). Specifically, we will unroll % your gradient matrices into a vector. grad = [W1grad(:) ; W2grad(:) ; b1grad(:) ; b2grad(:)]; end %------------------------------------------------------------------- % Here's an implementation of the sigmoid function, which you may find useful % in your computation of the costs and the gradients. This inputs a (row or % column) vector (say (z1, z2, z3)) and returns (f(z1), f(z2), f(z3)). function sigm = sigmoid(x) sigm = 1 ./ (1 + exp(-x)); end