📄 iis.h
字号:
//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao/// You may distribute under the terms of the Artistic License.////// <id>$Id: IIS.h,v 1.7 2003/05/14 06:01:53 yusuke Exp $</id>/// <collection>Maximum Entropy Estimator</collection>/// <name>IIS.h</name>/// <overview>Optimized implementation of Improved Iterative Scaling</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_IIS_h_#define Amis_IIS_h_#include <amis/configure.h>#include <amis/IS.h>#include <amis/EventSpaceInst.h>#include <amis/ModelExpect.h>#include <amis/FeatureFreqArray.h>#include <amis/NewtonSolver.h>#include <amis/ErrorBase.h>#include <utility>AMIS_NAMESPACE_BEGIN///////////////////////////////////////////////////////////////////////// <classdef>/// <name>IISError</name>/// <overview>Error in the IIS method</overview>/// <desc>/// The error is thrown when an error occured in the IIS method/// </desc>/// <body>class IISError : public ErrorBase { public: IISError( const std::string& s ) : ErrorBase( s ) {} /// Initialize with an error message IISError( char* s ) : ErrorBase( s ) {} /// Initialize with an error message};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>IISBase</name>/// <overview>Optimized implementation of Improved Iterative Scaling</overview>/// <desc>/// A maximum entropy estimator based on the Improved Iterative Scaling/// algorithm./// Calculate the solution update_i to/// \sum_{x,y}( \~p(x)p(y|x)f_i(x,y) update_i^{f#} ) - \~p(f_i) --(1)/// then/// alpha_i *= update_i/// Solution to (1) is computed by Newton's method./// Calculation of Summation is optimized by means of collecting the terms/// with the same f#./// </desc>/// <body>template < class Model, class EventSpace, class FeatureFreqArray, class ModelExpect = ModelExpect< FeatureFreqArray, Model, EventSpace >, class EmpiricalExpect = EmpiricalExpect< Model, EventSpace > >class IISBase : public IS< Model, EventSpace, ModelExpect, EmpiricalExpect > {public: static const int DEFAULT_MAX_NEWTON_ITERATIONS = 200;protected: NewtonSolver< FeatureFreqArray > solver; int max_newton_iterations; //////////////////////////////////////////////////////////////////////public: explicit IISBase( int max = DEFAULT_MAX_NEWTON_ITERATIONS ) : IS< Model, EventSpace, ModelExpect, EmpiricalExpect >() { max_newton_iterations = max; } IISBase( Model& init_model, EventSpace& init_event, int max = DEFAULT_MAX_NEWTON_ITERATIONS ) : IS< Model, EventSpace, ModelExpect, EmpiricalExpect >( init_model, init_event ) { max_newton_iterations = max; } virtual ~IISBase() {} //////////////////////////////////////////////////////////////////////protected: void initialize() { AMIS_DEBUG_MESSAGE( 3, "Set internal data...\n" ); IS< Model, EventSpace, ModelExpect, EmpiricalExpect >::initialize(); }public: void setMaxNewtonIterations( int n ) { max_newton_iterations = n; } int getMaxNewtonIterations() { return max_newton_iterations; }public: Real solveEquation( int i ) { // Please reimplement solveEquation //return solver.solveEquation( model_expectation[ i ], empirical_expectation[ i ], machineEpsilon(), max_newton_iterations ); return ModelBase::safe_log( solver.solveEquation( model_expectation[ i ], empirical_expectation[ i ], machineEpsilon(), max_newton_iterations ) ); } /// Solve the equation};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>IIS</name>/// <overview>Improved Iterative Scaling</overview>/// <desc>/// A maximum entropy estimator based on the Improved Iterative Scaling/// algorithm./// </desc>/// <body>template < class Feature, class FeatureFreqArray >class IIS : public IISBase< ModelBase, EventSpaceInst< Feature >, FeatureFreqArray > {public: IIS( int max = DEFAULT_MAX_NEWTON_ITERATIONS ) : IISBase< ModelBase, EventSpaceInst< Feature >, FeatureFreqArray >( max ) {} IIS( ModelBase& init_model, EventSpaceInst< Feature >& init_event, int max = DEFAULT_MAX_NEWTON_ITERATIONS ) : IISBase< ModelBase, EventSpaceInst< Feature >, FeatureFreqArray >( init_model, init_event, max ) { } virtual ~IIS() {} const std::string estimatorName() const { return "IIS<" + Feature::featureTypeName() + "," + FeatureFreqArray::featureFreqArrayName() + ">"; } /// Get the name of this class};/// </body>/// </classdef>template < class Feature >class IISHash : public IIS< Feature, FeatureFreqMap< Feature > > {public: IISHash( int max = DEFAULT_MAX_NEWTON_ITERATIONS ) : IIS< Feature, FeatureFreqMap< Feature > >( max ) {} IISHash( ModelBase& m, EventSpaceInst< Feature >& e, int max = DEFAULT_MAX_NEWTON_ITERATIONS ) : IIS< Feature, FeatureFreqMap< Feature > >( m, e, max ) {} virtual ~IISHash() {}};template < class Feature >class IISVector : public IIS< Feature, FeatureFreqVector< Feature > > {public: IISVector( int max = DEFAULT_MAX_NEWTON_ITERATIONS ) : IIS< Feature, FeatureFreqVector< Feature > >( max ) {} IISVector( ModelBase& m, EventSpaceInst< Feature >& e, int max = DEFAULT_MAX_NEWTON_ITERATIONS ) : IIS< Feature, FeatureFreqVector< Feature > >( m, e, max ) {} virtual ~IISVector() {}};template <>class IISVector< RealFeature > : public IIS< RealFeature, FeatureFreqMap< RealFeature > > {public: IISVector( int max = DEFAULT_MAX_NEWTON_ITERATIONS ) : IIS< RealFeature, FeatureFreqMap< RealFeature > >( max ) {} IISVector( ModelBase& m, EventSpaceInst< RealFeature >& e, int max = DEFAULT_MAX_NEWTON_ITERATIONS ) : IIS< RealFeature, FeatureFreqMap< RealFeature > >( m, e, max ) {} virtual ~IISVector() {}};AMIS_NAMESPACE_END#endif // IIS_h_// end of IIS.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -