src/esn.cpp
ArrayBO EchoBay::esn_train(YAML::Node confParams, Reservoir &ESN, const double lambda,
                      const std::string problemType, const std::string fitnessRule,
                      const std::string outputFolder, const DataStorage &store, const bool saveflag, 
                      const int guesses)
{
    // ESN general Parameters
    // Deep ESN
    int nLayers = ESN.get_nLayers();
    // Readout blockStep
    int blockStep = confParams["blockStep"].as<int>();

    // Construct comparator class
    Comparator fitter(problemType, fitnessRule);

    // Calculate readout matrix depending on the problem
    ArrayI8 trainSampling = store.get_sampleArray(EchoBay::Train);
    MatrixBO targetMatrix = fitter.get_targetMatrix(store.get_data(EchoBay::Train, EchoBay::selLabel), trainSampling);

    // Calculate Wout as readout layer
    MatrixBO Wout = readout_train(ESN, store, targetMatrix, lambda, blockStep);
    // Calculate prediction states
    // stateMat is already initialized with last training state

    // If saveflag is set, save the trainingState before being overwritten by evalState
    std::vector<ArrayBO> saveState;
    if (saveflag)
    {
        saveState.reserve(nLayers);
        saveState = ESN.stateMat;
    }

    MatrixBO prediction = readout_predict(ESN, store, Wout, blockStep);

    // Calculate fitness
    // Resize output
    fitter.set_label_size(prediction.rows(), prediction.cols());
    // Get fitness
    ArrayI8 evalSampling = store.get_sampleArray(EchoBay::Valid);
    fitter.set_targetLabel(store.get_data(EchoBay::Valid, EchoBay::selLabel), evalSampling);
    floatBO fitness = fitter.get_fitness(prediction);

    MatrixBO outputLabel;
    outputLabel = fitter.get_outputlabel();

    // Consider memory penalization
    floatBO memory = ESN.return_net_dimension(confParams);
    ArrayBO multiFitness(2);
    multiFitness(0) = fitness;
    multiFitness(1) = memory;

    // Flag used to save just when the parameters have been chosen
    // See main.cpp for clarity
    if (saveflag)
    {
        if (!boost::filesystem::exists(outputFolder))
        {
            boost::filesystem::create_directories(outputFolder);
        }
        //write_matrix<MatrixBO>(outputFolder + std::string("/State.csv"), biasedState);
        std::string tempName;
        for (int i = 0; i < nLayers; ++i)
        {
            tempName = "/Win_eigen" + std::to_string(i) + ".mtx";
            //saveMarket(ESN.WinLayers[0], outputFolder + tempName);
            write_matrix<MatrixBO>(outputFolder + tempName, ESN.WinLayers[i]);
            tempName = "/Wr_eigen" + std::to_string(i) + ".mtx";
            //write_matrix<MatrixBO>(outputFolder + tempName, ESN.WrLayers[i]);
            saveMarket(ESN.WrLayers[i], outputFolder + tempName);

            tempName = "/State_eigen" + std::to_string(i) + ".mtx";
            write_matrix<MatrixBO>(outputFolder + tempName, saveState[i]);
            //saveMarket(Wout, outputFolder + std::string("/Wout_eigen.mtx"));
        }

        std::string nameOut;
        nameOut = "/outputLabel_" + std::to_string(guesses) + ".csv";
        write_matrix<MatrixBO>(outputFolder + std::string(nameOut), prediction);

        nameOut = "/final_state_" + std::to_string(guesses) + ".dat";
        write_array<ArrayBO>(outputFolder + std::string("/final_state.dat"), saveState[nLayers - 1]);

        write_matrix<MatrixBO>(outputFolder + std::string("/Wout_eigen.dat"), Wout);

        if (confParams["Guesses"].as<int>() == (guesses + 1))
        {
            std::cout << "Computation ended \n";
        }
    }

    //return fitness - MemoryPenFactor * Memory;
    return multiFitness;
}
/**
 * @brief Perform Echo State Network training
 * 
 * @param confParams YAML Node containing hyper-parameters at high level
 * @param ESN Reservoir Object
 * @param lambda Regression lambda factor see EchoBay::readout_train(Reservoir &ESN, const MatrixBO &trainData,
                                const Eigen::Ref<const ArrayBO> sampleState,
                                Eigen::Ref<MatrixBO> target, double lambda, int blockStep)
 * @param problemType Classification, Regression or MemoryCapacity
 * @param fitnessRule Fitness function dependent from the chosen problemType
 * @param outputFolder Path to the folder where the output will be saved
 * @param store DataStorage object containing training data and labels. See also EchoBay::DataStorage
 * @param saveflag If true, save the output and trained ESN matrices on a file
 * @param guesses Number of guesses to be evaluated (used for output naming)
 * @return ArrayBO Return fitness value to the Bayesian Optimizer
 */
@brief Perform Echo State Network training


@brief Perform Echo State Network training
ArrayBO EchoBay::esn_train(YAML::Node confParams, Reservoir &ESN, const double lambda,
                      const std::string problemType, const std::string fitnessRule,
                      const std::string outputFolder, const DataStorage &store, const bool saveflag, 
                      const int guesses)
{
    int nLayers = ESN.get_nLayers();
    int blockStep = confParams["blockStep"].as<int>();
    Comparator fitter(problemType, fitnessRule);
    ArrayI8 trainSampling = store.get_sampleArray(EchoBay::Train);
    MatrixBO targetMatrix = fitter.get_targetMatrix(store.get_data(EchoBay::Train, EchoBay::selLabel), trainSampling);
    MatrixBO Wout = readout_train(ESN, store, targetMatrix, lambda, blockStep);
    std::vector<ArrayBO> saveState;
    if (saveflag)
    {
        saveState.reserve(nLayers);
        saveState = ESN.stateMat;
    }
    MatrixBO prediction = readout_predict(ESN, store, Wout, blockStep);
    fitter.set_label_size(prediction.rows(), prediction.cols());
    ArrayI8 evalSampling = store.get_sampleArray(EchoBay::Valid);
    fitter.set_targetLabel(store.get_data(EchoBay::Valid, EchoBay::selLabel), evalSampling);
    floatBO fitness = fitter.get_fitness(prediction);
    MatrixBO outputLabel;
    outputLabel = fitter.get_outputlabel();
    floatBO memory = ESN.return_net_dimension(confParams);
    ArrayBO multiFitness(2);
    multiFitness(0) = fitness;
    multiFitness(1) = memory;
    if (saveflag)
    {
        if (!boost::filesystem::exists(outputFolder))
        {
            boost::filesystem::create_directories(outputFolder);
        }
        std::string tempName;
        for (int i = 0; i < nLayers; ++i)
        {
            tempName = "/Win_eigen" + std::to_string(i) + ".mtx";
            write_matrix<MatrixBO>(outputFolder + tempName, ESN.WinLayers[i]);
            tempName = "/Wr_eigen" + std::to_string(i) + ".mtx";
            saveMarket(ESN.WrLayers[i], outputFolder + tempName);
            tempName = "/State_eigen" + std::to_string(i) + ".mtx";
            write_matrix<MatrixBO>(outputFolder + tempName, saveState[i]);
        }
        std::string nameOut;
        nameOut = "/outputLabel_" + std::to_string(guesses) + ".csv";
        write_matrix<MatrixBO>(outputFolder + std::string(nameOut), prediction);
        nameOut = "/final_state_" + std::to_string(guesses) + ".dat";
        write_array<ArrayBO>(outputFolder + std::string("/final_state.dat"), saveState[nLayers - 1]);
        write_matrix<MatrixBO>(outputFolder + std::string("/Wout_eigen.dat"), Wout);
        if (confParams["Guesses"].as<int>() == (guesses + 1))
        {
            std::cout << "Computation ended \n";
        }
    }
    return multiFitness;
}
ArrayBO EchoBay :: esn_train ( YAML :: Node confParams , Reservoir & ESN , const double lambda , const std :: string problemType , const std :: string fitnessRule , const std :: string outputFolder , const DataStorage & store , const bool saveflag , const int guesses ) { int nLayers = ESN . get_nLayers ( ) ; int blockStep = confParams [ " " ] . as < int > ( ) ; Comparator fitter ( problemType , fitnessRule ) ; ArrayI8 trainSampling = store . get_sampleArray ( EchoBay :: Train ) ; MatrixBO targetMatrix = fitter . get_targetMatrix ( store . get_data ( EchoBay :: Train , EchoBay :: selLabel ) , trainSampling ) ; MatrixBO Wout = readout_train ( ESN , store , targetMatrix , lambda , blockStep ) ; std :: vector < ArrayBO > saveState ; if ( saveflag ) { saveState . reserve ( nLayers ) ; saveState = ESN . stateMat ; } MatrixBO prediction = readout_predict ( ESN , store , Wout , blockStep ) ; fitter . set_label_size ( prediction . rows ( ) , prediction . cols ( ) ) ; ArrayI8 evalSampling = store . get_sampleArray ( EchoBay :: Valid ) ; fitter . set_targetLabel ( store . get_data ( EchoBay :: Valid , EchoBay :: selLabel ) , evalSampling ) ; floatBO fitness = fitter . get_fitness ( prediction ) ; MatrixBO outputLabel ; outputLabel = fitter . get_outputlabel ( ) ; floatBO memory = ESN . return_net_dimension ( confParams ) ; ArrayBO multiFitness ( 2 ) ; multiFitness ( 0 ) = fitness ; multiFitness ( 1 ) = memory ; if ( saveflag ) { if ( ! boost :: filesystem :: exists ( outputFolder ) ) { boost :: filesystem :: create_directories ( outputFolder ) ; } std :: string tempName ; for ( int i = 0 ; i < nLayers ; ++ i ) { tempName = " " + std :: to_string ( i ) + " " ; write_matrix < MatrixBO > ( outputFolder + tempName , ESN . WinLayers [ i ] ) ; tempName = " " + std :: to_string ( i ) + " " ; saveMarket ( ESN . WrLayers [ i ] , outputFolder + tempName ) ; tempName = " " + std :: to_string ( i ) + " " ; write_matrix < MatrixBO > ( outputFolder + tempName , saveState [ i ] ) ; } std :: string nameOut ; nameOut = " " + std :: to_string ( guesses ) + " " ; write_matrix < MatrixBO > ( outputFolder + std :: string ( nameOut ) , prediction ) ; nameOut = " " + std :: to_string ( guesses ) + " " ; write_array < ArrayBO > ( outputFolder + std :: string ( " " ) , saveState [ nLayers - 1 ] ) ; write_matrix < MatrixBO > ( outputFolder + std :: string ( " " ) , Wout ) ; if ( confParams [ " " ] . as < int > ( ) == ( guesses + 1 ) ) { std :: cout << " \n " ; } } return multiFitness ; }
@brief Perform Echo State Network training
@brief Perform Echo State Network training
// ESN general Parameters // Deep ESN // Readout blockStep // Construct comparator class // Calculate readout matrix depending on the problem // Calculate Wout as readout layer // Calculate prediction states // stateMat is already initialized with last training state // If saveflag is set, save the trainingState before being overwritten by evalState // Calculate fitness // Resize output // Get fitness // Consider memory penalization // Flag used to save just when the parameters have been chosen // See main.cpp for clarity //write_matrix<MatrixBO>(outputFolder + std::string("/State.csv"), biasedState); //saveMarket(ESN.WinLayers[0], outputFolder + tempName); //write_matrix<MatrixBO>(outputFolder + tempName, ESN.WrLayers[i]); //saveMarket(Wout, outputFolder + std::string("/Wout_eigen.mtx")); //return fitness - MemoryPenFactor * Memory;