📄 hmm.h
字号:
// file : hmm.h// version: 1.03 [August 21, 1995]/*
Copyright (C) 1994 Richard Myers and James Whitson
Permission is granted to any individual or institution to use, copy, or
redistribute this software so long as all of the original files are
included unmodified, that it is not sold for profit, and that this
copyright notice is retained.
范士喜把 这个源代码 修改成了一个通用的hmm模型
*/// some global constantsconst double MIN_PROB = 0.000001; // never let alpha or beta equal zeroconst int MAX_LINE = 1000; // length of sequencesconst int TRUE = 1; // flagconst int FALSE = 0; // flagclass STATE {
public: double* recur_out; // array of output symbols double* next_trans;
double start; int max_symbols;
int max_states;public: STATE(int num_state,int num_symbols); ~STATE() {delete recur_out; delete next_trans;}; double set_recur_out(int symbol, double prob=-1.0); double set_next_trans(int state,double prob=-1.0);}; class HMM {public:
int seed; // seed for rnd init of probs int max_states; // number of states in the model int max_symbols; // symbol emissions per state int trellis_width; // cols allocated in trellis =num_symbols-1 int num_strings; // number of training/testing strings int** strings; // matrix of training/testing strings int diff_len; // TRUE if strings are of different lengths int* string_len; // use if strings are of different length STATE** States; // matrix of transition and emission probs double** alpha; // matrix for alpha trellis double** beta; // matrix for beta trellis double* scaling_factors; // array of alpha and beta scaling factors double** gamma; // matrix of gamma transition probs
double*** epsum; //[t][i][j] t时刻为状态i t+1时刻为状态j的概率
///////////////////////////////////////////////训练的时候针对多个实例使用的
double** sum_gamma; //[t][i] t时刻为状态i的概率 double** sum_aij; //[i][j] t时状态为i t+1时状态为 j的概率 a_ij's double** sum_b; //[i][k] 状态为i时 观察值为k的概率 b_ij's //////////////////////////////////////////////// // ----- SPACE ALLOC ----- void alloc_model_matrices(); void alloc_training_matrices(); void alloc_testing_matrices(); // ------- MODEL INIT ------- void rnd_init_hmm_probs(); void rnd_sym_probs(double prob_syms[]);
void rnd_state_probs(double prob_states[]); // ------- FILE OPS ------- void load_model(char* filename); int load_string_matrix(char* filename); // ------ CALCULATIONS ------- void rescale_alphas(int col); void rescale_betas(int col); double alpha_F(int* symbol_array, int symbol_count=-1); double beta_I(int* symbol_array, int symbol_count=-1); void compute_gamma(int* symbol_array, int symbol_count=-1);
void compute_epsum(int* symbol_array, int symbol_count=-1); double set_ab_counts(int* string_array,int symbol_count); double reestimate(); // returns sum of changes double test(int* string, int symbol_count=-1);// test one string public: double viterbi(int* symbol_array, int symbol_count,int **out);
HMM(int symbols, int states, int new_seed=-1);// initialize random model HMM(char* filename); // initialize pre-built model ~HMM(); // deallocate arrays void batch_train(char* filename, double min_delta_psum); // train parameters double batch_test(char* filename); // test using file void show_probs(); // display model probabilities void dump_model(char* filename); // dumps the current model void set_seed(int s) { seed = s;} // set seed};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -