📄 modelfunctionfix.h
字号:
#ifndef Amis_ModelFunctionFix_h_#define Amis_ModelFunctionFix_h_#include <amis/configure.h>#include <amis/ModelFunction.h>#include <amis/ModelFix.h>#include <amis/EventFix.h>AMIS_NAMESPACE_BEGINtemplate < class Feature >class ModelFunction< ModelFixBase, EventFix< Feature > > {public: typedef typename Feature::FeatureFreq FeatureFreq;public: Real setProducts( const ModelFixBase& model, const EventFix< Feature >& event, Real* p ) const { Real* prod = p; Real sum = 0.0; const FeatureList<Feature>& hist_list = event.getHistoryList();#ifdef AMIS_FEATURE_WEIGHT_LAMBDA for(int i = 0; i < event.numTargets(); i++){ prod[i] = 0.0; } for( typename FeatureList<Feature>::const_iterator itr = hist_list.begin(); itr != hist_list.end(); ++itr ) { FeatureID hid = itr->id(); typename Feature::FeatureFreq freq = itr->freq(); const std::vector< std::pair<FeatureID, FeatureID> >& fv = model.getFeatures( hid ); for(size_t k = 0; k < fv.size(); k++){ FeatureID tid = fv[k].first; FeatureID fid = fv[k].second; prod[tid] += model.getLambda( fid ) * freq; } } for(int i = 0; i < event.numTargets(); i++){ if( !finite( prod[i] ) && prod[i] > 0.0 ) { prod[i] = ModelBase::MAX_EXPONENT; } } Real max_weight = *( std::max_element( prod, prod + event.numTargets() ) ); for(int i = 0; i < event.numTargets(); i++){ prod[ i ] = exp( prod[ i ] - max_weight ); sum += prod[ i ]; }#else // AMIS_FEATURE_WEIGHT_LAMBDA for(int i = 0; i < event.numTargets(); i++){ prod[i] = 1.0; } for ( typename FeatureList< Feature >::const_iterator itr = hist_list.begin(); itr != hist_list.end(); ++itr ) { FeatureID hid = itr->id(); typename Feature::FeatureFreq freq = itr->freq(); const std::vector< std::pair< FeatureID, FeatureID > >& fv = model.getFeatures( hid ); for ( size_t k = 0; k < fv.size(); ++k ) { FeatureID tid = fv[ k ].first; FeatureID fid = fv[ k ].second; prod[ tid ] *= ModelBase::power( model.getAlpha( fid ), freq ); } } for ( int i = 0; i < event.numTargets(); ++i ) { sum += prod[ i ]; }#endif // AMIS_FEATURE_WEIGHT_LAMBDA if( !finite(sum) ){ AMIS_PROF_MESSAGE( "IN: " << __PRETTY_FUNCTION__ << ", sum is not finite. this should not occur\n" ); } return sum; } Real probability( const ModelFixBase& model, const EventFix< Feature >& event ) const { Real prod[ event.numTargets() ]; Real sum = setProducts( model, event, prod ); Real sum_active = 0.0; return prod[ event.observedEventID() ] / sum; } template < class ModelExpect > Real setModelExpectation( const ModelFixBase& model, const EventFix< Feature >& event, ModelExpect& model_expectation ) const { int n = event.numTargets(); Real prod[ n ]; Real sum = setProducts( model, event, prod ); if ( sum == 0.0 ) return 1.0; Real inv_sum = 1.0 / sum; Real denom = event.eventFrequency() * inv_sum; Real likelihood = prod[ event.observedEventID() ] * inv_sum * event.eventProbability(); for(int i = 0; i < n; i++) { prod[ i ] *= denom; } const FeatureList<Feature>& hist_list = event.getHistoryList(); for( typename FeatureList<Feature>::const_iterator itr = hist_list.begin(); itr != hist_list.end(); ++itr){ FeatureID hid = itr->id(); FeatureFreq freq = itr->freq(); const std::vector< std::pair<FeatureID, FeatureID> >& fv = model.getFeatures( hid ); for(size_t k = 0; k < fv.size(); k++){ FeatureID tid = fv[k].first; FeatureID fid = fv[k].second; model_expectation[fid].add( event.featureCount(tid), prod[tid] * freq ); } } return likelihood; } template < class EmpiricalExpect > void setEmpiricalExpectation( const ModelFixBase& model, const EventFix< Feature >& event, EmpiricalExpect& empirical_expectation ) const { Real freq = event.eventFrequency(); //if ( EnableJointProb ) freq *= eventProbability(); const FeatureList< Feature >& history_list = event.getHistoryList(); for ( typename FeatureList< Feature >::const_iterator feature = history_list.begin(); feature != history_list.end(); ++feature ) { FeatureID fid = model.getFeatures( feature->id() )[ event.observedEventID() ].second; AMIS_DEBUG_MESSAGE( 5, "feature=" << fid << std::endl ); empirical_expectation[ fid ] += static_cast< Real >( feature->freq() * freq ); } }};AMIS_NAMESPACE_END#endif // Amis_ModelFunctionFix_h_// end of ModelFunctionFix.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -