src/ComputeState.cpp
ArrayBO EchoBay::update_state(const std::vector<MatrixBO> &WinL,
                              const std::vector<SparseBO> &WrL,
                              const Eigen::Ref<ArrayBO> src, std::vector<ArrayBO> &stateMat,
                              const std::vector<layerParameter> &layerConfig)
{
    // Variables
    int nLayers = WinL.size();
    int i;

    // Leaky state
    // Allocate deep states
    std::vector<ArrayBO> prevState;
    prevState.reserve(nLayers);
    std::vector<ArrayBO> layerInput;
    layerInput.reserve(nLayers - 1); //TODO check if we need to save this
    prevState.push_back(stateMat[0]);
    for (i = 1; i < nLayers; i++)
    {
        prevState.push_back(stateMat[i]);
        ArrayBO inputSupport(stateMat[i - 1].rows() + 1);
        inputSupport << stateMat[i - 1], 1.0;
        layerInput.push_back(inputSupport);
    }

    // Leaky factor
    ArrayBO leakyFactor(nLayers);
    ArrayBO leaky(nLayers);
    i = 0;
    for(const auto& layer: layerConfig)
    {
        leaky[i] = layer.leaky;
        ++i;
    }
    leakyFactor = 1.0 - leaky;

    // update input
    ArrayBO u = ArrayBO::Constant(src.cols() + 1, 1.0);
    size_t uSize = src.cols();
    u.head(uSize) = src;

    // Update leaky state
    prevState[0] = leakyFactor[0] * stateMat[0];

    // Compute new state
    stateMat[0] = WinL[0] * u.matrix() + WrL[0] * stateMat[0].matrix();
    stateMat[0] = prevState[0] + leaky[0] * stateMat[0].tanh();
    for (i = 1; i < nLayers; ++i)
    {
        // Update leaky state
        prevState[i] = leakyFactor[i] * stateMat[i];
        layerInput[i - 1] << stateMat[i - 1], 1.0;

        // Compute new state
        stateMat[i] = WinL[i] * layerInput[i - 1].matrix() + WrL[i] * stateMat[i].matrix();
        stateMat[i] = prevState[i] + leaky[i] * stateMat[i].tanh();
    }

    // Return final state TODO decide how to manage this in DeepESN
    return stateMat[nLayers - 1];
}
/**
 * @brief Single state update with leaky integration and deep layers
 * 
 * @param WinL Vector containing input scaling Matrices
 * @param WrL Vector containing reservoir weights matrices
 * @param src Input data
 * @param stateMat ESN state Matrix, one Array for each layer
 * @param layerConfig Layers configuration vector
 * @return ArrayBO Output state of the last layer
 */
@brief Single state update with leaky integration and deep layers


@brief Single state update with leaky integration and deep layers
ArrayBO EchoBay::update_state(const std::vector<MatrixBO> &WinL,
                              const std::vector<SparseBO> &WrL,
                              const Eigen::Ref<ArrayBO> src, std::vector<ArrayBO> &stateMat,
                              const std::vector<layerParameter> &layerConfig)
{
    int nLayers = WinL.size();
    int i;
    std::vector<ArrayBO> prevState;
    prevState.reserve(nLayers);
    std::vector<ArrayBO> layerInput;
    layerInput.reserve(nLayers - 1); 
    prevState.push_back(stateMat[0]);
    for (i = 1; i < nLayers; i++)
    {
        prevState.push_back(stateMat[i]);
        ArrayBO inputSupport(stateMat[i - 1].rows() + 1);
        inputSupport << stateMat[i - 1], 1.0;
        layerInput.push_back(inputSupport);
    }
    ArrayBO leakyFactor(nLayers);
    ArrayBO leaky(nLayers);
    i = 0;
    for(const auto& layer: layerConfig)
    {
        leaky[i] = layer.leaky;
        ++i;
    }
    leakyFactor = 1.0 - leaky;
    ArrayBO u = ArrayBO::Constant(src.cols() + 1, 1.0);
    size_t uSize = src.cols();
    u.head(uSize) = src;
    prevState[0] = leakyFactor[0] * stateMat[0];
    stateMat[0] = WinL[0] * u.matrix() + WrL[0] * stateMat[0].matrix();
    stateMat[0] = prevState[0] + leaky[0] * stateMat[0].tanh();
    for (i = 1; i < nLayers; ++i)
    {
        prevState[i] = leakyFactor[i] * stateMat[i];
        layerInput[i - 1] << stateMat[i - 1], 1.0;
        stateMat[i] = WinL[i] * layerInput[i - 1].matrix() + WrL[i] * stateMat[i].matrix();
        stateMat[i] = prevState[i] + leaky[i] * stateMat[i].tanh();
    }
    return stateMat[nLayers - 1];
}
ArrayBO EchoBay :: update_state ( const std :: vector < MatrixBO > & WinL , const std :: vector < SparseBO > & WrL , const Eigen :: Ref < ArrayBO > src , std :: vector < ArrayBO > & stateMat , const std :: vector < layerParameter > & layerConfig ) { int nLayers = WinL . size ( ) ; int i ; std :: vector < ArrayBO > prevState ; prevState . reserve ( nLayers ) ; std :: vector < ArrayBO > layerInput ; layerInput . reserve ( nLayers - 1 ) ; prevState . push_back ( stateMat [ 0 ] ) ; for ( i = 1 ; i < nLayers ; i ++ ) { prevState . push_back ( stateMat [ i ] ) ; ArrayBO inputSupport ( stateMat [ i - 1 ] . rows ( ) + 1 ) ; inputSupport << stateMat [ i - 1 ] , 1.0 ; layerInput . push_back ( inputSupport ) ; } ArrayBO leakyFactor ( nLayers ) ; ArrayBO leaky ( nLayers ) ; i = 0 ; for ( const auto & layer : layerConfig ) { leaky [ i ] = layer . leaky ; ++ i ; } leakyFactor = 1.0 - leaky ; ArrayBO u = ArrayBO :: Constant ( src . cols ( ) + 1 , 1.0 ) ; size_t uSize = src . cols ( ) ; u . head ( uSize ) = src ; prevState [ 0 ] = leakyFactor [ 0 ] * stateMat [ 0 ] ; stateMat [ 0 ] = WinL [ 0 ] * u . matrix ( ) + WrL [ 0 ] * stateMat [ 0 ] . matrix ( ) ; stateMat [ 0 ] = prevState [ 0 ] + leaky [ 0 ] * stateMat [ 0 ] . tanh ( ) ; for ( i = 1 ; i < nLayers ; ++ i ) { prevState [ i ] = leakyFactor [ i ] * stateMat [ i ] ; layerInput [ i - 1 ] << stateMat [ i - 1 ] , 1.0 ; stateMat [ i ] = WinL [ i ] * layerInput [ i - 1 ] . matrix ( ) + WrL [ i ] * stateMat [ i ] . matrix ( ) ; stateMat [ i ] = prevState [ i ] + leaky [ i ] * stateMat [ i ] . tanh ( ) ; } return stateMat [ nLayers - 1 ] ; }
@brief Single state update with leaky integration and deep layers
@brief Single state update with leaky integration and deep layers
// Variables // Leaky state // Allocate deep states //TODO check if we need to save this // Leaky factor // update input // Update leaky state // Compute new state // Update leaky state // Compute new state // Return final state TODO decide how to manage this in DeepESN