2 #include <shark/Models/FFNet.h> 3 #include <shark/Models/Autoencoder.h> 16 using namespace shark;
20 std::vector<RealVector> data(320,RealVector(16));
21 std::vector<unsigned int> label(320);
23 for(std::size_t k = 0; k != 10; ++k){
24 for(
size_t x=0; x != 16; x++) {
25 for(
size_t j=0; j != 4; j++) {
26 bool val = (x & (1<<j)) > 0;
32 for(
int i=0; i != 4; i++) {
33 subrange(data[x+k*16],i*4 ,i*4 + 4) = line;
35 for(
int i=0; i != 4; i++) {
36 for(
int l=0; l<4; l++) {
37 data[x+k*16+160](l*4 + i) = line(l);
41 label[x+k*16+160] = 0;
48 template<
class AutoencoderModel>
51 std::size_t numHidden,
52 double regularisation,
53 std::size_t iterations
58 model.setStructure(inputs, numHidden);
69 optimizer.
init(error);
70 std::cout<<
"Optimizing model: "+model.name()<<std::endl;
71 for(std::size_t i = 0; i != iterations; ++i){
72 optimizer.
step(error);
80 typedef FFNet<RectifierNeuron,LinearNeuron>
Network;
85 std::size_t numHidden1,std::size_t numHidden2, std::size_t numOutputs,
86 double regularisation, std::size_t iterations
89 std::cout<<
"training first layer"<<std::endl;
99 std::cout<<
"training second layer"<<std::endl;
101 intermediateData,numHidden2,
107 network.setStructure(
dataDimension(data),numHidden1,numHidden2, numOutputs);
109 network.setLayer(0,layer.encoderMatrix(),layer.hiddenBias());
110 network.setLayer(1,layer2.encoderMatrix(),layer2.hiddenBias());
118 std::size_t numHidden1 = 8;
119 std::size_t numHidden2 = 8;
121 double unsupRegularisation = 0.001;
122 std::size_t unsupIterations = 100;
124 double regularisation = 0.0001;
125 std::size_t iterations = 200;
135 unsupRegularisation, unsupIterations
145 std::cout<<
"training supervised model"<<std::endl;
148 optimizer.
init(error);
149 for(std::size_t i = 0; i != iterations; ++i){
150 optimizer.
step(error);
158 cout <<
"classification error,train: " << loss01.
eval(data.
labels(), predictionTrain) << endl;
161 cout <<
"classification error,test: " << loss01.
eval(test.
labels(), prediction) << endl;