5 #include <shark/ObjectiveFunctions/SparseAutoencoderError.h> 12 using namespace shark;
17 const std::size_t
w = 512;
18 const std::size_t
h = 512;
23 const double rho = 0.01;
36 cout <<
"Found " << n <<
" images of size " <<
w <<
"x" <<
h << endl;
48 vector<RealVector> patches;
49 for (ElRef it = images.
elements().begin(); it != images.
elements().end(); ++it) {
50 for (
size_t i = 0; i < patchesPerImg; ++i) {
55 unsigned int ul = ulx *
h + uly;
57 const RealVector& img = *it;
58 for (
size_t j = 0; j <
psize; ++j)
59 for (
size_t k = 0; k <
psize; ++k)
60 sample(j * psize + k) = img(ul + k + j *
h);
61 patches.push_back(sample);
68 RealVector meanvec =
mean(samples);
69 samples =
transform(samples, Shift(-meanvec));
73 RealVector pstd = 3 * sqrt(
variance(samples));
74 samples =
transform(samples, TruncateAndRescale(-pstd, pstd, 0.1, 0.9));
82 double r = std::sqrt(6.0) / std::sqrt(model.numberOfHiddenNeurons() + model.inputSize() + 1.0);
83 RealVector params(model.numberOfParameters(),0);
85 std::size_t hiddenWeights = model.inputSize()+model.outputSize();
86 hiddenWeights *= model.numberOfHiddenNeurons();
87 for(std::size_t i = 0; i != hiddenWeights;++i){
90 model.setParameterVector(params);
101 cout <<
"Generated : " << samples.
numberOfElements() <<
" patches." << endl;
105 Autoencoder<LogisticNeuron, LogisticNeuron> model;
109 SparseAutoencoderError error(data,&model, &loss,
rho,
beta);
112 error.setRegularizer(
lambda,®ularizer);
114 cout <<
"Model has: " << model.numberOfParameters() <<
" params." << endl;
115 cout <<
"Model has: " << model.numberOfHiddenNeurons() <<
"hidden neurons." << endl;
116 cout <<
"Model has: " << model.inputSize() <<
" inputs." << endl;
117 cout <<
"Model has: " << model.outputSize() <<
" outputs." << endl;
122 optimizer.
init(error);
124 for (
unsigned int i = 0; i <
maxIter; ++i) {
125 optimizer.
step(error);
128 cout <<
"Elapsed time: " << timer.
stop() << endl;
129 cout <<
"Function evaluations: " << error.evaluationCounter() << endl;