📄 modelexpect.h
字号:
//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao/// You may distribute under the terms of the Artistic License.////// <id>$Id: ModelExpect.h,v 1.10 2003/05/16 13:03:46 yusuke Exp $</id>/// <collection>Maximum Entropy Estimator</collection>/// <name>ModelExpect.h</name>/// <overview>Model expectations used by IIS</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_ModelExpect_h_#define Amis_ModelExpect_h_#include <amis/configure.h>#include <amis/EventSpace.h>#include <amis/ModelFunction.h>#include <amis/Thread.h>#include <iostream>#include <vector>AMIS_NAMESPACE_BEGIN//////////////////////////////////////////////////////////////////////template < class FeatureFreqArray >class ModelExpectBase : public std::vector< FeatureFreqArray > {public: void debugInfo( std::ostream& ostr ) const { for ( typename std::vector< FeatureFreqArray >::const_iterator it = this->begin(); it != this->end(); ++it ) { it->debugInfo( ostr ); ostr << '\n'; } } /// Print data};template < class FeatureFreqArray >std::ostream& operator<<( std::ostream& os, const ModelExpectBase< FeatureFreqArray >& e ) { e.debugInfo( os ); return os;}//////////////////////////////////////////////////////////////////////template < class FeatureFreqArray, class Model, class EventSpace, bool EnableJointProb = false >class ModelExpect : public ModelExpectBase< FeatureFreqArray > {public: typedef typename EventSpace::FeatureType Feature; typedef typename Feature::FeatureFreq FeatureFreq; typedef typename EventSpace::EventType Event;private: const Model* model; const EventSpace* event_space; int current_event_id;#ifdef AMIS_PARALLEL typedef Thread< ModelExpect< FeatureFreqArray, Model, EventSpace, EnableJointProb >, int, Real > ThreadType; int num_threads; std::vector< std::vector< FeatureFreqArray > > local_model_expectation; MutexLock lock; bool is_report_iteration;#endif // AMIS_PARALLELpublic: ModelExpect() { model = NULL; event_space = NULL; } virtual ~ModelExpect() {}public: /* void add( FeatureID id, FeatureFreq count, FeatureFreq freq, Real prob ) { (*this)[ id ].add( count, prob * freq ); } */ void initialize( const Model& init_model, const EventSpace& init_event, int num = 1 ) { model = &init_model; event_space = &init_event; AMIS_DEBUG_MESSAGE( 3, "Set internal data...\n" ); resize( model->numFeatures() ); for ( typename std::vector< FeatureFreqArray >::iterator it = begin(); it != end(); ++it ) { it->reserve( event_space->maxFeatureCount() + 1 ); it->clear(); } current_event_id = 0;#ifdef AMIS_PARALLEL num_threads = num - 1; local_model_expectation.resize( num_threads ); for ( typename std::vector< std::vector< FeatureFreqArray > >::iterator it1 = local_model_expectation.begin(); it1 != local_model_expectation.end(); ++it1 ) { it1->resize( model->numFeatures() ); for ( typename std::vector< FeatureFreqArray >::iterator it2 = it1->begin(); it2 != it1->end(); ++it2 ) { it2->reserve( event_space->maxFeatureCount() + 1 ); it2->clear(); } }#endif // AMIS_PARALLEL } //////////////////////////////////////////////////////////////////////public: Real setModelExpectation( bool report_iteration = true ) { Real log_likelihood = 0.0; current_event_id = 0; if ( report_iteration ) { log_likelihood = setModelExpectationInternal< true >( *this ); } else { log_likelihood = setModelExpectationInternal< false >( *this ); }#ifdef AMIS_PARALLEL is_report_iteration = report_iteration; ThreadType* threads[ num_threads ]; for ( int i = 0; i < num_threads; ++i ) { threads[ i ] = new ThreadType( this ); threads[ i ]->run( i ); } for ( int i = 0; i < num_threads; ++i ) { log_likelihood += threads[ i ]->wait(); delete threads[ i ]; for ( int j = 0; j < model->numFeatures(); ++j ) { (*this)[ j ] += local_model_expectation[ i ][ j ]; } }#endif // AMIS_PARALLEL return log_likelihood; }#ifdef AMIS_PARALLEL Real run( int i ) { if ( is_report_iteration ) { return setModelExpectationInternal< true >( local_model_expectation[ i ] ); } else { return setModelExpectationInternal< false >( local_model_expectation[ i ] ); } }#endif // AMIS_PARALLELprotected: template < bool REPORT > Real setModelExpectationInternal( std::vector< FeatureFreqArray >& model_expectation ) { for ( typename std::vector< FeatureFreqArray >::iterator it = model_expectation.begin(); it != model_expectation.end(); ++it ) { it->clear(); } Real log_likelihood = 0.0; for ( ; ; ) {#ifdef AMIS_PARALLEL lock.acquire();#endif // AMIS_PARALLEL int event_id = current_event_id++;#ifdef AMIS_PARALLEL lock.release();#endif // AMIS_PARALLEL if ( event_id >= event_space->numEvents() ) break; const Event& event = (*event_space)[ event_id ]; Real prob = ModelFunction< Model, Event >().setModelExpectation( *model, event, model_expectation ); if ( REPORT ) log_likelihood += event.eventProbability() * event.frequency() * event_space->eventEmpiricalExpectation() * ModelBase::safe_log( prob ); } for ( typename std::vector< FeatureFreqArray >::iterator it = model_expectation.begin(); it != model_expectation.end(); ++it ) { (*it) *= event_space->eventEmpiricalExpectation(); } return log_likelihood; }};AMIS_NAMESPACE_END#endif // ModelExpect_h_// end of ModelExpect.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -