Simulated Annealing for ANN_MLP training method (#10213)

* Simulated Annealing for ANN_MLP training method

* EXPECT_LT

* just to test new data

* manage RNG

* Try again

* Just run buildbot with new data

* try to understand

* Test layer

* New data- new test

* Force RNG in backprop

* Use Impl to avoid virtual method

* reset all weights

* try to solve ABI

* retry

* ABI solved?

* till problem with dynamic_cast

* Something is wrong

* Solved?

* disable backprop test

* remove ANN_MLP_ANNEALImpl

* Disable weight in varmap

* Add example for SimulatedAnnealing
This commit is contained in:
LaurentBerger
2017-12-15 11:57:39 +01:00
committed by Vadim Pisarevsky
parent 6df8ac0342
commit 7ad308ea47
5 changed files with 685 additions and 26 deletions
+137 -3
View File
@@ -1406,13 +1406,14 @@ public:
/** Available training methods */
enum TrainingMethods {
BACKPROP=0, //!< The back-propagation algorithm.
RPROP=1 //!< The RPROP algorithm. See @cite RPROP93 for details.
RPROP = 1, //!< The RPROP algorithm. See @cite RPROP93 for details.
ANNEAL = 2 //!< The simulated annealing algorithm. See @cite Kirkpatrick83 for details.
};
/** Sets training method and common parameters.
@param method Default value is ANN_MLP::RPROP. See ANN_MLP::TrainingMethods.
@param param1 passed to setRpropDW0 for ANN_MLP::RPROP and to setBackpropWeightScale for ANN_MLP::BACKPROP
@param param2 passed to setRpropDWMin for ANN_MLP::RPROP and to setBackpropMomentumScale for ANN_MLP::BACKPROP.
@param param1 passed to setRpropDW0 for ANN_MLP::RPROP and to setBackpropWeightScale for ANN_MLP::BACKPROP and to initialT for ANN_MLP::ANNEAL.
@param param2 passed to setRpropDWMin for ANN_MLP::RPROP and to setBackpropMomentumScale for ANN_MLP::BACKPROP and to finalT for ANN_MLP::ANNEAL.
*/
CV_WRAP virtual void setTrainMethod(int method, double param1 = 0, double param2 = 0) = 0;
@@ -1499,6 +1500,34 @@ public:
/** @copybrief getRpropDWMax @see getRpropDWMax */
CV_WRAP virtual void setRpropDWMax(double val) = 0;
/** ANNEAL: Update initial temperature.
It must be \>=0. Default value is 10.*/
/** @see setAnnealInitialT */
CV_WRAP double getAnnealInitialT() const;
/** @copybrief getAnnealInitialT @see getAnnealInitialT */
CV_WRAP void setAnnealInitialT(double val);
/** ANNEAL: Update final temperature.
It must be \>=0 and less than initialT. Default value is 0.1.*/
/** @see setAnnealFinalT */
CV_WRAP double getAnnealFinalT() const;
/** @copybrief getAnnealFinalT @see getAnnealFinalT */
CV_WRAP void setAnnealFinalT(double val);
/** ANNEAL: Update cooling ratio.
It must be \>0 and less than 1. Default value is 0.95.*/
/** @see setAnnealCoolingRatio */
CV_WRAP double getAnnealCoolingRatio() const;
/** @copybrief getAnnealCoolingRatio @see getAnnealCoolingRatio */
CV_WRAP void setAnnealCoolingRatio(double val);
/** ANNEAL: Update iteration per step.
It must be \>0 . Default value is 10.*/
/** @see setAnnealItePerStep */
CV_WRAP int getAnnealItePerStep() const;
/** @copybrief getAnnealItePerStep @see getAnnealItePerStep */
CV_WRAP void setAnnealItePerStep(int val);
/** possible activation functions */
enum ActivationFunctions {
/** Identity function: \f$f(x)=x\f$ */
@@ -1838,6 +1867,111 @@ CV_EXPORTS void randMVNormal( InputArray mean, InputArray cov, int nsamples, Out
CV_EXPORTS void createConcentricSpheresTestSet( int nsamples, int nfeatures, int nclasses,
OutputArray samples, OutputArray responses);
/** @brief Artificial Neural Networks - Multi-Layer Perceptrons.
@sa @ref ml_intro_ann
*/
class CV_EXPORTS_W ANN_MLP_ANNEAL : public ANN_MLP
{
public:
/** @see setAnnealInitialT */
CV_WRAP virtual double getAnnealInitialT() const;
/** @copybrief getAnnealInitialT @see getAnnealInitialT */
CV_WRAP virtual void setAnnealInitialT(double val);
/** ANNEAL: Update final temperature.
It must be \>=0 and less than initialT. Default value is 0.1.*/
/** @see setAnnealFinalT */
CV_WRAP virtual double getAnnealFinalT() const;
/** @copybrief getAnnealFinalT @see getAnnealFinalT */
CV_WRAP virtual void setAnnealFinalT(double val);
/** ANNEAL: Update cooling ratio.
It must be \>0 and less than 1. Default value is 0.95.*/
/** @see setAnnealCoolingRatio */
CV_WRAP virtual double getAnnealCoolingRatio() const;
/** @copybrief getAnnealCoolingRatio @see getAnnealCoolingRatio */
CV_WRAP virtual void setAnnealCoolingRatio(double val);
/** ANNEAL: Update iteration per step.
It must be \>0 . Default value is 10.*/
/** @see setAnnealItePerStep */
CV_WRAP virtual int getAnnealItePerStep() const;
/** @copybrief getAnnealItePerStep @see getAnnealItePerStep */
CV_WRAP virtual void setAnnealItePerStep(int val);
/** @brief Creates empty model
Use StatModel::train to train the model, Algorithm::load\<ANN_MLP\>(filename) to load the pre-trained model.
Note that the train method has optional flags: ANN_MLP::TrainFlags.
*/
// CV_WRAP static Ptr<ANN_MLP> create();
};
/****************************************************************************************\
* Simulated annealing solver *
\****************************************************************************************/
/** @brief The class implements simulated annealing for optimization.
@cite Kirkpatrick83 for details
*/
class CV_EXPORTS SimulatedAnnealingSolver : public Algorithm
{
public:
SimulatedAnnealingSolver() { init(); };
~SimulatedAnnealingSolver();
/** Give energy value for a state of system.*/
virtual double energy() =0;
/** Function which change the state of system (random pertubation).*/
virtual void changedState() = 0;
/** Function to reverse to the previous state.*/
virtual void reverseChangedState() = 0;
/** Simulated annealing procedure. */
int run();
/** Set intial temperature of simulated annealing procedure.
*@param x new initial temperature. x\>0
*/
void setInitialTemperature(double x);
/** Set final temperature of simulated annealing procedure.
*@param x new final temperature value. 0\<x\<initial temperature
*/
void setFinalTemperature(double x);
double getFinalTemperature();
/** Set setCoolingRatio of simulated annealing procedure : T(t) = coolingRatio * T(t-1).
* @param x new cooling ratio value. 0\<x\<1
*/
void setCoolingRatio(double x);
/** Set number iteration per temperature step.
* @param ite number of iteration per temperature step ite \> 0
*/
void setIterPerStep(int ite);
struct Impl;
protected :
void init();
Impl* impl;
};
struct SimulatedAnnealingSolver::Impl
{
RNG rEnergy;
double coolingRatio;
double initialT;
double finalT;
int iterPerStep;
Impl()
{
initialT = 2;
finalT = 0.1;
coolingRatio = 0.95;
iterPerStep = 100;
refcount = 1;
}
int refcount;
~Impl() { refcount--;CV_Assert(refcount==0); }
};
//! @} ml
}