25 #ifdef USE_HMMPARALLEL 26 #define USE_HMMPARALLEL_STRUCTURES 1 32 template <
class ST>
class CStringFeatures;
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS 47 T_ALPHA_BETA_TABLE* table;
55 #endif // DOXYGEN_SHOULD_SKIP_THIS 112 inline int32_t get_learn_a(int32_t line, int32_t column)
const 115 return learn_a[line*2 + column];
121 return learn_b[line*2 + column];
127 return learn_p[offset];
133 return learn_q[offset];
139 return const_a[line*2 + column];
145 return const_b[line*2 + column];
151 return const_p[offset];
157 return const_q[offset];
163 return const_a_val[line];
169 return const_b_val[line];
175 return const_p_val[offset];
181 return const_q_val[offset];
184 inline char get_fix_pos_state(int32_t pos, T_STATES state, T_STATES num_states)
188 if ((pos<0)||(pos*num_states+state>65336))
189 SG_DEBUG(
"index out of range in get_fix_pos_state(%i,%i,%i) \n", pos,state,num_states)
191 return fix_pos_state[pos*num_states+state] ;
200 inline void set_learn_a(int32_t offset, int32_t value)
203 learn_a[offset]=value;
209 learn_b[offset]=value;
215 learn_p[offset]=value;
221 learn_q[offset]=value;
227 const_a[offset]=value;
233 const_b[offset]=value;
239 const_p[offset]=value;
245 const_q[offset]=value;
251 const_a_val[offset]=value;
257 const_b_val[offset]=value;
263 const_p_val[offset]=value;
269 const_q_val[offset]=value;
272 inline void set_fix_pos_state(
274 int32_t pos, T_STATES state, T_STATES num_states,
char value)
277 if ((pos<0)||(pos*num_states+state>65336))
278 SG_DEBUG(
"index out of range in set_fix_pos_state(%i,%i,%i,%i) [%i]\n", pos,state,num_states,(
int)value, pos*num_states+state)
280 fix_pos_state[pos*num_states+state]=value;
281 if (value==FIX_ALLOWED)
282 for (int32_t i=0; i<num_states; i++)
283 if (get_fix_pos_state(pos,i,num_states)==FIX_DEFAULT)
284 set_fix_pos_state(pos,i,num_states,FIX_DISALLOWED) ;
289 const static char FIX_DISALLOWED ;
292 const static char FIX_ALLOWED ;
295 const static char FIX_DEFAULT ;
298 const static float64_t DISALLOWED_PENALTY ;
375 T_STATES trans_list_len ;
376 T_STATES **trans_list_forward ;
377 T_STATES *trans_list_forward_cnt ;
379 T_STATES **trans_list_backward ;
380 T_STATES *trans_list_backward_cnt ;
381 bool mem_initialized ;
383 #ifdef USE_HMMPARALLEL_STRUCTURES 386 struct S_DIM_THREAD_PARAM
394 struct S_BW_THREAD_PARAM
408 inline T_ALPHA_BETA & ALPHA_CACHE(int32_t dim) {
409 return alpha_cache[dim%parallel->get_num_threads()] ; } ;
410 inline T_ALPHA_BETA & BETA_CACHE(int32_t dim) {
411 return beta_cache[dim%parallel->get_num_threads()] ; } ;
412 #ifdef USE_LOGSUMARRAY 414 return arrayS[dim%parallel->get_num_threads()] ; } ;
417 return arrayN1[dim%parallel->get_num_threads()] ; } ;
419 return arrayN2[dim%parallel->get_num_threads()] ; } ;
420 inline T_STATES* STATES_PER_OBSERVATION_PSI(int32_t dim) {
421 return states_per_observation_psi[dim%parallel->get_num_threads()] ; } ;
422 inline const T_STATES* STATES_PER_OBSERVATION_PSI(int32_t dim)
const {
423 return states_per_observation_psi[dim%parallel->get_num_threads()] ; } ;
424 inline T_STATES* PATH(int32_t dim) {
425 return path[dim%parallel->get_num_threads()] ; } ;
426 inline bool & PATH_PROB_UPDATED(int32_t dim) {
427 return path_prob_updated[dim%parallel->get_num_threads()] ; } ;
428 inline int32_t & PATH_PROB_DIMENSION(int32_t dim) {
429 return path_prob_dimension[dim%parallel->get_num_threads()] ; } ;
431 inline T_ALPHA_BETA & ALPHA_CACHE(int32_t ) {
432 return alpha_cache ; } ;
433 inline T_ALPHA_BETA & BETA_CACHE(int32_t ) {
434 return beta_cache ; } ;
435 #ifdef USE_LOGSUMARRAY 443 inline T_STATES* STATES_PER_OBSERVATION_PSI(int32_t ) {
444 return states_per_observation_psi ; } ;
445 inline const T_STATES* STATES_PER_OBSERVATION_PSI(int32_t )
const {
446 return states_per_observation_psi ; } ;
447 inline T_STATES* PATH(int32_t ) {
449 inline bool & PATH_PROB_UPDATED(int32_t ) {
450 return path_prob_updated ; } ;
451 inline int32_t & PATH_PROB_DIMENSION(int32_t ) {
452 return path_prob_dimension ; } ;
511 virtual bool train(
CFeatures* data=NULL);
513 virtual float64_t get_log_model_parameter(int32_t num_param);
514 virtual float64_t get_log_derivative(int32_t num_param, int32_t num_example);
517 return model_probability(num_example);
525 bool initialize(
Model* model,
float64_t PSEUDO, FILE* model_file=NULL);
529 bool alloc_state_dependend_arrays();
532 void free_state_dependend_arrays();
545 float64_t forward_comp(int32_t time, int32_t state, int32_t dimension);
547 int32_t time, int32_t state, int32_t dimension);
556 float64_t backward_comp(int32_t time, int32_t state, int32_t dimension);
558 int32_t time, int32_t state, int32_t dimension);
581 if (mod_prob_updated)
582 return mod_prob/p_observations->get_num_vectors();
584 return model_probability_comp()/p_observations->get_num_vectors();
587 return forward(p_observations->get_vector_length(dimension), 0, dimension);
600 uint16_t* o=p_observations->get_feature_vector(dimension, len, free_vec);
605 for (int32_t i=0; i<N; i++)
610 p_observations->free_feature_vector(o, dimension, free_vec);
643 void estimate_model_baum_welch(
CHMM* train);
644 void estimate_model_baum_welch_trans(
CHMM* train);
646 #ifdef USE_HMMPARALLEL_STRUCTURES 651 void estimate_model_baum_welch_old(
CHMM* train);
657 void estimate_model_baum_welch_defined(
CHMM* train);
662 void estimate_model_viterbi(
CHMM* train);
667 void estimate_model_viterbi_defined(
CHMM* train);
672 bool linear_train(
bool right_align=
false);
675 bool permutation_entropy(int32_t window_width, int32_t sequence_number);
683 void output_model(
bool verbose=
false);
686 void output_model_defined(
bool verbose=
false);
694 void normalize(
bool keep_dead_states=
false);
699 void add_states(int32_t num_states,
float64_t default_val=0);
712 bool append_model(
CHMM* append_model);
718 void convert_to_log();
721 void init_model_random();
728 void init_model_defined();
734 void clear_model_defined();
737 void copy_model(
CHMM* l);
743 void invalidate_model();
765 #ifdef USE_HMMPARALLEL_STRUCTURES 766 static void* bw_dim_prefetch(
void * params);
767 static void* bw_single_dim_prefetch(
void * params);
768 static void* vit_dim_prefetch(
void * params);
775 inline bool set_fix_pos_state(int32_t pos, T_STATES state,
char value)
779 model->set_fix_pos_state(pos, state, N, value) ;
804 return p_observations;
875 bool load_definitions(FILE* file,
bool verbose,
bool initialize=
true);
912 bool load_model(FILE* file);
917 bool save_model(FILE* file);
922 bool save_model_derivatives(FILE* file);
927 bool save_model_derivatives_bin(FILE* file);
932 bool save_model_bin(FILE* file);
935 bool check_model_derivatives() ;
936 bool check_model_derivatives_combined() ;
943 T_STATES* get_path(int32_t dim,
float64_t& prob);
948 bool save_path(FILE* file);
953 bool save_path_derivatives(FILE* file);
958 bool save_path_derivatives_bin(FILE* file);
961 bool check_path_derivatives() ;
963 #endif //USE_HMMDEBUG 968 bool save_likelihood_bin(FILE* file);
973 bool save_likelihood(FILE* file);
983 inline T_STATES
get_N()
const {
return N ; }
986 inline int32_t
get_M()
const {
return M ; }
996 SG_DEBUG(
"index out of range in set_q(%i,%e) [%i]\n", offset,value,N)
998 end_state_distribution_q[offset]=value;
1009 SG_DEBUG(
"index out of range in set_p(%i,.) [%i]\n", offset,N)
1011 initial_state_distribution_p[offset]=value;
1022 if ((line_>N)||(column>N))
1023 SG_DEBUG(
"index out of range in set_A(%i,%i,.) [%i,%i]\n",line_,column,N,N)
1025 transition_matrix_A[line_+column*N]=value;
1036 if ((line_>N)||(column>N))
1037 SG_DEBUG(
"index out of range in set_a(%i,%i,.) [%i,%i]\n",line_,column,N,N)
1039 transition_matrix_a[line_+column*N]=value;
1050 if ((line_>=N)||(column>=M))
1051 SG_DEBUG(
"index out of range in set_B(%i,%i) [%i,%i]\n", line_, column,N,M)
1053 observation_matrix_B[line_*M+column]=value;
1064 if ((line_>=N)||(column>=M))
1065 SG_DEBUG(
"index out of range in set_b(%i,%i) [%i,%i]\n", line_, column,N,M)
1067 observation_matrix_b[line_*M+column]=value;
1077 int32_t time, T_STATES state, T_STATES value, int32_t dimension)
1080 if ((time>=p_observations->get_max_vector_length())||(state>N))
1081 SG_DEBUG(
"index out of range in set_psi(%i,%i,.) [%i,%i]\n",time,state,p_observations->get_max_vector_length(),N)
1083 STATES_PER_OBSERVATION_PSI(dimension)[time*N+state]=value;
1094 SG_DEBUG(
"index out of range in %e=get_q(%i) [%i]\n", end_state_distribution_q[offset],offset,N)
1096 return end_state_distribution_q[offset];
1107 SG_DEBUG(
"index out of range in get_p(%i,.) [%i]\n", offset,N)
1109 return initial_state_distribution_p[offset];
1120 if ((line_>N)||(column>N))
1121 SG_DEBUG(
"index out of range in get_A(%i,%i) [%i,%i]\n",line_,column,N,N)
1123 return transition_matrix_A[line_+column*N];
1134 if ((line_>N)||(column>N))
1135 SG_DEBUG(
"index out of range in get_a(%i,%i) [%i,%i]\n",line_,column,N,N)
1137 return transition_matrix_a[line_+column*N];
1148 if ((line_>=N)||(column>=M))
1149 SG_DEBUG(
"index out of range in get_B(%i,%i) [%i,%i]\n", line_, column,N,M)
1151 return observation_matrix_B[line_*M+column];
1162 if ((line_>=N)||(column>=M))
1163 SG_DEBUG(
"index out of range in get_b(%i,%i) [%i,%i]\n", line_, column,N,M)
1166 return observation_matrix_b[line_*M+column];
1176 int32_t time, T_STATES state, int32_t dimension)
const 1179 if ((time>=p_observations->get_max_vector_length())||(state>N))
1180 SG_DEBUG(
"index out of range in get_psi(%i,%i) [%i,%i]\n",time,state,p_observations->get_max_vector_length(),N)
1182 return STATES_PER_OBSERVATION_PSI(dimension)[time*N+state];
1270 #ifdef USE_HMMPARALLEL_STRUCTURES 1275 #else //USE_HMMPARALLEL_STRUCTURES 1280 #endif //USE_HMMPARALLEL_STRUCTURES 1282 #ifdef USE_LOGSUMARRAY 1283 #ifdef USE_HMMPARALLEL_STRUCTURES 1289 #endif // USE_HMMPARALLEL_STRUCTURES 1290 #endif // USE_LOGSUMARRAY 1292 #ifdef USE_HMMPARALLEL_STRUCTURES 1295 T_ALPHA_BETA* alpha_cache ;
1297 T_ALPHA_BETA* beta_cache ;
1300 T_STATES** states_per_observation_psi ;
1306 bool* path_prob_updated ;
1309 int32_t* path_prob_dimension ;
1311 #else //USE_HMMPARALLEL_STRUCTURES 1312 T_ALPHA_BETA alpha_cache;
1329 #endif //USE_HMMPARALLEL_STRUCTURES 1372 int32_t time, int32_t state, int32_t dimension)
1374 return forward(time, state, dimension) + backward(time, state, dimension) - model_probability(dimension);
1379 int32_t time, int32_t state_i, int32_t state_j, int32_t dimension)
1381 return forward(time, state_i, dimension) +
1382 backward(time+1, state_j, dimension) +
1383 get_a(state_i,state_j) + get_b(state_j,p_observations->
get_feature(dimension ,time+1)) - model_probability(dimension);
1396 T_STATES i, uint16_t j, int32_t dimension)
1400 for (int32_t k=0; k<N; k++)
1402 if (k!=i || p_observations->
get_feature(dimension, k) != j)
1403 der+=get_b(k, p_observations->
get_feature(dimension, k));
1414 return backward(0,i,dimension)+get_b(i, p_observations->
get_feature(dimension, 0));
1461 best_path(dimension);
1462 return (i==PATH(dimension)[0]) ? (exp(-get_p(PATH(dimension)[0]))) : (0) ;
1468 best_path(dimension);
1475 prepare_path_derivative(dimension) ;
1476 return (get_A(i,j)==0) ? (0) : (get_A(i,j)*exp(-get_a(i,j))) ;
1482 prepare_path_derivative(dimension) ;
1483 return (get_B(i,j)==0) ? (0) : (get_B(i,j)*exp(-get_b(i,j))) ;
1494 bool get_numbuffer(FILE* file,
char* buffer, int32_t length);
1498 void open_bracket(FILE* file);
1501 void close_bracket(FILE* file);
1504 bool comma_or_space(FILE* file);
1507 inline void error(int32_t p_line,
const char* str)
1510 SG_ERROR(
"error in line %d %s\n", p_line, str)
1519 if (path_deriv_updated && (path_deriv_dimension==dim))
1535 set_A(PATH(dim)[t], PATH(dim)[t+1], get_A(PATH(dim)[t], PATH(dim)[t+1])+1);
1536 set_B(PATH(dim)[t], p_observations->
get_feature(dim,t), get_B(PATH(dim)[t], p_observations->
get_feature(dim,t))+1);
1539 path_deriv_dimension=dim ;
1540 path_deriv_updated=true ;
1550 if (ALPHA_CACHE(dimension).table && (dimension==ALPHA_CACHE(dimension).dimension) && ALPHA_CACHE(dimension).updated)
1552 if (time<p_observations->get_vector_length(dimension))
1553 return ALPHA_CACHE(dimension).table[time*N+state];
1555 return ALPHA_CACHE(dimension).sum;
1558 return forward_comp(time, state, dimension) ;
1564 if (BETA_CACHE(dimension).table && (dimension==BETA_CACHE(dimension).dimension) && (BETA_CACHE(dimension).updated))
1567 return BETA_CACHE(dimension).sum;
1568 if (time<p_observations->get_vector_length(dimension))
1569 return BETA_CACHE(dimension).table[time*N+state];
1574 return backward_comp(time, state, dimension) ;
int32_t get_learn_p(int32_t offset) const
get entry out of learn_p vector
int32_t * learn_p
start states to be learned
float64_t * transition_matrix_a
transition matrix
bool mod_prob_updated
true if model probability is up to date
int32_t N
number of states
float64_t backward(int32_t time, int32_t state, int32_t dimension)
inline proxies for backward pass
static const int32_t GOTp
float64_t get_const_p_val(int32_t offset) const
get value out of const_p_val vector
float64_t * const_a_val
values for transitions that have constant probability
virtual const char * get_name() const
virtual int32_t get_num_model_parameters()
void set_const_p(int32_t offset, int32_t value)
set value in const_p vector
int32_t get_M() const
access function for number of observations M
float64_t pat_prob
probability of best path
float64_t get_const_b_val(int32_t line) const
get value out of const_b_val vector
void set_const_p_val(int32_t offset, float64_t value)
set value in const_p_val vector
static const float64_t INFTY
infinity
T_STATES * states_per_observation_psi
backtracking table for viterbi can be terrible HUGE O(T*N)
float64_t forward(int32_t time, int32_t state, int32_t dimension)
inline proxies for forward pass
float64_t * const_q_val
values for end states that have constant probability
bool all_path_prob_updated
true if path probability is up to date
float64_t epsilon
convergence criterion epsilon
static const int32_t GOTconst_p
viterbi only for defined transitions/observations
int32_t get_const_p(int32_t offset) const
get entry out of const_p vector
baum welch only for defined transitions/observations
int32_t * learn_b
emissions to be learned
float64_t * transition_matrix_A
matrix of absolute counts of transitions
static const int32_t GOTlearn_a
float64_t path_derivative_b(T_STATES i, uint16_t j, int32_t dimension)
computes d log p(lambda,best_path)/d b_ij
T_ALPHA_BETA beta_cache
cache for backward variables can be terrible HUGE O(T*N)
float64_t get_b(T_STATES line_, uint16_t column) const
bool path_prob_updated
true if path probability is up to date
float64_t state_probability(int32_t time, int32_t state, int32_t dimension)
calculates probability of being in state i at time t for dimension
static const int32_t GOTO
float64_t * observation_matrix_B
matrix of absolute counts of observations within each state
float64_t get_A(T_STATES line_, T_STATES column) const
float64_t T_ALPHA_BETA_TABLE
type for alpha/beta caching table
Base class Distribution from which all methods implementing a distribution are derived.
float64_t linear_model_derivative(T_STATES i, uint16_t j, int32_t dimension)
static const int32_t GOTlearn_p
int32_t get_learn_q(int32_t offset) const
get entry out of learn_q vector
float64_t get_pseudo() const
returns current pseudo value
float64_t get_B(T_STATES line_, uint16_t column) const
float64_t * const_b_val
values for emissions that have constant probability
static const int32_t GOTlearn_q
uint16_t get_best_path_state(int32_t dim, int32_t t)
CStringFeatures< uint16_t > * get_observations()
return observation pointer
static const float64_t epsilon
int32_t path_prob_dimension
dimension for which path_prob was calculated
virtual int32_t get_vector_length(int32_t vec_num)
int32_t * learn_q
end states to be learned
void set_pseudo(float64_t pseudo)
sets current pseudo value
float64_t model_probability(int32_t dimension=-1)
inline proxy for model probability.
int32_t path_deriv_dimension
dimension for which path_deriv was calculated
static const int32_t GOTb
float64_t mod_prob
probability of model
float64_t model_derivative_q(T_STATES i, int32_t dimension)
float64_t * const_p_val
values for start states that have constant probability
T_STATES get_psi(int32_t time, T_STATES state, int32_t dimension) const
virtual float64_t get_log_likelihood_example(int32_t num_example)
void set_p(T_STATES offset, float64_t value)
static const int32_t GOTconst_a
float64_t * end_state_distribution_q
distribution of end-states
float64_t PSEUDO
define pseudocounts against overfitting
float64_t all_pat_prob
probability of best path
float64_t path_derivative_a(T_STATES i, T_STATES j, int32_t dimension)
computes d log p(lambda,best_path)/d a_ij
void set_const_q_val(int32_t offset, float64_t value)
set value in const_q_val vector
float64_t transition_probability(int32_t time, int32_t state_i, int32_t state_j, int32_t dimension)
calculates probability of being in state i at time t and state j at time t+1 for dimension ...
float64_t get_q(T_STATES offset) const
int32_t * const_q
end states that have constant probability
CStringFeatures< uint16_t > * p_observations
observation matrix
void set_learn_q(int32_t offset, int32_t value)
set value in learn_q vector
void set_A(T_STATES line_, T_STATES column, float64_t value)
void set_q(T_STATES offset, float64_t value)
void set_B(T_STATES line_, uint16_t column, float64_t value)
int32_t get_const_q(int32_t offset) const
get entry out of const_q vector
int32_t iterations
convergence criterion iterations
float64_t get_const_q_val(int32_t offset) const
get value out of const_q_val vector
static const int32_t GOTq
static const int32_t GOTlearn_b
float64_t * observation_matrix_b
distribution of observations within each state
float64_t model_derivative_a(T_STATES i, T_STATES j, int32_t dimension)
computes log dp(lambda)/d a_ij.
int32_t get_const_a(int32_t line, int32_t column) const
get entry out of const_a matrix
void set_const_b_val(int32_t offset, float64_t value)
set value in const_b_val vector
bool path_deriv_updated
true if path derivative is up to date
bool set_epsilon(float64_t eps)
void sort_learn_a()
sorts learn_a matrix
void set_learn_p(int32_t offset, int32_t value)
set value in learn_p vector
bool set_iterations(int32_t num)
float64_t linear_model_probability(int32_t dimension)
void set_learn_b(int32_t offset, int32_t value)
set value in learn_b matrix
float64_t * initial_state_distribution_p
initial distribution of states
baum welch only for specified transitions
float64_t get_a(T_STATES line_, T_STATES column) const
all of classes and functions are contained in the shogun namespace
float64_t path_derivative_p(T_STATES i, int32_t dimension)
computes d log p(lambda,best_path)/d p_i
float64_t model_derivative_p(T_STATES i, int32_t dimension)
float64_t get_p(T_STATES offset) const
int32_t * const_b
emissions that have constant probability
void sort_learn_b()
sorts learn_b matrix
The class Features is the base class of all feature objects.
static const int32_t GOTconst_b
void set_const_a_val(int32_t offset, float64_t value)
set value in const_a_val vector
void set_const_a(int32_t offset, int32_t value)
set value in const_a matrix
void set_b(T_STATES line_, uint16_t column, float64_t value)
float64_t path_derivative_q(T_STATES i, int32_t dimension)
computes d log p(lambda,best_path)/d q_i
static const int32_t GOTM
void prepare_path_derivative(int32_t dim)
initialization function that is called before path_derivatives are calculated
static void sort(int32_t *a, int32_t cols, int32_t sort_col=0)
int32_t get_const_b(int32_t line, int32_t column) const
get entry out of const_b matrix
static const int32_t GOTconst_q
void set_psi(int32_t time, T_STATES state, T_STATES value, int32_t dimension)
void set_a(T_STATES line_, T_STATES column, float64_t value)
static float64_t logarithmic_sum(float64_t p, float64_t q)
static const int32_t GOTN
virtual ST get_feature(int32_t vec_num, int32_t feat_num)
T_STATES * path
best path (=state sequence) through model
int32_t * const_p
start states that have constant probability
void set_const_b(int32_t offset, int32_t value)
set value in const_b matrix
T_STATES get_N() const
access function for number of states N
void set_const_q(int32_t offset, int32_t value)
set value in const_q vector
int32_t get_learn_b(int32_t line, int32_t column) const
get entry out of learn_b matrix
void error(int32_t p_line, const char *str)
parse error messages
float64_t model_derivative_b(T_STATES i, uint16_t j, int32_t dimension)
computes log dp(lambda)/d b_ij.
float64_t get_const_a_val(int32_t line) const
get value out of const_a_val vector
static const int32_t GOTa