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

📄 is.h

📁 Amis - A maximum entropy estimator 一个最大熵模型统计工具
💻 H
字号:
////////////////////////////////////////////////////////////////////////////  Copyright (c) 2000, Yusuke Miyao///  You may distribute under the terms of the Artistic License.//////  <id>$Id: IS.h,v 1.9 2003/05/16 10:00:08 yusuke Exp $</id>///  <collection>Maximum Entropy Estimator</collection>///  <name>IS.h</name>///  <overview>Base class for Maximum Entropy Estimators</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_IS_h_#define Amis_IS_h_#include <amis/configure.h>#include <amis/LogStream.h>#include <amis/Estimator.h>#include <amis/EmpiricalExpect.h>#include <amis/FeatureFreqArray.h>#include <vector>AMIS_NAMESPACE_BEGIN///////////////////////////////////////////////////////////////////////// <classdef>/// <name>IS</name>/// <overview>Implementation of Iterative Scaling Algorithm</overview>/// <desc>/// This class implements an iterative scaling algorithm./// </desc>/// <body>template < class Model, class EventSpace,	   class ModelExpect, class EmpiricalExpect = EmpiricalExpect< Model, EventSpace > >class IS : public Estimator {protected:  Model* model;  /// Maximum entropy model  EventSpace* event_space;  /// Event space, i.e., Set of events  ModelExpect model_expectation;  /// Model expectation of features  EmpiricalExpect empirical_expectation;  /// Empirical expectation of features  int num_threads;  /// Number of threads  //////////////////////////////////////////////////////////////////////public:  IS() {    model = NULL;    event_space = NULL;    num_threads = 1;  }  IS( Model& init_model, EventSpace& init_event ) {    model = &init_model;    event_space = &init_event;    num_threads = 1;  }  /// Initialize with the initial model and the events  virtual ~IS() {}  //////////////////////////////////////////////////////////////////////public:  void setModel( ModelBase& m ) {    model = dynamic_cast< Model* >( &m );  }  /// Set the model  const Model& getModel() const {    return *model;  }  /// Get the model  void setEventSpace( EventSpaceBase& e ) {    event_space = dynamic_cast< EventSpace* >( &e );  }  /// Set the event space  const EventSpace& getEventSpace() const {    return *event_space;  }  /// Get the event space  virtual int getNumThreads() {    return num_threads;  }  virtual void setNumThreads( int n ) {    num_threads = n;  }  /// Set the number of threads  //////////////////////////////////////////////////////////////////////public:  void initialize() {    model_expectation.initialize( *model, *event_space, num_threads );    //std::vector< Real > val_sums;    //std::vector< Real > vval_sums;    //std::vector< Real > emp_cnts;    empirical_expectation.initialize( *model, *event_space );    empirical_expectation.setEmpiricalExpectation();  }  /// Initialization of the internal data before estimation  void finalize() {}  /// Finalization of the internal data after estimation  void iteration() {	      log_likelihood = model_expectation.setModelExpectation( isReportIteration() );	    setLogLikelihood( log_likelihood, event_space->sumEventCount() );    setObjectiveFunctionValue( log_likelihood );	    AMIS_DEBUG_MESSAGE( 3, "\nEmpirical Expectation:\n" );    AMIS_DEBUG_MESSAGE( 3, empirical_expectation );    AMIS_DEBUG_MESSAGE( 3, "\nModel Expectation:\n" );    AMIS_DEBUG_MESSAGE( 3, model_expectation );    AMIS_DEBUG_MESSAGE( 3, "\nModel:\n" );    AMIS_DEBUG_MESSAGE( 3, *model );    // Update a model according to model expectations    // not parallelized yet    AMIS_DEBUG_MESSAGE( 3, "\t\t\t\t>>>>>>>>>>>>>>>>>>>>>>>>\n" );    AMIS_DEBUG_MESSAGE( 3, "\t\t\t\tFeature\tAlpha\tUpdate\n" );    for ( int i = 0; i < model->numFeatures(); ++i ) {      if ( empirical_expectation[ i ] != 0.0 ) {        Real model_update = solveEquation( i );        AMIS_DEBUG_MESSAGE( 3, "\t\t\t\tFeature " << i << '\t' << model->getLambda( i ) <<                            '\t' << model_update << '\n' );        if ( ! finite( model_update ) ) {          AMIS_PROF_MESSAGE( "Infinite!: " << model->featureNameString( i ) << '\n' );          if ( ! isnan( model_update ) ) {            if ( model_update > 0.0 ) {              model->setLambda( i, model->LAMBDA_MAX );            } else {              model->setLambda( i, model->LAMBDA_MIN );            }          }          else{            // do not change          }        } else { // ! finite( model_update )          setMinMaxUpdate( model_update );          model->addLambda( i, model_update );        }      }    }    AMIS_DEBUG_MESSAGE( 5, "\t\t\t\t<<<<<<<<<<<<<<<<<<<<<<<<\n" );  }  /// Each iteration for estimation  const EmpiricalExpect& getEmpiricalExpectation() {    return empirical_expectation;  }  bool isConverged() {    return fabs( min_update ) < machineEpsilon() &&           fabs( max_update ) < machineEpsilon();  }    virtual Real solveEquation( int index ) = 0;};/// </body>/// </classdef>AMIS_NAMESPACE_END#endif // IS_h_// end of IS.h

⌨️ 快捷键说明

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