init.hpp

Go to the documentation of this file.
00001 /***************************************************************************/
00020 #include <algorithm>
00021 
00022 namespace aureservoir
00023 {
00024 
00026 
00027 
00028 template <typename T>
00029 void InitBase<T>::checkInitParams()
00030   throw(AUExcept)
00031 {
00032   T tmp;
00033 
00034   tmp = esn_->init_params_[CONNECTIVITY];
00035   if( tmp<=0 || tmp>1 )
00036     throw AUExcept("InitBase::checkInitParams: CONNECTIVITY must be within [0|1]");
00037 
00038   tmp = esn_->init_params_[ALPHA];
00039   if( tmp<0 )
00040     throw AUExcept("InitBase::checkInitParams: ALPHA must be >= 0");
00041 
00042   tmp = esn_->init_params_[IN_CONNECTIVITY];
00043   if( tmp<0 || tmp>1 )
00044     throw AUExcept("InitBase::checkInitParams: IN_CONNECTIVITY must be within [0|1]");
00045 
00046   tmp = esn_->init_params_[FB_CONNECTIVITY];
00047   if( tmp<0 || tmp>1 )
00048     throw AUExcept("InitBase::checkInitParams: FB_CONNECTIVITY must be within [0|1]");
00049 
00050   if( esn_->net_info_[ESN<T>::SIMULATE_ALG] == SIM_LI )
00051   {
00052     if( esn_->init_params_.find(LEAKING_RATE) == esn_->init_params_.end() )
00053       throw AUExcept("InitBase::checkInitParams: No LEAKING_RATE given !");
00054 
00055     tmp = esn_->init_params_[LEAKING_RATE];
00056     if( tmp<0 )
00057       throw AUExcept("InitBase::checkInitParams: LEAKING_RATE must be >= 0 !");
00058   }
00059 
00060   if( esn_->net_info_[ESN<T>::TRAIN_ALG] == TRAIN_RIDGEREG )
00061   {
00062     if( esn_->init_params_.find(TIKHONOV_FACTOR) == esn_->init_params_.end() )
00063       throw AUExcept("InitBase::checkInitParams: No TIKHONOV_FACTOR given !");
00064 
00065     tmp = esn_->init_params_[TIKHONOV_FACTOR];
00066     if( tmp<0 )
00067       throw AUExcept("InitBase::checkInitParams: TIKHONOV_FACTOR must be >= 0 !");
00068   }
00069 }
00070 
00071 template <typename T>
00072 void InitBase<T>::allocateWorkData()
00073 {
00074   // to allocate sim class data, if we don't use training
00075   esn_->sim_->reallocate();
00076 
00077   // calc params for tanh2 activation function
00078   if( esn_->net_info_[ESN<T>::RESERVOIR_ACT] == ACT_TANH2 )
00079   {
00080     tanh2_a_.resize( esn_->neurons_ );
00081     tanh2_b_.resize( esn_->neurons_ );
00082     std::fill_n( tanh2_a_.data(), esn_->neurons_, 1. );
00083     std::fill_n( tanh2_b_.data(), esn_->neurons_, 0. );
00084   }
00085 
00086   // set filtered neurons to standard ESN calculation
00087   int simalg = esn_->net_info_[ESN<T>::SIMULATE_ALG];
00088   if( simalg==SIM_FILTER || simalg==SIM_FILTER2 || simalg==SIM_FILTER_DS || simalg==SIM_SQUARE )
00089   {
00090     typename DEMatrix<T>::Type B(esn_->neurons_, 2), A(esn_->neurons_, 2);
00091 
00092     for(int i=1; i<=esn_->neurons_; ++i)
00093     {
00094       B(i,1) = 1.; B(i,2) = 0.;
00095       A(i,1) = 1.; A(i,2) = 0.;
00096     }
00097     esn_->setIIRCoeff(B,A);
00098   }
00099 }
00100 
00102 
00103 
00104 
00105 template <typename T>
00106 void InitStd<T>::init()
00107   throw(AUExcept)
00108 {
00109   this->checkInitParams();
00110   this->allocateWorkData();
00111 
00112   esn_->Win_.resizeOrClear(esn_->neurons_, esn_->inputs_);
00113   esn_->Wback_.resizeOrClear(esn_->neurons_, esn_->outputs_);
00114   esn_->Wout_.resizeOrClear(esn_->outputs_, esn_->neurons_+esn_->inputs_);
00115   esn_->x_.resizeOrClear(esn_->neurons_);
00116 
00117 
00118   // INPUT WEIGHTS
00119 
00120   // generate random weigths within [-1,1] with given connectivity
00121   T nrn = esn_->neurons_*esn_->init_params_[IN_CONNECTIVITY]
00122           * esn_->inputs_ - 0.5;
00123   int i;
00124   for (i=0; i<nrn; ++i)
00125     esn_->Win_.data()[i] = Rand<T>::uniform();
00126 
00127   // shuffle elements within the matrix
00128   int mtxsize = esn_->Win_.numRows() * esn_->Win_.numCols();
00129   std::random_shuffle( esn_->Win_.data(),
00130                        esn_->Win_.data() + mtxsize );
00131 
00132   // scale and shift elemets
00133   esn_->Win_ *= esn_->init_params_[IN_SCALE];
00134   esn_->Win_ += esn_->init_params_[IN_SHIFT];
00135 
00136 
00137   // FEEDBACK WEIGHTS
00138 
00139   // generate random weigths within [-1,1] with given connectivity
00140   nrn = esn_->neurons_*esn_->init_params_[FB_CONNECTIVITY]
00141         * esn_->outputs_ - 0.5;
00142   for (int i=0; i<nrn; ++i)
00143     esn_->Wback_.data()[i] = Rand<T>::uniform();
00144 
00145   // shuffle elements within the matrix
00146   mtxsize = esn_->Wback_.numRows() * esn_->Wback_.numCols();
00147   std::random_shuffle( esn_->Wback_.data(),
00148                        esn_->Wback_.data() + mtxsize );
00149 
00150   // scale and shift elemets
00151   esn_->Wback_ *= esn_->init_params_[FB_SCALE];
00152   esn_->Wback_ += esn_->init_params_[FB_SHIFT];
00153 
00154 
00155   // RESERVOIR WEIGHTS
00156 
00157   // temporal dense reservoir weight matrix
00158   typename DEMatrix<T>::Type Wtmp(esn_->neurons_, esn_->neurons_),
00159                              tmp(esn_->neurons_, esn_->neurons_);
00160 
00161   // generate random weigths within [-1,1] with given connectivity
00162   nrn = esn_->neurons_*esn_->neurons_*esn_->init_params_[CONNECTIVITY] - 0.5;
00163   for (int i=0; i<nrn; ++i)
00164     Wtmp.data()[i] = Rand<T>::uniform();
00165 
00166   // shuffle elements within the matrix
00167   std::random_shuffle( Wtmp.data(), 
00168                        Wtmp.data() + (Wtmp.numRows()*Wtmp.numCols()) );
00169 
00170   // calc eigenvalues of Wtmp
00171   typename DEVector<T>::Type wr(esn_->neurons_),
00172                              wi(esn_->neurons_),
00173                              w(esn_->neurons_);
00174   tmp = Wtmp; // needs a deep copy for ev
00175 
00176   flens::ev(false, false, tmp, wr, wi, tmp, tmp); // last 2 are dummy for EV
00177 
00178   // calc largest absolut eigenvalue
00179   for(int i=1; i<=esn_->neurons_; ++i)
00180     w(i) = sqrt( pow(wr(i),2) + pow(wi(i),2) );
00181   T max_ew = *std::max_element( w.data(), w.data()+w.length() );
00182 
00183   // check if we have zero max_ew
00184   if( max_ew == 0 )
00185     throw AUExcept("InitStd::init: maximum eigenvalue is zero ! Try init again!");
00186 
00187   // scale matrix to spectral radius alpha
00188   T alpha = esn_->init_params_[ALPHA];
00189   Wtmp *= (alpha / max_ew);
00190 
00191   // finally convert it to sparse matrix
00192 //   esn_->W_.initWith(Wtmp, 1E-9);
00193   esn_->W_ = Wtmp; 
00194 }
00195 
00197 
00198 } // end of namespace aureservoir

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