📄 modelbase.h
字号:
//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao/// You may distribute under the terms of the Artistic License.////// <id>$Id: ModelBase.h,v 1.7 2003/05/16 05:29:08 yusuke Exp $</id>/// <collection>Maximum Entropy Estimator</collection>/// <name>ModelBase.h</name>/// <overview>Base class for Model</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_ModelBase_h_#define Amis_ModelBase_h_#include <amis/configure.h>#include <amis/Real.h>#include <amis/Feature.h>#include <vector>#include <iostream>#include <cmath>#include <memory>AMIS_NAMESPACE_BEGIN///////////////////////////////////////////////////////////////////////// <classdef>/// <name>ModelBase</name>/// <overview>Base class of Model</overview>/// <body>class ModelBase {private: std::vector< bool > is_freezed;public: static const Real LAMBDA_MAX; static const Real LAMBDA_MIN; static const Real MAX_EXPONENT; static const Real LOG_PROB_ZERO; static const Real PROB_ZERO;public: ModelBase() {} /// Constructor virtual ~ModelBase() {} /// Destructor virtual std::string modelName() const = 0; /// Name of this class virtual std::string featureNameString( FeatureID ) const = 0; /// Name of a feature virtual void debugInfo( std::ostream& ostr ) const = 0; /// Output the debug information /*public: typedef std::vector<Real>::iterator iterator; typedef std::vector<Real>::const_iterator const_iterator; iterator begin() { return lambda.begin(); } const_iterator begin() const { return lambda.begin(); } iterator end() { return lambda.end(); } const_iterator end() const { return lambda.end(); } */public: static Real power( Real b, Real e ) { return pow( b, e ); } static Real power( Real b, int e ) { Real p = 1.0; for ( int j = 0; j < e; ++j ) { p *= b; } return p; } static Real safe_log( Real x ) { Real l = log( x ); return ( finite( l ) ? l : LOG_PROB_ZERO ); }public: bool isFreezed( FeatureID id ) const { return is_freezed[ id ]; } void freeze( FeatureID id, bool f = true ) { is_freezed[ id ] = f; } //////////////////////////////////////////////////////////////////////#ifdef AMIS_FEATURE_WEIGHT_LAMBDAprivate: std::vector< Real > lambda;public: FeatureID numFeatures() const { return lambda.size(); } Real getLambda( FeatureID id ) const { return lambda[ id ]; } Real getAlpha( FeatureID id ) const { return exp( lambda[ id ] ); } void setLambda( FeatureID id, Real l ) { if ( ! isFreezed( id ) ) lambda[ id ] = l; } void addLambda( FeatureID id, Real a ) { if ( ! isFreezed( id ) ) lambda[ id ] += a; } FeatureID newFeature( Real l ) { FeatureID id = lambda.size(); lambda.push_back( l ); is_freezed.push_back( false ); return id; } //////////////////////////////////////////////////////////////////////#else // AMIS_FEATURE_WEIGHT_LAMBDAprivate: std::vector< Real > alpha;public: FeatureID numFeatures() const { return alpha.size(); } Real getLambda( FeatureID id ) const { return log( alpha[ id ] ); } Real getAlpha( FeatureID id ) const { return alpha[ id ]; } void setLambda( FeatureID id, Real l ) { if ( ! isFreezed( id ) ) alpha[ id ] = exp( l ); } void addLambda( FeatureID id, Real a ) { if ( ! isFreezed( id ) ) alpha[ id ] *= exp( a ); } FeatureID newFeature( Real l ) { FeatureID id = alpha.size(); alpha.push_back( exp( l ) ); is_freezed.push_back( false ); return id; }#endif // AMIS_FEATURE_WEIGHT_LAMBDA};//////////////////////////////////////////////////////////////////////inline std::ostream& operator<<( std::ostream& os, const ModelBase& model ) { model.debugInfo( os ); return os;}/// </body>/// </classdef>typedef std::auto_ptr< ModelBase > ModelPtr;AMIS_NAMESPACE_END#endif // Amis_ModelBase_h_// end of ModelBase.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -