4 #include <shark/Models/Autoencoder.h> 5 #include <shark/Models/TiedAutoencoder.h> 13 using namespace shark;
19 class NoiseModel :
public AbstractModel<RealVector,RealVector>
25 NoiseModel(
double prob)
29 std::string name()
const 30 {
return "NoiseModel"; }
34 RealVector parameterVector()
const{
37 void setParameterVector(RealVector
const& newParameters){}
38 size_t numberOfParameters()
const{
43 void eval(BatchInputType
const& inputs, BatchOutputType& outputs,
State& state)
const{
49 for(std::size_t i = 0; i != outputs.size1(); ++i){
50 for(std::size_t j = 0; j != outputs.size2(); ++j){
61 template<
class AutoencoderModel>
64 std::size_t numHidden,
65 std::size_t iterations,
66 double regularisation,
72 baseModel.setStructure(inputs, numHidden);
73 initRandomUniform(baseModel,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs));
74 NoiseModel
noise(noiseStrength);
88 optimizer.
init(error);
89 std::cout<<
"Optimizing model: "+model.
name()<<std::endl;
90 for(std::size_t i = 0; i != iterations; ++i){
91 optimizer.
step(error);
97 int main(
int argc,
char **argv)
100 cerr <<
"usage: " << argv[0] <<
" path/to/mnist_subset.libsvm" << endl;
103 std::size_t numHidden = 200;
104 std::size_t iterations = 200;
105 double regularisation = 0.01;
106 double noiseStrengt = 0.5;
112 for(std::size_t i = 0; i != numElems; ++i){
113 for(std::size_t j = 0; j != 784; ++j){
114 if(train.
element(i).input(j) > 0.5){
122 typedef Autoencoder<LogisticNeuron, LogisticNeuron> Autoencoder1;
123 typedef TiedAutoencoder<LogisticNeuron, LogisticNeuron> Autoencoder2;
125 Autoencoder1 net1 = trainAutoencoderModel<Autoencoder1>(train.
inputs(),numHidden,iterations,regularisation,noiseStrengt);
126 Autoencoder2 net2 = trainAutoencoderModel<Autoencoder2>(train.
inputs(),numHidden,iterations,regularisation,noiseStrengt);