📄 ht_update_1.cc
字号:
// file: ht_update_1.cc//// system include files//#include <memory.h>// isip include files//#include "hmm_train.h"#include "hmm_train_constants.h"// method: combine_acc_cc//// arguments:// float_4*** transitions : (output) the transition data need to be updated// Train_State** states : (output) the states data need to be updated// int_4* trans_size : (input) size of each transition matrix// int_4 num_st : (input) the number of the states// int_4 num_mix : (input) the number of the mixtures// int_4 num_feat : (input) the number of the features// int_4 num_trans : (input) the number of the transition// float_4 var_floor : (input) variance floor// FILE* fp_list : (inptut) list of viterbi accumulator files// int_4** state_count : (inptut) list of viterbi state count accumulators//// return: a logical flag to indicate success//logical_1 combine_acc_cc(float_4*** transitions_a, Train_State** states_a, int_4* trans_size_a, int_4 num_st_a, int_4 num_mix_a, int_4 num_feat_a, int_4 num_trans_a, float_8 var_floor_a, FILE* fp_list_a, int_4** state_count_a) { // local variables // int_4 sum = (int_4)0; float_4*** mean = (float_4***)NULL; float_4*** covar = (float_4***)NULL; int_4** temp_st_count = (int_4**)NULL; int_4*** trans_count = (int_4***)NULL; int_4*** temp_trans_count = (int_4***)NULL; float_4* temp_scale = new float_4[num_mix_a]; float_4* temp_weight = new float_4[num_mix_a]; float_4** temp_mean = new float_4*[num_mix_a]; float_4** temp_covar = new float_4*[num_mix_a]; float_8* temp_double = new float_8[num_feat_a]; int_4 weight_sum = (int_4)0; float_4* temp_scale1 = (float_4*)NULL; float_4** temp_mean1 = (float_4**)NULL; float_4** temp_covar1 = (float_4**)NULL; float_4* temp_weight1 = (float_4*)NULL; for (int_4 i = 0; i < num_mix_a; i++) { temp_mean[i] = new float_4[num_feat_a]; temp_covar[i] = new float_4[num_feat_a]; temp_weight[i] = (float_4)0; } // define memory for the variable that hold the accumulated values // mean = new float_4**[num_st_a]; covar = new float_4**[num_st_a]; temp_st_count = new int_4*[num_st_a]; // for each transition matrix // trans_count = new int_4**[num_trans_a]; temp_trans_count = new int_4**[num_trans_a]; for (int_4 i = 0; i < num_trans_a; i++) { trans_count[i] = new int_4*[trans_size_a[i]]; temp_trans_count[i] = new int_4*[trans_size_a[i]]; for (int_4 j = 0; j < trans_size_a[i]; j++) { trans_count[i][j] = new int_4[trans_size_a[i]]; temp_trans_count[i][j] = new int_4[trans_size_a[i]]; memset((void_p)trans_count[i][j], 0, sizeof(float_4)*trans_size_a[i]); } } // for each state // for (int_4 i = 0; i < num_st_a; i++) { mean[i] = new float_4*[num_mix_a]; covar[i] = new float_4*[num_mix_a]; for (int_4 j = 0; j < num_mix_a; j++) { mean[i][j] = new float_4[num_feat_a]; covar[i][j] = new float_4[num_feat_a]; } memset((void_p)state_count_a[i], 0, sizeof(int_4)*num_mix_a); temp_st_count[i] = new int_4[num_mix_a]; } // loop over accumulator file list and open a list of file handles to // the accumulator files // FILE* fp_acc = (FILE*)NULL; char_1* acc_file = new char_1[ISIP_MAX_STRING_LENGTH]; while (fgets((char*)acc_file, ISIP_MAX_STRING_LENGTH, fp_list_a) != (char*)NULL) { // open file // acc_file[strlen((char*)acc_file)-1] = '\0'; fp_acc = fopen((char*)acc_file, "rb"); // get the state and transition counts from this accumulator // // loop over all states // for (int_4 i = 1; i < num_st_a; i++) { // get the number of times each mixture has been accessed during // training // for (int_4 j = 0; j < num_mix_a; j++) { fread((void_p)&temp_st_count[i][j], sizeof(int_4), (int_4)1, fp_acc); state_count_a[i][j] += temp_st_count[i][j]; } } // loop over all transitions // for (int_4 i = 1; i < num_trans_a; i++) { for (int_4 j = 0; j < trans_size_a[i]; j++) { for (int_4 k = 0; k < trans_size_a[i]; k++) { fread((void_p)&temp_trans_count[i][j][k], sizeof(int_4), (int_4)1, fp_acc); trans_count[i][j][k] += temp_trans_count[i][j][k]; } } } // loop over over the mean // for (int_4 i = 1; i < num_st_a; i++) { for (int_4 j = 0; j < num_mix_a; j++) { for (int_4 k = 0; k < num_feat_a; k++) { fread((void_p)&temp_double[k], sizeof(float_8), (int_4)1, fp_acc); mean[i][j][k] += temp_double[k]; } } } // loop over for the covariance // for (int_4 i = 1; i < num_st_a; i++) { for (int_4 j = 0; j < num_mix_a; j++) { for (int_4 k = 0; k < num_feat_a; k++) { fread((void_p)&temp_double[k], sizeof(float_8), (int_4)1, fp_acc); covar[i][j][k] += temp_double[k]; } } } // close the file // fclose (fp_acc); } delete [] acc_file; // update the means and variances in states array // for (int_4 i = 1; i < num_st_a; i++) { // initialize the temp variables // temp_scale1 = (float_4*)(states_a[i]->get_scale_cc()); temp_mean1 = (float_4**)(states_a[i]->get_mean_cc()); temp_covar1 = (float_4**)(states_a[i]->get_covar_cc()); temp_weight1 = (float_4*)(states_a[i]->get_weights_cc()); // initialize the means and variances to values from previous pass // of estimation // memcpy(temp_scale, temp_scale1, num_mix_a * sizeof(float_4)); memcpy(temp_weight, temp_weight1, num_mix_a * sizeof(float_4)); for (int_4 aa = 0; aa < num_mix_a; aa++) { memcpy(temp_mean[aa], temp_mean1[aa], num_feat_a * sizeof(float_4)); memcpy(temp_covar[aa], temp_covar1[aa], num_feat_a * sizeof(float_4)); } // compute the new mean and variance // for (int_4 j = 0; j < num_mix_a; j++) { for (int_4 l = 0; l < num_feat_a; l++) { // accumulate only if mixture has been accessed atleast once // if (state_count_a[i][j] > (int_4)0) { // compute the correct covariance by subtracting the square // of the mean // mean[i][j][l] = mean[i][j][l]/(float_4)state_count_a[i][j]; covar[i][j][l] = covar[i][j][l]/(float_4)state_count_a[i][j]; covar[i][j][l] = covar[i][j][l] - (mean[i][j][l]*mean[i][j][l]); } } } // update the mean and variance for the current state // for (int_4 tt = 0; tt < num_mix_a; tt++) { // accumulate only if mixture has been accessed atleast once // if (state_count_a[i][tt] > (int_4)0) { temp_scale[tt] = (float_4)0.0; for (int_4 ss = 0; ss < num_feat_a; ss++) { temp_mean[tt][ss] = (float_4)mean[i][tt][ss]; // accumulate covariance only if mixture has been accessed // atleast twice // if (state_count_a[i][tt] > (int_4)1) { // variance flooring // if (covar[i][tt][ss] < var_floor_a) { temp_covar[tt][ss] = 1.0 / var_floor_a; } else { temp_covar[tt][ss] = (float_4)1/(float_4)covar[i][tt][ss]; } } // accumulate scale factor // temp_scale[tt] += log((1.0/temp_covar[tt][ss]) * ISIP_TWOPI); } } } // update the mixture weight for all the states // weight_sum = (int_4)0; for (int_4 mm = 0; mm < num_mix_a; mm++) { weight_sum += state_count_a[i][mm]; } if (weight_sum > (int_4)0) { for (int_4 nn = 0; nn < num_mix_a; nn++) { // find new weight only if mixture has been accessed // (note that this does not guarantee weights summing to unity) // if (state_count_a[i][nn] > (int_4)0) { temp_weight[nn] = log((float_4)state_count_a[i][nn]/(float_4)weight_sum); } } } // update state information if state has been accessed // if(weight_sum > (int_4)1) { states_a[i]->set_mean_cc((float_4**)temp_mean); states_a[i]->set_covar_cc((float_4**)temp_covar); states_a[i]->set_scale_cc((float_4*)temp_scale); states_a[i]->set_weights_cc((float_4*)temp_weight); } // reset pointers // temp_scale1 = (float_4*)NULL; temp_mean1 = (float_4**)NULL; temp_covar1 = (float_4**)NULL; temp_weight1 = (float_4*)NULL; } // update the transition matrix // for (int_4 i = 1; i < num_trans_a; i++) { for (int_4 start_st = 0; start_st < trans_size_a[i]; start_st++) { for (int_4 end = 0; end < trans_size_a[i]; end++) { sum += trans_count[i][start_st][end]; } for (int_4 end_st = 0; end_st < trans_size_a[i]; end_st++) { if (sum > (int_4)0) { transitions_a[i][start_st][end_st] = trans_count[i][start_st][end_st]/(float_4)sum; } else { transitions_a[i][start_st][end_st] = exp(transitions_a[i][start_st][end_st]); } } // reset the sum // sum = (int_4)0; } } // free memory // for (int_4 i = 0; i < num_mix_a; i++) { delete [] temp_mean[i]; delete [] temp_covar[i]; } delete [] temp_mean; delete [] temp_covar; delete [] temp_scale; delete [] temp_weight; for (int_4 i = 0; i < num_st_a; i++) { delete [] temp_st_count[i]; } delete [] temp_st_count; for (int_4 i = 0; i < num_trans_a; i++) { for (int_4 j = 0; j < trans_size_a[i]; j++) { delete [] trans_count[i][j]; delete [] temp_trans_count[i][j]; } delete [] trans_count[i]; delete [] temp_trans_count[i]; } delete [] trans_count; delete [] temp_trans_count; for (int_4 i = 0; i < num_st_a; i++) { for (int_4 j = 0; j < num_mix_a; j++) { delete [] mean[i][j]; delete [] covar[i][j]; } delete [] mean[i]; delete [] covar[i]; } delete [] mean; delete [] covar; delete [] temp_double; // exit gracefully // return ISIP_TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -