esn.hpp

Go to the documentation of this file.
00001 /***************************************************************************/
00020 namespace aureservoir
00021 {
00022 
00023 template <typename T>
00024 ESN<T>::ESN()
00025 {
00026   set_denormal_flags();
00027   Rand<T>::initSeed();
00028 
00029   init_=0;
00030   train_=0;
00031   sim_=0;
00032 
00033   // set some standard parameters
00034 
00035   setSize(10);
00036   setInputs(1);
00037   setOutputs(1);
00038   noise_=0;
00039 
00040   setInitParam(CONNECTIVITY, 0.8);
00041   setInitParam(ALPHA, 0.8);
00042   setInitParam(IN_CONNECTIVITY, 0.8);
00043   setInitParam(IN_SCALE, 1.);
00044   setInitParam(IN_SHIFT, 0.);
00045   setInitParam(FB_CONNECTIVITY, 0.);
00046   setInitParam(FB_SCALE, 1.);
00047   setInitParam(FB_SHIFT, 0.);
00048 
00049   setReservoirAct(ACT_TANH);
00050   setOutputAct(ACT_LINEAR);
00051 
00052   setInitAlgorithm(INIT_STD);
00053   setTrainAlgorithm(TRAIN_PI);
00054   setSimAlgorithm(SIM_STD);
00055 }
00056 
00057 template <typename T>
00058 ESN<T>::ESN(const ESN<T> &src)
00059 {
00060   init_=0;
00061   train_=0;
00062   sim_=0;
00063   noise_=0;
00064 
00065   neurons_ = src.neurons_;
00066   inputs_ = src.inputs_;
00067   outputs_ = src.outputs_;
00068   noise_ = src.noise_;
00069 
00071   init_params_ = src.init_params_;
00072 
00073   InitAlgorithm init = src.getInitAlgorithm();
00074   setInitAlgorithm( init );
00075   TrainAlgorithm train = src.getTrainAlgorithm();
00076   setTrainAlgorithm(train);
00077 
00078   // copy simulation alg and its data
00079   net_info_[SIMULATE_ALG] = src.getSimAlgorithm();
00080   sim_ = src.sim_->clone(this);
00081 
00082   Win_ = src.Win_;
00083   W_ = src.W_;
00084   Wback_ = src.Wback_;
00085   Wout_ = src.Wout_;
00086   x_ = src.x_;
00087 
00088   ActivationFunction tmp = src.getReservoirAct();
00089   setReservoirAct(tmp);
00090   tmp = src.getOutputAct();
00091   setOutputAct(tmp);
00092 }
00093 
00094 template <typename T>
00095 const ESN<T> &ESN<T>::operator= (const ESN<T> &src)
00096 {
00097   ESN<T>::ESN(src);
00098   return *this;
00099 }
00100 
00101 template <typename T>
00102 ESN<T>::~ESN()
00103 {
00104   if(init_) delete init_;
00105   if(train_) delete train_;
00106   if(sim_) delete sim_;
00107 }
00108 
00109 template <typename T>
00110 double ESN<T>::adapt(const DEMatrix &in)
00111   throw(AUExcept)
00112 {
00113   // this allocates the data, so we really need ACT_TANH2 activation function
00114   if( net_info_[RESERVOIR_ACT] != ACT_TANH2 )
00115     throw AUExcept("adapt: You need to set ACT_TANH2 activation function!");
00116 
00117   // check if we have all parameters
00118   if( init_params_.find(IP_LEARNRATE) == init_params_.end() )
00119     throw AUExcept("adapt: You need IP_LEARNRATE init parameter for adaptation");
00120   if( init_params_.find(IP_VAR) == init_params_.end() )
00121     throw AUExcept("adapt: You need IP_VAR init parameter for adaptation");
00122   if( init_params_.find(IP_MEAN) == init_params_.end() )
00123     init_params_[IP_MEAN] = 0.;
00124 
00125   // difference vectors to see learn progress
00126   flens::DenseVector<flens::Array<double> > a_diff = tanh2_a_,
00127                                             b_diff = tanh2_b_;
00128 
00129   // adaptation algorithm
00130 
00131   DEVector y(neurons_);
00132   DEMatrix sim_in(inputs_,1), sim_out(outputs_,1);
00133   int steps = in.numCols();
00134   T lr = init_params_[IP_LEARNRATE];
00135   T mean = init_params_[IP_MEAN];
00136   T var = init_params_[IP_VAR];
00137   T db;
00138 
00139   // set linear activation function to get values before nonlinearity
00140   setReservoirAct(ACT_LINEAR);
00141 
00142   for(int n=1; n<=steps; ++n)
00143   {
00144     // simulate one step
00145     sim_in(_,1) = in(_,n);
00146     simulate(sim_in, sim_out);
00147     // now we have our network state x_ without activation function
00148 
00149     // calc activation
00150     y = x_;
00151     act_tanh2( y.data(), y.length() );
00152 
00153     for(int m=1; m<=neurons_; ++m)
00154     {
00155       db = -lr * ( -mean/var + (y(m)/var)*(2*var+1-pow(y(m),2)+y(m)*mean) );
00156       tanh2_b_(m) += db;
00157       tanh2_a_(m) += lr/tanh2_a_(m) + db*x_(m);
00158     }
00159   }
00160 
00161   // re-set original reservoir activation
00162   setReservoirAct(ACT_TANH2);
00163 
00164   // calc difference / learn progress
00165   double progress=0;
00166   a_diff -= tanh2_a_;
00167   b_diff -= tanh2_b_;
00168   for(int i=1; i<=neurons_; ++i)
00169     progress = a_diff(i) + b_diff(i);
00170   progress /= 2*neurons_;
00171 
00172   return progress;
00173 }
00174 
00175 template <typename T>
00176 double ESN<T>::adapt(T *inmtx, int inrows, int incols)
00177   throw(AUExcept)
00178 {
00179   if( inrows != inputs_ )
00180     throw AUExcept("adapt: wrong input row size!");
00181 
00182   DEMatrix flin(inrows,incols);
00183 
00184   // copy data to FLENS matrix (column major storage)
00185   for(int i=0; i<inrows; ++i) {
00186   for(int j=0; j<incols; ++j) {
00187     flin(i+1,j+1) = inmtx[i*incols+j];
00188   } }
00189 
00190   return adapt(flin);
00191 }
00192 
00193 template <typename T>
00194 inline void ESN<T>::train(T *inmtx, int inrows, int incols,
00195                           T *outmtx, int outrows, int outcols, int washout)
00196   throw(AUExcept)
00197 {
00198   DEMatrix flin(inrows,incols);
00199   DEMatrix flout(outrows,outcols);
00200 
00201   // copy data to FLENS matrix (column major storage)
00202   for(int i=0; i<inrows; ++i) {
00203   for(int j=0; j<incols; ++j) {
00204     flin(i+1,j+1) = inmtx[i*incols+j];
00205   } }
00206   for(int i=0; i<outrows; ++i) {
00207   for(int j=0; j<outcols; ++j) {
00208     flout(i+1,j+1) = outmtx[i*outcols+j];
00209   } }
00210 
00211 //   std::cout << "Flens IN: " << flin << std::endl;
00212 //   std::cout << "Flens OUT: " << flout << std::endl;
00213 
00214   train(flin, flout, washout);
00215 }
00216 
00217 template <typename T>
00218 inline void ESN<T>::simulate(T *inmtx, int inrows, int incols,
00219                              T *outmtx, int outrows, int outcols)
00220   throw(AUExcept)
00221 {
00222   if( outcols != incols )
00223     throw AUExcept("ESN::simulate: output and input must have same nr of columns!");
00224   if( inrows != inputs_ )
00225     throw AUExcept("ESN::simulate: wrong input row size!");
00226   if( outrows != outputs_ )
00227     throw AUExcept("ESN::simulate: wrong output row size!");
00228 
00229   // check if we already have allocated data in simulation algorithm
00230   if( sim_->last_out_.numRows() != outputs_ )
00231     throw AUExcept("ESN::simulate: You need to allocate data for simulation algortihm - e.g. set an Wout matrix or train ESN !");
00232 
00233   DEMatrix flin(inrows,incols);
00234   DEMatrix flout(outrows,outcols);
00235 
00236   // copy data to FLENS matrix
00237   for(int i=0; i<inrows; ++i) {
00238   for(int j=0; j<incols; ++j) {
00239     flin(i+1,j+1) = inmtx[i*incols+j];
00240   } }
00241 
00242   simulate(flin, flout);
00243 
00244   // copy data to output
00245   for(int i=0; i<outrows; ++i) {
00246   for(int j=0; j<outcols; ++j) {
00247     outmtx[i*outcols+j] = flout(i+1,j+1);
00248   } }
00249 }
00250 
00251 template <typename T>
00252 inline void ESN<T>::simulateStep(T *invec, int insize, T *outvec, int outsize)
00253     throw(AUExcept)
00254 {
00255   if( insize != inputs_ )
00256     throw AUExcept("ESN::simulate: wrong input row size!");
00257   if( outsize != outputs_ )
00258     throw AUExcept("ESN::simulate: wrong output row size!");
00259 
00260   // check if we already have allocated data in simulation algorithm
00261   if( sim_->last_out_.numRows() != outputs_ )
00262     throw AUExcept("ESN::simulate: You need to allocate data for simulation algortihm - e.g. set an Wout matrix or train ESN !");
00263 
00264   DEMatrix flin(insize,1);
00265   DEMatrix flout(outsize,1);
00266 
00267   // copy data to FLENS matrix
00268   for(int i=0; i<insize; ++i)
00269     flin(i+1,1) = invec[i];
00270 
00271   simulate(flin, flout);
00272 
00273   // copy data to output
00274   for(int i=0; i<outsize; ++i)
00275     outvec[i] = flout(i+1,1);
00276 }
00277 
00278 template <typename T>
00279 void ESN<T>::setBPCutoff(const DEVector &f1, const DEVector &f2)
00280   throw(AUExcept)
00281 {
00282   if( net_info_[SIMULATE_ALG] != SIM_BP )
00283     throw AUExcept("ESN::setBPCutoff: you need to set SIM_BP and init the matrix first!");
00284 
00285   sim_->setBPCutoff(f1,f2);
00286 }
00287 
00288 template <typename T>
00289 void ESN<T>::setBPCutoff(T *f1vec, int f1size, T *f2vec, int f2size)
00290     throw(AUExcept)
00291 {
00292   if( f1size != neurons_ || f2size != neurons_ )
00293     throw AUExcept("ESN::setBPCutoff: vectors f1, f2 must be same size as neurons in the reservoir !");
00294 
00295   DEVector f1(neurons_);
00296   DEVector f2(neurons_);
00297 
00298   // copy data to FLENS vectors
00299   for(int i=0; i<neurons_; ++i)
00300   {
00301     f1(i+1) = f1vec[i];
00302     f2(i+1) = f2vec[i];
00303   }
00304 
00305   setBPCutoff(f1,f2);
00306 }
00307 
00308 template <typename T>
00309 void ESN<T>::setIIRCoeff(const DEMatrix &B, const DEMatrix &A, int series)
00310   throw(AUExcept)
00311 {
00312   if( net_info_[SIMULATE_ALG] != SIM_FILTER  &&
00313       net_info_[SIMULATE_ALG] != SIM_FILTER2 &&
00314       net_info_[SIMULATE_ALG] != SIM_FILTER_DS &&
00315       net_info_[SIMULATE_ALG] != SIM_SQUARE )
00316     throw AUExcept("ESN::setIIRCoeff: you need to set SIM_FILTER, SIM_FILTER2, SIM_FILTER_DS or SIM_SQUARE and init the matrix first!");
00317 
00318   sim_->setIIRCoeff(B,A,series);
00319 }
00320 
00321 template <typename T>
00322 void ESN<T>::setIIRCoeff(T *bmtx, int brows, int bcols,
00323                          T *amtx, int arows, int acols,
00324                          int series)
00325   throw(AUExcept)
00326 {
00327   if( brows != neurons_ || arows != neurons_ )
00328     throw AUExcept("ESN::setIIRCoeff: A and B must have as many rows as neurons in the ESN !");
00329 
00330   DEMatrix B(brows,bcols);
00331   DEMatrix A(arows,acols);
00332 
00333   // copy data to FLENS matrix (column major storage)
00334   for(int i=0; i<arows; ++i)
00335   {
00336     for(int j=0; j<acols; ++j)
00337       A(i+1,j+1) = amtx[i*acols+j];
00338 
00339     for(int j=0; j<bcols; ++j)
00340       B(i+1,j+1) = bmtx[i*bcols+j];
00341   }
00342 
00343   sim_->setIIRCoeff(B,A,series);
00344 }
00345 
00346 template <typename T>
00347 void ESN<T>::post()
00348 {
00349   std::cout << "--------------------------------------------\n"
00350             << "ESN Parameters:\n"
00351             << "\n"
00352             << "nr of neurons:\t" << neurons_ << "\n"
00353             << "reservoir connectivity:\t"
00354             << init_params_[CONNECTIVITY] << "\n"
00355             << "spectral radius:\t" << init_params_[ALPHA] << "\n";
00356   if( net_info_[SIMULATE_ALG] == SIM_LI )
00357   {
00358       std::cout << "leaking rate:\t" << init_params_[LEAKING_RATE] << "\n";
00359   }
00360   std::cout << "\n"
00361             << "inputs:\t" << inputs_ << "\n"
00362             << "input connectivity:\t"
00363             << init_params_[IN_CONNECTIVITY] << "\n"
00364             << "input scale:\t" << init_params_[IN_SCALE] << "\n"
00365             << "input shift:\t" << init_params_[IN_SHIFT] << "\n"
00366             << "\n"
00367             << "outputs:\t" << outputs_ << "\n"
00368             << "feedback connectivity:\t"
00369             << init_params_[FB_CONNECTIVITY] << "\n"
00370             << "feedback scale:\t" << init_params_[FB_SCALE] << "\n"
00371             << "feedback shift:\t" << init_params_[FB_SHIFT] << "\n"
00372             << "\n"
00373             << "noise level for simulation/training:\t" << noise_ << "\n"
00374             << "\n"
00375             << "reservoir activation fct:\t"
00376             << getActString( net_info_[RESERVOIR_ACT] ) << "\n"
00377             << "output activation fct:\t"
00378             << getActString( net_info_[OUTPUT_ACT] ) << "\n"
00379             << "\n"
00380             << "initialization algorithm:\t"
00381             << getInitString( net_info_[INIT_ALG] ) << "\n"
00382             << "training algorithm:\t"
00383             << getTrainString( net_info_[TRAIN_ALG] ) << "\n"
00384             << "simulation algorithm:\t"
00385             << getSimString( net_info_[SIMULATE_ALG] ) << "\n"
00386             << "--------------------------------------------\n";
00387 }
00388 
00389 template <typename T>
00390 void ESN<T>::getWin(T **mtx, int *rows, int *cols)
00391 {
00392   *mtx = Win_.data();
00393   *rows = Win_.numRows();
00394   *cols = Win_.numCols();
00395 }
00396 
00397 template <typename T>
00398 void ESN<T>::getWback(T **mtx, int *rows, int *cols)
00399 {
00400   *mtx = Wback_.data();
00401   *rows = Wback_.numRows();
00402   *cols = Wback_.numCols();
00403 }
00404 
00405 template <typename T>
00406 void ESN<T>::getWout(T **mtx, int *rows, int *cols)
00407 {
00408 //   std::cout << "Wout in C++: " << Wout_ << std::endl;
00409 
00410   *mtx = Wout_.data();
00411   *rows = Wout_.numRows();
00412   *cols = Wout_.numCols();
00413 }
00414 
00415 template <typename T>
00416 void ESN<T>::getX(T **vec, int *length)
00417 {
00418   *vec = x_.data();
00419   *length = x_.length();
00420 }
00421 
00422 template <typename T>
00423 void ESN<T>::getW(T *wmtx, int wrows, int wcols)
00424   throw(AUExcept)
00425 {
00426   if( wrows != W_.numRows() )
00427     throw AUExcept("ESN::getW: wrong row size!");
00428   if( wcols != W_.numCols() )
00429     throw AUExcept("ESN::getW: wrong column size!");
00430 
00431   // convert to dense matrix for element access
00433   DEMatrix Wtmp;
00434   Wtmp = W_;
00435 
00436   for(int i=0; i<wrows; ++i) {
00437   for(int j=0; j<wcols; ++j) {
00438     wmtx[i*wcols+j] = Wtmp(i+1,j+1);
00439   } }
00440 }
00441 
00442 template <typename T>
00443 void ESN<T>::getDelays(T *wmtx, int wrows, int wcols)
00444   throw(AUExcept)
00445 {
00446   DEMatrix Dtmp = getDelays();
00447 
00448   if( wrows != Dtmp.numRows() )
00449     throw AUExcept("ESN::getDelays: wrong row size!");
00450   if( wcols != Dtmp.numCols() )
00451     throw AUExcept("ESN::getDelays: wrong column size!");
00452 
00453   for(int i=0; i<wrows; ++i) {
00454   for(int j=0; j<wcols; ++j) {
00455     wmtx[i*wcols+j] = Dtmp(i+1,j+1);
00456   } }
00457 }
00458 
00459 template <typename T>
00460 void ESN<T>::setInitAlgorithm(InitAlgorithm alg)
00461   throw(AUExcept)
00462 {
00463   switch(alg)
00464   {
00465     case INIT_STD:
00466       if(init_) delete init_;
00467       init_ = new InitStd<T>(this);
00468       net_info_[INIT_ALG] = INIT_STD;
00469       break;
00470 
00471     default:
00472       throw AUExcept("ESN::setInitAlgorithm: no valid Algorithm!");
00473   }
00474 }
00475 
00476 template <typename T>
00477 void ESN<T>::setTrainAlgorithm(TrainAlgorithm alg)
00478   throw(AUExcept)
00479 {
00480   switch(alg)
00481   {
00482     case TRAIN_PI:
00483       if(train_) delete train_;
00484       train_ = new TrainPI<T>(this);
00485       net_info_[TRAIN_ALG] = TRAIN_PI;
00486       break;
00487 
00488     case TRAIN_LS:
00489       if(train_) delete train_;
00490       train_ = new TrainLS<T>(this);
00491       net_info_[TRAIN_ALG] = TRAIN_LS;
00492       break;
00493 
00494     case TRAIN_RIDGEREG:
00495       if(train_) delete train_;
00496       train_ = new TrainRidgeReg<T>(this);
00497       net_info_[TRAIN_ALG] = TRAIN_RIDGEREG;
00498       break;
00499 
00500     case TRAIN_DS_PI:
00501       if(train_) delete train_;
00502       train_ = new TrainDSPI<T>(this);
00503       net_info_[TRAIN_ALG] = TRAIN_DS_PI;
00504       break;
00505 
00506     default:
00507       throw AUExcept("ESN::setTrainAlgorithm: no valid Algorithm!");
00508   }
00509 }
00510 
00511 template <typename T>
00512 void ESN<T>::setSimAlgorithm(SimAlgorithm alg)
00513   throw(AUExcept)
00514 {
00515   switch(alg)
00516   {
00517     case SIM_STD:
00518       if(sim_) delete sim_;
00519       sim_ = new SimStd<T>(this);
00520       net_info_[SIMULATE_ALG] = SIM_STD;
00521       break;
00522 
00523     case SIM_SQUARE:
00524       if(sim_) delete sim_;
00525       sim_ = new SimSquare<T>(this);
00526       net_info_[SIMULATE_ALG] = SIM_SQUARE;
00527       break;
00528 
00529     case SIM_LI:
00530       if(sim_) delete sim_;
00531       sim_ = new SimLI<T>(this);
00532       net_info_[SIMULATE_ALG] = SIM_LI;
00533       break;
00534 
00535     case SIM_BP:
00536       if(sim_) delete sim_;
00537       sim_ = new SimBP<T>(this);
00538       net_info_[SIMULATE_ALG] = SIM_BP;
00539       break;
00540 
00541     case SIM_FILTER:
00542       if(sim_) delete sim_;
00543       sim_ = new SimFilter<T>(this);
00544       net_info_[SIMULATE_ALG] = SIM_FILTER;
00545       break;
00546 
00547     case SIM_FILTER2:
00548       if(sim_) delete sim_;
00549       sim_ = new SimFilter2<T>(this);
00550       net_info_[SIMULATE_ALG] = SIM_FILTER2;
00551       break;
00552 
00553     case SIM_FILTER_DS:
00554       if(sim_) delete sim_;
00555       sim_ = new SimFilterDS<T>(this);
00556       net_info_[SIMULATE_ALG] = SIM_FILTER_DS;
00557       break;
00558 
00559     default:
00560       throw AUExcept("ESN::setSimAlgorithm: no valid Algorithm!");
00561   }
00562 }
00563 
00564 template <typename T>
00565 void ESN<T>::setSize(int neurons)
00566   throw(AUExcept)
00567 {
00568   if(neurons<1)
00569     throw AUExcept("ESN::setReservoirSize: there must be at least one neuron!");
00570 
00571   neurons_=neurons;
00572 }
00573 
00574 template <typename T>
00575 void ESN<T>::setInputs(int inputs)
00576   throw(AUExcept)
00577 {
00578   if(inputs<1)
00579     throw AUExcept("ESN::setInputs: there must be at least one input!");
00580 
00581   inputs_=inputs;
00582 }
00583 
00584 template <typename T>
00585 void ESN<T>::setOutputs(int outputs)
00586   throw(AUExcept)
00587 {
00588   if(outputs<1)
00589     throw AUExcept("ESN::setOutputs: there must be at least one output!");
00590 
00591   outputs_=outputs;
00592 }
00593 
00594 template <typename T>
00595 void ESN<T>::setNoise(double noise)
00596   throw(AUExcept)
00597 {
00598   if(noise<0)
00599     throw AUExcept("ESN::setNoise: Noise level must be a positive number!");
00600 
00601   noise_=noise;
00602 }
00603 
00604 template <typename T>
00605 void ESN<T>::setInitParam(InitParameter key, T value)
00606 {
00607   init_params_[key] = value;
00608 }
00609 
00610 template <typename T>
00611 void ESN<T>::setReservoirAct(ActivationFunction f)
00612   throw(AUExcept)
00613 {
00614   switch(f)
00615   {
00616     case ACT_LINEAR:
00617       reservoirAct_= act_linear;
00618       net_info_[RESERVOIR_ACT] = ACT_LINEAR;
00619       break;
00620 
00621     case ACT_TANH:
00622       reservoirAct_= act_tanh;
00623       net_info_[RESERVOIR_ACT] = ACT_TANH;
00624       break;
00625 
00626     case ACT_TANH2:
00627       reservoirAct_= act_tanh2;
00628       net_info_[RESERVOIR_ACT] = ACT_TANH2;
00629       break;
00630 
00631     case ACT_SIGMOID:
00632       reservoirAct_= act_sigmoid;
00633       net_info_[RESERVOIR_ACT] = ACT_SIGMOID;
00634       break;
00635 
00636     default:
00637       throw AUExcept("ESN::setReservoirAct: wrong reservoir activation function!");
00638   }
00639 }
00640 
00641 template <typename T>
00642 void ESN<T>::setOutputAct(ActivationFunction f)
00643   throw(AUExcept)
00644 {
00645   switch(f)
00646   {
00647     case ACT_LINEAR:
00648       outputAct_    = act_linear;
00649       outputInvAct_ = act_invlinear;
00650       net_info_[OUTPUT_ACT] = ACT_LINEAR;
00651       break;
00652 
00653     case ACT_TANH:
00654       outputAct_    = act_tanh;
00655       outputInvAct_ = act_invtanh;
00656       net_info_[OUTPUT_ACT] = ACT_TANH;
00657       break;
00658 
00659     case ACT_SIGMOID:
00660       outputAct_    = act_sigmoid;
00661       outputInvAct_ = act_invsigmoid;
00662       net_info_[OUTPUT_ACT] = ACT_SIGMOID;
00663       break;
00664 
00665     default:
00666       throw AUExcept("ESN::setOutputAct: wrong output activation function!");
00667   }
00668 }
00669 
00670 template <typename T>
00671 void ESN<T>::setWin(const DEMatrix &Win) throw(AUExcept)
00672 {
00673   if( Win.numRows() != neurons_ )
00674       throw AUExcept("ESN::setWin: wrong row size!");
00675   if( Win.numCols() != inputs_ )
00676       throw AUExcept("ESN::setWin: wrong column size!");
00677 
00678   Win_ = Win;
00679 }
00680 
00681 template <typename T>
00682 void ESN<T>::setW(const DEMatrix &W) throw(AUExcept)
00683 {
00684   if( W.numRows() != neurons_ )
00685       throw AUExcept("ESN::setW: wrong row size!");
00686   if( W.numCols() != neurons_ )
00687       throw AUExcept("ESN::setW: wrong column size!");
00688 
00689 //   W_.initWith(W, 1E-9);
00690   W_ = W;
00691 }
00692 
00693 template <typename T>
00694 void ESN<T>::setWback(const DEMatrix &Wback) throw(AUExcept)
00695 {
00696   if( Wback.numRows() != neurons_ )
00697       throw AUExcept("ESN::setWback: wrong row size!");
00698   if( Wback.numCols() != outputs_ )
00699       throw AUExcept("ESN::setWback: wrong column size!");
00700 
00701   Wback_ = Wback;
00702 }
00703 
00704 template <typename T>
00705 void ESN<T>::setWout(const DEMatrix &Wout) throw(AUExcept)
00706 {
00707   if( Wout.numRows() != outputs_ )
00708       throw AUExcept("ESN::setWout: Wout must have output_ rows!");
00709   if( Wout.numCols() != inputs_+neurons_ && 
00710       Wout.numCols() != 2*(inputs_+neurons_) )
00711       throw AUExcept("ESN::setWout: wrong column size!");
00712 
00713   Wout_ = Wout;
00714 }
00715 
00716 template <typename T>
00717 void ESN<T>::setX(const DEVector &x) throw(AUExcept)
00718 {
00719   if( x.length() != neurons_ )
00720       throw AUExcept("ESN::setX: wrong size!");
00721 
00722   x_ = x;
00723 }
00724 
00725 template <typename T>
00726 void ESN<T>::setLastOutput(const DEVector &last)
00727   throw(AUExcept)
00728 {
00729   if( last.length() != outputs_ )
00730       throw AUExcept("setLastOutput: wrong size!");
00731 
00732   sim_->last_out_.resize(last.length(),1);
00733   sim_->last_out_(_,1) = last;
00734 }
00735 
00736 template <typename T>
00737 void ESN<T>::setWin(T *inmtx, int inrows, int incols) throw(AUExcept)
00738 {
00739   if( inrows != neurons_ )
00740       throw AUExcept("ESN::setWin: wrong row size!");
00741   if( incols != inputs_ )
00742       throw AUExcept("ESN::setWin: wrong column size!");
00743 
00744   Win_.resize(inrows,incols);
00745 
00746   for(int i=0; i<inrows; ++i) {
00747   for(int j=0; j<incols; ++j) {
00748     Win_(i+1,j+1) = inmtx[i*incols+j];
00749   } }
00750 }
00751 
00752 template <typename T>
00753 void ESN<T>::setW(T *inmtx, int inrows, int incols) throw(AUExcept)
00754 {
00755   if( inrows != neurons_ )
00756       throw AUExcept("ESN::setW: wrong row size!");
00757   if( incols != neurons_ )
00758       throw AUExcept("ESN::setW: wrong column size!");
00759 
00760   DEMatrix Wtmp(neurons_,neurons_);
00761 
00762   for(int i=0; i<inrows; ++i) {
00763   for(int j=0; j<incols; ++j) {
00764     Wtmp(i+1,j+1) = inmtx[i*incols+j];
00765   } }
00766 
00767 //   W_.initWith(Wtmp, 1E-9);
00768   W_ = Wtmp;
00769 }
00770 
00771 template <typename T>
00772 void ESN<T>::setWback(T *inmtx, int inrows, int incols) throw(AUExcept)
00773 {
00774   if( inrows != neurons_ )
00775       throw AUExcept("ESN::setWback: wrong row size!");
00776   if( incols != outputs_ )
00777       throw AUExcept("ESN::setWback: wrong column size!");
00778 
00779   Wback_.resize(inrows,incols);
00780 
00781   for(int i=0; i<inrows; ++i) {
00782   for(int j=0; j<incols; ++j) {
00783     Wback_(i+1,j+1) = inmtx[i*incols+j];
00784   } }
00785 }
00786 
00787 template <typename T>
00788 void ESN<T>::setWout(T *inmtx, int inrows, int incols) throw(AUExcept)
00789 {
00790   if( inrows != outputs_ )
00791       throw AUExcept("ESN::setWout: Wout must have output_ rows!");
00792   if( incols != inputs_+neurons_ && 
00793       incols != 2*(inputs_+neurons_) )
00794       throw AUExcept("ESN::setWout: wrong column size!");
00795 
00796   Wout_.resize(inrows,incols);
00797 
00798   for(int i=0; i<inrows; ++i) {
00799   for(int j=0; j<incols; ++j) {
00800     Wout_(i+1,j+1) = inmtx[i*incols+j];
00801   } }
00802 }
00803 
00804 template <typename T>
00805 void ESN<T>::setX(T *invec, int insize) throw(AUExcept)
00806 {
00807   if( insize != neurons_ )
00808       throw AUExcept("ESN::setX: wrong size!");
00809 
00810   x_.resize(insize);
00811   for(int i=0; i<insize; ++i)
00812     x_(i+1) = invec[i];
00813 }
00814 
00815 template <typename T>
00816 void ESN<T>::setLastOutput(T *last, int size)
00817   throw(AUExcept)
00818 {
00819   if( size != outputs_ )
00820       throw AUExcept("setLastOutput: wrong size!");
00821 
00822   sim_->last_out_.resize(size,1);
00823   for(int i=0; i<size; ++i)
00824     sim_->last_out_(i+1,1) = last[i];
00825 }
00826 
00827 template <typename T>
00828 string ESN<T>::getActString(int act)
00829 {
00830   switch(act)
00831   {
00832     case ACT_LINEAR:
00833       return "ACT_LINEAR";
00834 
00835     case ACT_TANH:
00836       return "ACT_TANH";
00837 
00838     case ACT_TANH2:
00839       return "ACT_TANH2";
00840 
00841 
00842     case ACT_SIGMOID:
00843       return "ACT_SIGMOID";
00844 
00845     default:
00846       throw AUExcept("ESN::getActString: unknown activation function");
00847   }
00848 }
00849 
00850 template <typename T>
00851 string ESN<T>::getInitString(int alg)
00852 {
00853   switch(alg)
00854   {
00855     case INIT_STD:
00856       return "INIT_STD";
00857 
00858     default:
00859       throw AUExcept("ESN::getInitString: unknown init algorithm");
00860   }
00861 }
00862 
00863 template <typename T>
00864 string ESN<T>::getSimString(int alg)
00865 {
00866   switch(alg)
00867   {
00868     case SIM_STD:
00869       return "SIM_STD";
00870 
00871     case SIM_SQUARE:
00872       return "SIM_SQUARE";
00873 
00874     case SIM_LI:
00875       return "SIM_LI";
00876 
00877     case SIM_BP:
00878       return "SIM_BP";
00879 
00880     case SIM_FILTER:
00881       return "SIM_FILTER";
00882 
00883     case SIM_FILTER2:
00884       return "SIM_FILTER2";
00885 
00886     case SIM_FILTER_DS:
00887       return "SIM_FILTER_DS";
00888 
00889     default:
00890       throw AUExcept("ESN::getSimString: unknown simulation algorithm");
00891   }
00892 }
00893 
00894 template <typename T>
00895 string ESN<T>::getTrainString(int alg)
00896 {
00897   switch(alg)
00898   {
00899     case TRAIN_PI:
00900       return "TRAIN_PI";
00901 
00902     case TRAIN_LS:
00903       return "TRAIN_LS";
00904 
00905     case TRAIN_RIDGEREG:
00906       return "TRAIN_RIDGEREG";
00907 
00908     case TRAIN_DS_PI:
00909       return "TRAIN_DS_PI";
00910 
00911     default:
00912       throw AUExcept("ESN::getTrainString: unknown training algorithm");
00913   }
00914 }
00915 
00916 
00917 // template <typename T>
00918 // void ESN<T>::setParameter(string param, string value)
00919 //   throw(AUExcept)
00920 // {
00921 //   // tests machen für diese funktion
00922 // 
00923 //   bool ok = false;
00924 // 
00925 //   // algorithms
00926 // 
00927 //   if( param == "simulation_alg" || param == "sim_alg" ||
00928 //       param == "simulation_algorithm" || param == "sim_algorithm"  )
00929 //   {
00930 //     if( value == "sim_std" || value == "std" )
00931 //     {
00932 //       setSimAlgorithm( SIM_STD );
00933 //       ok = true;
00934 //     }
00935 //   }
00936 //   if( param == "training_alg" || param == "train_alg" ||
00937 //       param == "training_algorithm" || param == "train_algorithm"  )
00938 //   {
00939 //     if( value == "train_leastsquare" || value == "leastsquare" )
00940 //     {
00941 //       setTrainAlgorithm( TRAIN_LS );
00942 //       ok = true;
00943 //     }
00944 //   }
00945 //   if( param == "initialization_alg" || param == "init_alg" ||
00946 //       param == "initialization_algorithm" || param == "init_algorithm"  )
00947 //   {
00948 //     if( value == "init_std" || value == "std" )
00949 //     {
00950 //       setInitAlgorithm( INIT_STD );
00951 //       ok = true;
00952 //     }
00953 //   }
00954 // 
00955 //   // general
00956 // 
00957 //   if( param == "size" || param == "reservoir_size" ||
00958 //       param == "reservoir size" || param == "reservoir" )
00959 //   {
00960 //     setSize( stringToInt(value) );
00961 //     ok = true;
00962 //   }
00963 //   if( param == "inputs" )
00964 //   {
00965 //     setInputs( stringToInt(value) );
00966 //     ok = true;
00967 //   }
00968 //   if( param == "outputs" )
00969 //   {
00970 //     setOutputs( stringToInt(value) );
00971 //     ok = true;
00972 //   }
00973 // 
00974 //   // input parameter map
00975 // 
00976 //   if( param == "connectivity" || param == "conn" )
00977 //   {
00978 //     setInitParam( CONNECTIVITY, stringToDouble(value) );
00979 //     ok = true;
00980 //   }
00981 //   if( param == "alpha" )
00982 //   {
00983 //     setInitParam( ALPHA, stringToDouble(value) );
00984 //     ok = true;
00985 //   }
00986 //   if( param == "in_connectivity" || param == "in_conn" )
00987 //   {
00988 //     setInitParam( IN_CONNECTIVITY, stringToDouble(value) );
00989 //     ok = true;
00990 //   }
00991 //   if( param == "in_scale" )
00992 //   {
00993 //     setInitParam( IN_SCALE, stringToDouble(value) );
00994 //     ok = true;
00995 //   }
00996 //   if( param == "in_shift" )
00997 //   {
00998 //     setInitParam( IN_SHIFT, stringToDouble(value) );
00999 //     ok = true;
01000 //   }
01001 //   if( param == "fb_connectivity" || param == "fb_conn" )
01002 //   {
01003 //     setInitParam( FB_CONNECTIVITY, stringToDouble(value) );
01004 //     ok = true;
01005 //   }
01006 //   if( param == "fb_scale" )
01007 //   {
01008 //     setInitParam( FB_SCALE, stringToDouble(value) );
01009 //     ok = true;
01010 //   }
01011 //   if( param == "fb_shift" )
01012 //   {
01013 //     setInitParam( FB_SHIFT, stringToDouble(value) );
01014 //     ok = true;
01015 //   }
01016 // 
01017 //   // activation functions
01018 // 
01019 //   if( param == "reservoir_act" || param == "res_act" )
01020 //   {
01021 //     if( value == "linear" || value == "lin" )
01022 //     {
01023 //       setReservoirAct( ACT_LINEAR );
01024 //       ok = true;
01025 //     }
01026 //     if( value == "tanh" )
01027 //     {
01028 //       setReservoirAct( ACT_TANH );
01029 //       ok = true;
01030 //     }
01031 //   }
01032 //   if( param == "output_act" || param == "out_act" )
01033 //   {
01034 //     if( value == "linear" || value == "lin" )
01035 //     {
01036 //       setOutputAct( ACT_LINEAR );
01037 //       ok = true;
01038 //     }
01039 //     if( value == "tanh" )
01040 //     {
01041 //       setOutputAct( ACT_TANH );
01042 //       ok = true;
01043 //     }
01044 //   }
01045 // 
01046 //   if( !ok )
01047 //     throw AUExcept("ESN::setParameter: parameter value not valid!");
01048 // }
01049 
01050 } // end of namespace aureservoir

Generated on Wed Mar 12 21:16:05 2008 for aureservoir by  doxygen 1.5.3