⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelfunction.h

📁 Amis - A maximum entropy estimator 一个最大熵模型统计工具
💻 H
字号:
#ifndef Amis_ModelFunction_h_#define Amis_ModelFunction_h_#include <amis/configure.h>#include <amis/Real.h>#include <amis/FeatureList.h>#include <amis/ErrorBase.h>AMIS_NAMESPACE_BEGINclass UnknownModelFunctionError : public ErrorBase {public:  UnknownModelFunctionError( const std::string& s ) : ErrorBase( s ) {}  /// Initialize with an error message  UnknownModelFunctionError( char* s ) : ErrorBase( s ) {}  /// Initialize with an error message};template < class Model, class Event >class ModelFunction {public:  typedef typename Event::FeatureType Feature;  typedef FeatureList< Feature > FeatureList;public:#ifdef AMIS_FEATURE_WEIGHT_LAMBDA  Real exponent( const Model& model, const FeatureList& fl ) const {    Real sum = 0.0;    for ( typename FeatureList::const_iterator feature = fl.begin(); feature != fl.end(); ++feature ) {      sum += model.getLambda( feature->id() ) * feature->freq();    }    if( !finite( sum ) && sum > 0.0 ) {      sum = Model::MAX_EXPONENT;    }    return sum;  }  /// Product of feature weights  Real product( const Model& model, const FeatureList& fl ) const {    return exp( exponent( model, fl ) );  }  /// Product of feature weights#else // AMIS_FEATURE_WEIGHT_LAMBDA  Real exponent( const Model& model, const FeatureList& fl ) const {    return log( product( model, fl ) );  }  Real product( const Model& model, const FeatureList& fl ) const {    Real prod = 1.0;    for ( typename FeatureList::const_iterator feature = fl.begin(); feature != fl.end(); ++feature ) {      prod *= Model::power( model.getAlpha( feature->id() ), feature->freq() );    }    if ( ! finite( prod ) ) {      prod = exp( Model::MAX_EXPONENT );    }    return prod;  }#endif // AMIS_FEATURE_WEIGHT_LAMBDA  //////////////////////////////////////////////////////////////////////public:  Real setProducts( const Model& model, const Event& event, Real* p ) const {    Real* prod = p;    Real sum = 0.0;#ifdef AMIS_FEATURE_WEIGHT_LAMBDA    for ( typename Event::const_iterator fl = event.begin(); fl != event.end(); ++prod, ++fl ) {      *prod = exponent( model, *fl );    }    Real max_weight = *( std::max_element( p, p + event.numFeatureList() ) );    // scaling    prod = p;    for( typename Event::const_iterator fl = event.begin(); fl != event.end(); ++prod, ++fl ) {      *prod = exp( *prod - max_weight );      sum += *prod;    }#else // AMIS_FEATURE_WEIGHT_LAMBDA    for ( typename Event::const_iterator fl = event.begin(); fl != event.end(); ++prod, ++fl ) {      *prod = product( model, *fl );      sum += *prod;    }#endif // AMIS_FEATURE_WEIGHT_LAMBDA    if ( ! finite( sum ) ) {      OStringStream oss;      oss << "IN: " << __PRETTY_FUNCTION__ << "sum is not finite. this should not occur";      throw IllegalEventError( oss.str() );    }    return sum;  }  Real probability( const Model& model, const Event& event ) const {    Real prod[ event.numFeatureList() ];    Real sum = setProducts( model, event, prod );    Real sum_active = 0.0;    for ( typename Event::const_iterator fl = event.begin(); fl != event.end(); ++prod, ++fl ) {      if ( event.isActive( fl ) ) {	sum_active += *prod;      }    }    return sum_active / sum;  }  ////////////////////////////////////////////////////////////public:  template < class ModelExpect >  void setModelExpectation( const Model& model, const FeatureList& fl, Real prob,                            ModelExpect& model_expectation ) const {    for ( typename FeatureList::const_iterator feature = fl.begin();          feature != fl.end();          ++feature ) {      //model_expectation.add( feature->id(), fl.featureCount(), feature->freq(), prob );      model_expectation[ feature->id() ].add( fl.featureCount(), feature->freq() * prob );    }  }  template < class ModelExpect >  Real setModelExpectation( const Model& model, const Event& event,                            ModelExpect& model_expectation ) const {    int n = event.size();    Real prod[ n ];    Real sum = setProducts( model, event, prod );    //if ( ! finite( sum ) ) std::cerr << "Not finite!! " << event_id << std::endl;    if ( sum == 0.0 ) return 1.0;    Real inv_sum = 1.0 / sum;    Real denom = event.eventFrequency() * inv_sum;    //if ( EnableJointProb ) denom *= event.eventProbability();    for ( int i = 0; i < n; ++i ) {      setModelExpectation( model, event[ i ], prod[ i ] * denom, model_expectation );    }    return prod[ event.observedEventID() ] * inv_sum * event.eventProbability();  }  template < class EmpiricalExpect >  void setEmpiricalExpectation( const Model& model, const FeatureList& fl, Real freq,                                EmpiricalExpect& empirical_expectation ) const {    AMIS_DEBUG_MESSAGE( 5, "Feature loop\n" );    for ( typename FeatureList::const_iterator feature = fl.begin();	  feature != fl.end();	  ++feature ) {      AMIS_DEBUG_MESSAGE( 5, "feature=" << feature->id() << '\n' );      empirical_expectation.add( feature->id(), feature->freq(), freq );    }  }  template < class EmpiricalExpect >  void setEmpiricalExpectation( const Model& model, const Event& event, EmpiricalExpect& empirical_expectation ) const {    Real freq = event.eventFrequency();    //if ( EnableJointProb ) freq *= eventProbability();    setEmpiricalExpectation( model, event.observedEvent(), freq, empirical_expectation );  }  /*  void setEmpiricalExpectVariance( const ModelBase& model,				   std::vector< Real >& emp_cnt,				   std::vector< Real >& val_sums,				   std::vector< Real >& vval_sums ) const {    for ( typename Event::const_iterator fl = begin(); fl != end(); ++fl ) {      for ( typename FeatureList< Feature >::const_iterator feature = fl->begin(); feature != fl->end(); ++feature ) {	Real c = static_cast< Real >( feature->freq() );	if ( isObserved( fl ) ) emp_cnt[ feature->id() ] += frequency();	val_sums[ feature->id() ] += c;	vval_sums[ feature->id() ] += ( c * c );      }    }  }  */};AMIS_NAMESPACE_END#endif // Amis_ModelFunction_h_// end of ModelFunction.h

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -