📄 gismap.h
字号:
//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao/// You may distribute under the terms of the Artistic License.////// <id>$Id: GISMAP.h,v 1.2 2003/05/11 18:12:09 yusuke Exp $</id>/// <collection>Maximum Entropy Estimator</collection>/// <name>GISMAP.h</name>/// <overview>Generalized Iterative Scaling with MAP estimation</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_GISMAP_h_#define Amis_GISMAP_h_#include <amis/configure.h>#include <amis/IS.h>#include <amis/ModelExpect.h>#include <amis/FeatureFreqArray.h>#include <amis/EmpiricalExpect.h>AMIS_NAMESPACE_BEGIN///////////////////////////////////////////////////////////////////////// <classdef>/// <name>GISMAP</name>/// <overview>Generalized Iterative Scaling with MAP estimation</overview>/// <desc>/// A maximum entropy estimator based on the Generalized Iterative Scaling/// algorithm with MAP estimation with a Gaussian prior./// </desc>/// <body>template < class Model, class EventSpace, class ModelExpect, = ModelExpect< FeatureFreqValue, Model, EventSpace >, class EmpiricalExpect = EmpiricalExpect< Model, EventSpace > >class GISMAPBase : public IS< Model, EventSpace, ModelExpect, EmpiricalExpect > {protected: int max_newton_iterations; Real max_count; Real default_sigma; std::vector<Real> sigmas; //////////////////////////////////////////////////////////////////////public: GISMAPBase( int max, Real sigma ) { max_newton_iterations = max; default_sigma = sigma; } GISMAPBase( Model& init_model, EventSpace& init_event, int max, Real sigma ) : IS< Model, EventSpace, ModelExpect, EmpiricalExpect >( init_model, init_event ) { max_newton_iterations = max; default_sigma = sigma; } virtual ~GISMAPBase() {} void setSigma( Real s ) { default_sigma = s; } Real getSigma() const { return default_sigma; } //////////////////////////////////////////////////////////////////////public: void initialize() { AMIS_DEBUG_MESSAGE( 3, "Set internal data...\n" ); max_count = static_cast< Real >( event_space->maxFeatureCount() ); sigmas.resize( model->numFeatures() ); fill( sigmas.begin(), sigmas.end(), default_sigma ); IS< Model, EventSpace, ModelExpect, EmpiricalExpect >::initialize(); } /// Initialize the estimator Real solveEquation( int i ) { setObjectiveFunctionValue( 0.0 ); // dummy value. please set correct value return solveNewton( (*model)[ i ], model_expectation[ i ], empirical_expectation [ i ], sigmas[ i ] , max_count, max_newton_iterations, machineEpsilon()); } Real solveNewton( Real lambda, Real ex, Real em, Real s, Real c, int max_iter, Real epsilon ) const { std::cerr << __PRETTY_FUNCTION << " obsolete " << std::endl; exit(1); /// still contains bug Real lower_bound = 0.0; Real upper_bound = 100.0; // need modification Real current_x = upper_bound; Real next_x; Real d, g, gg; Real inv_s = 1.0/(s*s); d = pow(current_x, c ) * ex; g = d - em + ( lambda + ModelBase::safe_log(current_x ) ) * inv_s; if( !finite( g ) ) { return 1.0; } for( int i = 0; i < max_iter; i++ ) { d = pow( current_x, c ) * ex; g = d - em + ( lambda + ModelBase::safe_log(current_x) ) * inv_s; if( fabs( g ) <= epsilon ) { break; } gg = c*d + inv_s; next_x = current_x - g/gg; if( !finite( next_x ) ) { break; } else{ current_x = next_x; } if( current_x < lower_bound || upper_bound < current_x ) { current_x = ( lower_bound + upper_bound ) * 0.5; } if( g < 0.0 ) { lower_bound = current_x; } else { upper_bound = current_x; } if( fabs( upper_bound - lower_bound ) <= epsilon ) { break; } } return current_x; } /* Real solveNewton( Real al, Real ex, Real em, Real s, Real c, int max_iter, Real epsilon ) const { /// still contains bug Real current_x = 1.0; Real next_x; Real d, g, gg; Real lambda = Model::safe_log( al ); Real inv_s = 1.0/(s*s); d = pow(current_x, c ) * ex; g = d - em + ( lambda + Model::safe_log(current_x ) ) * inv_s; if( !finite( g ) ) { return 1.0; } for( int i = 0; i < max_iter; i++ ) { d = pow( current_x, c ) * ex; g = d - em + ( lambda + Model::safe_log(current_x) ) * inv_s; if( fabs( g ) <= epsilon ) { break; } gg = c*d + inv_s; next_x = current_x - g/gg; if( !finite( next_x ) ) { break; } else{ current_x = next_x; } } return current_x; } */ /// Solve the equation};/// </body>/// </classdef>AMIS_NAMESPACE_END#endif // Amis_GISMAP_h_// end of GISMAP.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -