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

📄 eventfixspace.h

📁 Amis - A maximum entropy estimator 一个最大熵模型统计工具
💻 H
字号:
// ========================================================// Time-stamp: <2003-05-14 16:57:50 yusuke>// File:// Author:      Jun'ichi KAZAMA// Description:// License:    // ========================================================#ifndef Amis_EventFixSpace_h_#define Amis_EventFixSpace_h_#include <amis/configure.h>#include <amis/EventFix.h>#include <amis/EventSpace.h>#include <amis/ModelFunctionFix.h>#include <amis/VectorOnFile.h>AMIS_NAMESPACE_BEGINtemplate<class Feature>class EventFixSpaceBase : public EventSpace<Feature, EventFix<Feature> >{	public:		typedef typename Feature::FeatureFreq FeatureFreq;		bool make_comb_feature;		EventFixSpaceBase( bool comb = false ) {		make_comb_feature = comb;		if( make_comb_feature ) {			std::cerr << "Combination feature on." << std::endl;		}	}		void setMakeCombinationFeature( bool b ) {		make_comb_feature = b;	}	/*	  // for runtime  virtual bool setProducts( std::istream& is, std::vector<Real>& to ) const {	    EventFix<Feature> event( model );		bool suc = inputEvent( is, event );		if( !suc ) { return false ; }				Real* prods = new Real[ model->numTargets() ];				model->setProducts( event, prods );				to.resize( model->numTargets() );				for(int i = 0; i < model->numTargets(); i++) {			to[i] = prods[i];		}		delete[] prods;		return true;  }    void convert_histname2id( const std::vector< std::pair<Name, Real> >& feats, std::vector< std::pair<FeatureID,FeatureFreq> >& to ) const {	  to.resize(0);	  for(int i = 0; i < feats.size(); i++){		  FeatureID   hid  = model->historyID( feats[i].first );		  FeatureFreq freq = (FeatureFreq)( feats[i].second );		  if( hid >= 0 ) {			  to.push_back( std::pair<FeatureID, FeatureFreq>( hid, freq ) );		  }		  // ignore unknown history	  }  }    void convert_targetid2name( const std::vector< Real >& prods, std::vector< std::pair<Name, Real> >& to ) const {	  to.resize( 0 );	  for( int i = 0; i < prods.size(); i++) {		  to.push_back( std::pair<Name, Real>( model->targetName( i ), prods[i] ) );	  }  }      bool setProductsFixNative(const std::vector< std::pair<FeatureID, FeatureFreq> >& features,  std::vector< Real >& prods ) const  {	 // std::cerr << __PRETTY_FUNCTION__ << " obsolate." << std::endl;	  	  int num_targets = model->numTargets();	  	  	  prods.resize( num_targets );	  for(int i = 0; i < num_targets; i++){		  prods[i] = 0.0;	  }		  	  const std::vector< std::vector< std::pair<FeatureID, FeatureID> > >& feature_map = model->getFeatures();	  for(typename std::vector< std::pair<FeatureID, FeatureFreq> >::const_iterator itr = features.begin(); itr != features.end(); ++itr){		  FeatureID    hid  = itr->first;		  FeatureFreq  freq = (FeatureFreq)(itr->second);		  const std::vector< std::pair<FeatureID, FeatureID> >& fv = feature_map[hid];		  for(int k = 0; k < fv.size(); k++){			  FeatureID tid = fv[k].first;			  FeatureID fid = fv[k].second;			  Real lambda = (*model)[ fid ];			  prods[tid] += lambda * freq;		  }	  }	  	  	  for(int i = 0; i < num_targets; i++){		  if( !finite( prods[ i ] ) && prods[ i ] > 0.0 ) {			  prods[ i ] = Model::MAX_EXPONENT;		  }	  }	  	  Real max_e;	  int  max_i;	  	  Model::find_max( prods, num_targets, &max_e, &max_i );	  	  	  Real sum = 0.0;	  for(int i = 0; i < num_targets; i++){		  prods[ i ] = exp( prods[ i ] - max_e );		  sum += prods[ i ];	  }	  	  if( !finite( sum ) ){		  std::cerr << "IN: " << __PRETTY_FUNCTION__ << std::endl;		  std::cerr << "sum is not finite. this should not occur" << std::endl;	  }	  	  return true;  }    virtual bool setProductsFix(const std::vector< std::pair<Name, Real> >& feats,  std::vector< std::pair<Name,Real> >& to) const  {	  int num_targets = model->numTargets();	  	  std::vector< std::pair<FeatureID, FeatureFreq> > features;	  	  convert_histname2id( feats, features );	  	  std::vector<Real> prods( num_targets );	  	  setProductsFixNative( features, prods );		  convert_targetid2name( prods, to); 	  	  return true;  }	*/};//////////////////////////////////////////////////////////////////////template<class Feature>class EventFixSpaceOnMemory : public EventFixSpaceBase<Feature>{private:  std::vector< EventFix<Feature> > event_list;  public:  typedef EventFixSpaceBase<Feature> Base;  EventFixSpaceOnMemory( bool comb = false) : Base( comb ) { }    virtual ~EventFixSpaceOnMemory(){ }  const std::string eventSpaceName() const {    return "EventFixSpaceOnMemory<" + Feature::featureTypeName() + ">";  }  virtual const EventFix<Feature>& operator[](int i) const {     return event_list[i];  }protected:  virtual void push( EventFix<Feature>& event )   {    event_list.push_back( event );  }};template<class Feature>class EventFixSpaceOnFile : public EventFixSpaceBase<Feature>{private:  VectorOnFile< EventFix<Feature> > event_list;    public:typedef EventFixSpaceBase<Feature> Base;  EventFixSpaceOnFile(objstream* s, bool comb = false) : Base( comb ), event_list(s) { }  virtual ~EventFixSpaceOnFile(){ }  const std::string eventSpaceName() const {    return "EventFixSpaceOnFile<" + Feature::featureTypeName() + ">";  }  virtual const EventFix<Feature>& operator[](int i) const {    return event_list[i];  }protected:  virtual void push( EventFix<Feature>& event )   {    event_list.push_back(event);  }};AMIS_NAMESPACE_END#endif

⌨️ 快捷键说明

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