程序代码: [选择]function [cost grad] = SparseEncoder( netConfig, data, weight, predOnly )
%% this is a basic autoencoder only weight sparse penalize
if ~exist('predOnly', 'var')
predOnly = false;
end
[n, m] = size(data);
tic();
netConfig = stack2param(netConfig, weight);
toc();
w1 = netConfig.layers{1}.w;
b1 = netConfig.layers{1}.b;
w2 = netConfig.layers{2}.w;
b2 = netConfig.layers{2}.b;
% feedforward
z2 = bsxfun(@plus, w1*data, b1);
a2 = sigmoid(z2);
z3 = bsxfun(@plus, w2*a2, b2);
a3 = sigmoid(z3);
% compute kl distance
rouBar = sum(a2, 2)./m;
rou = netConfig.sparsity;
klPenatyItem = rou*log(rou./rouBar)+(1-rou)*log((1-rou)./(1-rouBar));
%cost
cost = 1/(2*m)*(sum