📄 bfgsmap.h
字号:
//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao/// You may distribute under the terms of the Artistic License.////// <id>$Id: BFGSMAP.h,v 1.10 2003/05/13 08:55:55 yusuke Exp $</id>/// <collection>Maximum Entropy Estimator</collection>/// <name>BFGSMAP.h</name>/// <overview>Maximum entropy model estimator by BFGS method with/// MAP estimation</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_BFGSMAP_h_#define Amis_BFGSMAP_h_#include <amis/configure.h>#include <amis/ModelBase.h>#include <amis/GaussianPrior.h>#include <amis/BFGS.h>#include <amis/StringStream.h>AMIS_NAMESPACE_BEGIN///////////////////////////////////////////////////////////////////////// <classdef>/// <name>BFGSMAPBase</name>/// <overview>Base class for ME estimator by limited-memory BFGS method</overview>/// <desc>/// A maximum entropy estimator based on the limited-memory BFGS method./// </desc>/// <body>template < class Model, class EventSpace, class ModelExpect = ModelExpect< FeatureFreqValue, Model, EventSpace >, class EmpiricalExpect = EmpiricalExpect< Model, EventSpace > >class BFGSMAPBase : public BFGSBase< Model, EventSpace, ModelExpect, EmpiricalExpect > {protected: GaussianPriorPtr gaussian_prior;public: explicit BFGSMAPBase( int m = DEFAULT_MEMORY_SIZE ) : BFGSBase< Model, EventSpace, ModelExpect, EmpiricalExpect >( m ) { } explicit BFGSMAPBase( GaussianPriorPtr p, int m = DEFAULT_MEMORY_SIZE ) : BFGSBase< Model, EventSpace, ModelExpect, EmpiricalExpect >( m ), gaussian_prior( p ) { } /// Constructor BFGSMAPBase( Model& init_model, EventSpace& init_event, GaussianPriorPtr p, int m = DEFAULT_MEMORY_SIZE ) : BFGSBase< Model, EventSpace, ModelExpect, EmpiricalExpect >( init_model, init_event, m ), gaussian_prior( p ) { } /// Constructor virtual ~BFGSMAPBase() {} /// Destructor //////////////////////////////////////////////////////////////////////public: virtual void setGaussianPrior( GaussianPriorPtr p ) { gaussian_prior = p; } virtual const GaussianPrior& getGaussianPrior() const { return *gaussian_prior; }public: void initialize() { BFGSBase< Model, EventSpace, ModelExpect, EmpiricalExpect >::initialize(); gaussian_prior->initialize( empirical_expectation ); } /// Initialize the gaussian prior Real computeFunctionDerivative( std::valarray< Real >& grad ) { AMIS_DEBUG_MESSAGE( 3, "begin BFGSMAP::computeFunctionDerivative" ); Real ret = BFGSBase< Model, EventSpace, ModelExpect, EmpiricalExpect >::computeFunctionDerivative( grad ); for ( int i = 0; i < model->numFeatures(); ++i ) { Real lambda = model->getLambda( i ); ret += lambda * lambda * ( 0.5 * (*gaussian_prior)[ i ] ); grad[ i ] += lambda * (*gaussian_prior)[ i ]; } setObjectiveFunctionValue( -ret ); // likelihood != objective function AMIS_DEBUG_MESSAGE( 3, "end BFGSMAP::computeFunctionDerivative" ); return ret; } /// Compute the function value and the derivative - accessed by BFGSSolver};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>BFGSMAP</name>/// <overview>ME estimator by limited-memory BFGS method</overview>/// <desc>/// A maximum entropy estimator based on the limited-memory BFGS method./// </desc>/// <body>template < class Feature >class BFGSMAP : public BFGSMAPBase< ModelBase, EventSpaceInst< Feature > > {public: explicit BFGSMAP( int m = DEFAULT_MEMORY_SIZE ) : BFGSMAPBase< ModelBase, EventSpaceInst< Feature > >( m ) {} /// Constructor explicit BFGSMAP( GaussianPrior& p, int m = DEFAULT_MEMORY_SIZE ) : BFGSMAPBase< ModelBase, EventSpaceInst< Feature > >( p, m ) {} /// Constructor BFGSMAP( ModelBase& init_model, EventSpaceInst< Feature >& init_event, GaussianPrior& p, int m = DEFAULT_MEMORY_SIZE ) : BFGSMAPBase< ModelBase, EventSpaceInst< Feature > >( init_model, init_event, p, m ) {} /// Constructor virtual ~BFGSMAP() {} /// Destructor const std::string estimatorName() const { return "BFGSMAP<" + Feature::featureTypeName() + ">(" + gaussian_prior->getName() + ")"; } /// Get the name of this class};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>BFGSMAPJoint</name>/// <overview>ME estimator by limited-memory BFGS method</overview>/// <desc>/// A maximum entropy estimator based on the limited-memory BFGS method/// for joint probability models/// </desc>/// <body>template < class Feature >class BFGSMAPJoint : public BFGSMAPBase< ModelBase, EventSpaceInst< Feature >, ModelExpect< FeatureFreqValue, ModelBase, EventSpaceInst< Feature >, true >, EmpiricalExpect< ModelBase, EventSpaceInst< Feature >, true > > {public: explicit BFGSMAPJoint( int m = DEFAULT_MEMORY_SIZE ) : BFGSMAPBase< ModelBase, EventSpaceInst< Feature >, ModelExpect< FeatureFreqValue, ModelBase, EventSpaceInst< Feature >, true >, EmpiricalExpect< ModelBase, EventSpaceInst< Feature >, true > >( m ) {} /// Constructor explicit BFGSMAPJoint( GaussianPriorPtr p, int m = DEFAULT_MEMORY_SIZE ) : BFGSMAPBase< ModelBase, EventSpaceInst< Feature >, ModelExpect< FeatureFreqValue, ModelBase, EventSpaceInst< Feature >, true >, EmpiricalExpect< ModelBase, EventSpaceInst< Feature >, true > >( p, m ) {} /// Constructor BFGSMAPJoint( ModelBase& init_model, EventSpaceInst< Feature >& init_event, GaussianPriorPtr p, int m = DEFAULT_MEMORY_SIZE ) : BFGSMAPBase< ModelBase, EventSpaceInst< Feature >, ModelExpect< FeatureFreqValue, ModelBase, EventSpaceInst< Feature >, true >, EmpiricalExpect< ModelBase, EventSpaceInst< Feature >, true > >( init_model, init_event, p, m ) {} /// Constructor virtual ~BFGSMAPJoint() {} /// Destructor const std::string estimatorName() const { return "BFGSMAPJoint<" + Feature::featureTypeName() + ">(" + gaussian_prior->getName() + ")"; } /// Get the name of this class};/// </body>/// </classdef>AMIS_NAMESPACE_END#endif // BFGSMAP_h_// end of BFGSMAP.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -