returnValue ShootingMethod::differentiateBackward( const int &idx , const Matrix &seed, Matrix &Gx , Matrix &Gp , Matrix &Gu , Matrix &Gw ){ uint run1; Gx.init( seed.getNumRows(), nx ); Gp.init( seed.getNumRows(), np ); Gu.init( seed.getNumRows(), nu ); Gw.init( seed.getNumRows(), nw ); for( run1 = 0; run1 < seed.getNumRows(); run1++ ){ Vector tmp = seed.getRow( run1 ); Vector tmpX( nx ); Vector tmpP( np ); Vector tmpU( nu ); Vector tmpW( nw ); ACADO_TRY( integrator[idx]->setBackwardSeed( 1, tmp ) ); ACADO_TRY( integrator[idx]->integrateSensitivities( ) ); ACADO_TRY( integrator[idx]->getBackwardSensitivities( tmpX, tmpP, tmpU, tmpW , 1 ) ); Gx.setRow( run1, tmpX ); Gp.setRow( run1, tmpP ); Gu.setRow( run1, tmpU ); Gw.setRow( run1, tmpW ); } return SUCCESSFUL_RETURN; }
float SelectorLearner::run() { const int numClasses = _pTrainingData->getNumClasses(); const int numColumns = _pTrainingData->getNumAttributes(); const int numExamples = _pTrainingData->getNumExamples(); // set the smoothing value to avoid numerical problem // when theta=0. setSmoothingVal( 1.0 / (float)_pTrainingData->getNumExamples() * 0.01 ); vector<sRates> vMu(numClasses); // The class-wise rates. See BaseLearner::sRates for more info. vector<float> tmpV(numClasses); // The class-wise votes/abstentions float tmpAlpha, tmpEnergy; float bestEnergy = numeric_limits<float>::max(); int numOfDimensions = _maxNumOfDimensions; for (int j = 0; j < numColumns; ++j) { // Tricky way to select numOfDimensions columns randomly out of numColumns int rest = numColumns - j; float r = rand()/static_cast<float>(RAND_MAX); if ( static_cast<float>(numOfDimensions) / rest > r ) { --numOfDimensions; /* if (_verbose > 2) cout << " --> trying attribute = " <<_pTrainingData->getAttributeNameMap().getNameFromIdx(j) << endl << flush; */ const int numIdxs = _pTrainingData->getEnumMap(j).getNumNames(); // Create and initialize the numIdxs x numClasses gamma matrix vector<vector<float> > tmpGammasPls(numIdxs); vector<vector<float> > tmpGammasMin(numIdxs); for (int io = 0; io < numIdxs; ++io) { vector<float> tmpGammaPls(numClasses); vector<float> tmpGammaMin(numClasses); fill(tmpGammaPls.begin(), tmpGammaPls.end(), 0.0); fill(tmpGammaMin.begin(), tmpGammaMin.end(), 0.0); tmpGammasPls[io] = tmpGammaPls; tmpGammasMin[io] = tmpGammaMin; } // Compute the elements of the gamma plus and minus matrices float entry; for (int i = 0; i < numExamples; ++i) { const vector<Label>& labels = _pTrainingData->getLabels(i); int io = static_cast<int>(_pTrainingData->getValue(i,j)); for (int l = 0; l < numClasses; ++l) { entry = labels[l].weight * labels[l].y; if (entry > 0) tmpGammasPls[io][l] += entry; else if (entry < 0) tmpGammasMin[io][l] += -entry; } } // Initialize the u vector to random +-1 vector<sRates> uMu(numIdxs); // The idx-wise rates vector<float> tmpU(numIdxs);// The idx-wise votes/abstentions vector<float> previousTmpU(numIdxs);// The idx-wise votes/abstentions for (int io = 0; io < numIdxs; ++io) { uMu[io].classIdx = io; } for (int io = 0; io < numIdxs; ++io) { // initializing u as it has only one positive element fill( tmpU.begin(), tmpU.end(), -1 ); tmpU[io] = +1; vector<sRates> vMu(numClasses); // The label-wise rates for (int l = 0; l < numClasses; ++l) vMu[l].classIdx = l; vector<float> tmpV(numClasses); // The label-wise votes/abstentions float tmpVal; tmpAlpha = 0.0; //filling out tmpV and vMu for (int l = 0; l < numClasses; ++l) { vMu[l].rPls = vMu[l].rMin = vMu[l].rZero = 0; for (int io = 0; io < numIdxs; ++io) { if (tmpU[io] > 0) { vMu[l].rPls += tmpGammasPls[io][l]; vMu[l].rMin += tmpGammasMin[io][l]; } else if (tmpU[io] < 0) { vMu[l].rPls += tmpGammasMin[io][l]; vMu[l].rMin += tmpGammasPls[io][l]; } } if (vMu[l].rPls >= vMu[l].rMin) { tmpV[l] = +1; } else { tmpV[l] = -1; tmpVal = vMu[l].rPls; vMu[l].rPls = vMu[l].rMin; vMu[l].rMin = tmpVal; } } tmpEnergy = AbstainableLearner::getEnergy(vMu, tmpAlpha, tmpV); if ( tmpEnergy < bestEnergy && tmpAlpha > 0 ) { _alpha = tmpAlpha; _v = tmpV; //_u = tmpU; _positiveIdxOfArrayU = io; _selectedColumn = j; bestEnergy = tmpEnergy; } } } } if (_selectedColumn>-1) { _id = _pTrainingData->getAttributeNameMap().getNameFromIdx(_selectedColumn); return bestEnergy; } else { return bestEnergy = numeric_limits<float>::signaling_NaN(); } }
float EnumLearner::run( int colIdx ) { const int numClasses = _pTrainingData->getNumClasses(); const int numColumns = _pTrainingData->getNumAttributes(); const int numExamples = _pTrainingData->getNumExamples(); // set the smoothing value to avoid numerical problem // when theta=0. setSmoothingVal( 1.0 / (float)_pTrainingData->getNumExamples() * 0.01 ); vector<sRates> vMu(numClasses); // The class-wise rates. See BaseLearner::sRates for more info. vector<float> tmpV(numClasses); // The class-wise votes/abstentions vector<float> previousTmpV(numClasses); // The class-wise votes/abstentions float tmpAlpha,previousTmpAlpha, previousEnergy; float bestEnergy = numeric_limits<float>::max(); int numOfDimensions = _maxNumOfDimensions; // Tricky way to select numOfDimensions columns randomly out of numColumns int j = colIdx; if (_verbose > 2) cout << " --> trying attribute = " <<_pTrainingData->getAttributeNameMap().getNameFromIdx(j) << endl << flush; const int numIdxs = _pTrainingData->getEnumMap(j).getNumNames(); // Create and initialize the numIdxs x numClasses gamma matrix vector<vector<float> > tmpGammasPls(numIdxs); vector<vector<float> > tmpGammasMin(numIdxs); for (int io = 0; io < numIdxs; ++io) { vector<float> tmpGammaPls(numClasses); vector<float> tmpGammaMin(numClasses); fill(tmpGammaPls.begin(), tmpGammaPls.end(), 0.0); fill(tmpGammaMin.begin(), tmpGammaMin.end(), 0.0); tmpGammasPls[io] = tmpGammaPls; tmpGammasMin[io] = tmpGammaMin; } // Compute the elements of the gamma plus and minus matrices float entry; for (int i = 0; i < numExamples; ++i) { const vector<Label>& labels = _pTrainingData->getLabels(i); int io = static_cast<int>(_pTrainingData->getValue(i,j)); for (int l = 0; l < numClasses; ++l) { entry = labels[l].weight * labels[l].y; if (entry > 0) tmpGammasPls[io][l] += entry; else if (entry < 0) tmpGammasMin[io][l] += -entry; } } // Initialize the u vector to random +-1 vector<sRates> uMu(numIdxs); // The idx-wise rates vector<float> tmpU(numIdxs);// The idx-wise votes/abstentions vector<float> previousTmpU(numIdxs);// The idx-wise votes/abstentions for (int io = 0; io < numIdxs; ++io) { uMu[io].classIdx = io; if ( rand()/static_cast<float>(RAND_MAX) > 0.5 ) tmpU[io] = +1; else tmpU[io] = -1; } //vector<sRates> vMu(numClasses); // The label-wise rates for (int l = 0; l < numClasses; ++l) vMu[l].classIdx = l; //vector<float> tmpV(numClasses); // The label-wise votes/abstentions float tmpEnergy = numeric_limits<float>::max(); float tmpVal; tmpAlpha = 0.0; while (1) { previousEnergy = tmpEnergy; previousTmpV = tmpV; previousTmpAlpha = tmpAlpha; //filling out tmpV and vMu for (int l = 0; l < numClasses; ++l) { vMu[l].rPls = vMu[l].rMin = vMu[l].rZero = 0; for (int io = 0; io < numIdxs; ++io) { if (tmpU[io] > 0) { vMu[l].rPls += tmpGammasPls[io][l]; vMu[l].rMin += tmpGammasMin[io][l]; } else if (tmpU[io] < 0) { vMu[l].rPls += tmpGammasMin[io][l]; vMu[l].rMin += tmpGammasPls[io][l]; } } if (vMu[l].rPls >= vMu[l].rMin) { tmpV[l] = +1; } else { tmpV[l] = -1; tmpVal = vMu[l].rPls; vMu[l].rPls = vMu[l].rMin; vMu[l].rMin = tmpVal; } } tmpEnergy = AbstainableLearner::getEnergy(vMu, tmpAlpha, tmpV); if (_verbose > 2) cout << " --> energy V = " << tmpEnergy << "\talpha = " << tmpAlpha << endl << flush; if (tmpEnergy >= previousEnergy) { tmpV = previousTmpV; break; } previousEnergy = tmpEnergy; previousTmpU = tmpU; previousTmpAlpha = tmpAlpha; //filling out tmpU and uMu for (int io = 0; io < numIdxs; ++io) { uMu[io].rPls = uMu[io].rMin = uMu[io].rZero = 0; for (int l = 0; l < numClasses; ++l) { if (tmpV[l] > 0) { uMu[io].rPls += tmpGammasPls[io][l]; uMu[io].rMin += tmpGammasMin[io][l]; } else if (tmpV[l] < 0) { uMu[io].rPls += tmpGammasMin[io][l]; uMu[io].rMin += tmpGammasPls[io][l]; } } if (uMu[io].rPls >= uMu[io].rMin) { tmpU[io] = +1; } else { tmpU[io] = -1; tmpVal = uMu[io].rPls; uMu[io].rPls = uMu[io].rMin; uMu[io].rMin = tmpVal; } } tmpEnergy = AbstainableLearner::getEnergy(uMu, tmpAlpha, tmpU); if (_verbose > 2) cout << " --> energy U = " << tmpEnergy << "\talpha = " << tmpAlpha << endl << flush; if (tmpEnergy >= previousEnergy) { tmpU = previousTmpU; break; } if ( previousEnergy < bestEnergy && previousTmpAlpha > 0 ) { _alpha = previousTmpAlpha; _v = tmpV; _u = tmpU; _selectedColumn = j; bestEnergy = previousEnergy; } } _id = _pTrainingData->getAttributeNameMap().getNameFromIdx(_selectedColumn); return bestEnergy; }