src/modules/convex/mlp_igd.cpp
/**
 * @brief Perform the multilayer perceptron transition step
 *
 * Called for each tuple.
 */
@brief Perform the multilayer perceptron transition step
Called for each tuple.
@brief Perform the multilayer perceptron transition step Called for each tuple .
AnyType
mlp_igd_transition::run(AnyType &args) {
    MLPIGDState<MutableArrayHandle<double> > state = args[0];
    if (state.algo.numRows == 0) {
        if (!args[3].isNull()) {
            MLPIGDState<ArrayHandle<double> > previousState = args[3];
            state.allocate(*this, previousState.task.numberOfStages,
                           previousState.task.numbersOfUnits);
            state = previousState;
        } else {
            ArrayHandle<double> numbersOfUnits = args[4].getAs<ArrayHandle<double> >();
            int numberOfStages = numbersOfUnits.size() - 1;
            double stepsize = args[5].getAs<double>();
            state.allocate(*this, numberOfStages,
                           reinterpret_cast<const double *>(numbersOfUnits.ptr()));
            state.task.stepsize = stepsize;
            const int activation = args[6].getAs<int>();
            const int is_classification = args[7].getAs<int>();
            const bool warm_start = args[9].getAs<bool>();
            const double lambda = args[11].getAs<double>();
            state.task.lambda = lambda;
            MLPTask::lambda = lambda;
            double is_classification_double = (double) is_classification;
            double activation_double = (double) activation;
            MappedColumnVector coeff = args[10].getAs<MappedColumnVector>();
            state.task.model.rebind(&is_classification_double,&activation_double,
                                    &coeff.data()[0], numberOfStages,
                                    &numbersOfUnits[0]);
        }
        state.reset();
    }
    ColumnVector indVar;
    MappedColumnVector depVar;
    try {
        indVar = args[1].getAs<MappedColumnVector>();
        MappedColumnVector y = args[2].getAs<MappedColumnVector>();
        depVar.rebind(y.memoryHandle(), y.size());
    } catch (const ArrayWithNullException &e) {
        return args[0];
    }
    MLPTuple tuple;
    tuple.indVar = indVar;
    tuple.depVar.rebind(depVar.memoryHandle(), depVar.size());
    tuple.weight = args[8].getAs<double>();
    MLPIGDAlgorithm::transition(state, tuple);
    MLPLossAlgorithm::transition(state, tuple);
    state.algo.numRows ++;
    return state;
}
AnyType mlp_igd_transition :: run ( AnyType & args ) { MLPIGDState < MutableArrayHandle < double > > state = args [ 0 ] ; if ( state . algo . numRows == 0 ) { if ( ! args [ 3 ] . isNull ( ) ) { MLPIGDState < ArrayHandle < double > > previousState = args [ 3 ] ; state . allocate ( * this , previousState . task . numberOfStages , previousState . task . numbersOfUnits ) ; state = previousState ; } else { ArrayHandle < double > numbersOfUnits = args [ 4 ] . getAs < ArrayHandle < double > > ( ) ; int numberOfStages = numbersOfUnits . size ( ) - 1 ; double stepsize = args [ 5 ] . getAs < double > ( ) ; state . allocate ( * this , numberOfStages , reinterpret_cast < const double * > ( numbersOfUnits . ptr ( ) ) ) ; state . task . stepsize = stepsize ; const int activation = args [ 6 ] . getAs < int > ( ) ; const int is_classification = args [ 7 ] . getAs < int > ( ) ; const bool warm_start = args [ 9 ] . getAs < bool > ( ) ; const double lambda = args [ 11 ] . getAs < double > ( ) ; state . task . lambda = lambda ; MLPTask :: lambda = lambda ; double is_classification_double = ( double ) is_classification ; double activation_double = ( double ) activation ; MappedColumnVector coeff = args [ 10 ] . getAs < MappedColumnVector > ( ) ; state . task . model . rebind ( & is_classification_double , & activation_double , & coeff . data ( ) [ 0 ] , numberOfStages , & numbersOfUnits [ 0 ] ) ; } state . reset ( ) ; } ColumnVector indVar ; MappedColumnVector depVar ; try { indVar = args [ 1 ] . getAs < MappedColumnVector > ( ) ; MappedColumnVector y = args [ 2 ] . getAs < MappedColumnVector > ( ) ; depVar . rebind ( y . memoryHandle ( ) , y . size ( ) ) ; } catch ( const ArrayWithNullException & e ) { return args [ 0 ] ; } MLPTuple tuple ; tuple . indVar = indVar ; tuple . depVar . rebind ( depVar . memoryHandle ( ) , depVar . size ( ) ) ; tuple . weight = args [ 8 ] . getAs < double > ( ) ; MLPIGDAlgorithm :: transition ( state , tuple ) ; MLPLossAlgorithm :: transition ( state , tuple ) ; state . algo . numRows ++ ; return state ; }
@brief Perform the multilayer perceptron transition step
 Called for each tuple.
@brief Perform the multilayer perceptron transition step Called for each tuple .
// For the first tuple: args[0] is nothing more than a marker that // indicates that we should do some initial operations. // For other tuples: args[0] holds the computation state until last tuple // initilize the state if first tuple // configuration parameters and initialization // this is run only once (first iteration, first tuple) // args[8] is for weighting the input row, which is populated later. // state.task.model.is_classification = //     static_cast<double>(is_classification); // state.task.model.activation = static_cast<double>(activation); // MappedColumnVector initial_coeff = args[10].getAs<MappedColumnVector>(); // // copy initial_coeff into the model // Index fan_in, fan_out, layer_start = 0; // for (size_t k = 0; k < numberOfStages; ++k){ //     fan_in = numbersOfUnits[k]; //     fan_out = numbersOfUnits[k+1]; //     state.task.model.u[k] << initial_coeff.segment(layer_start, (fan_in+1)*fan_out); //     layer_start = (fan_in + 1) * fan_out; // } // resetting in either case // tuple // Use the model from the previous iteration to compute the loss (note that // it is stored in Task's state, and the Algo's state holds the model from // the current iteration.