亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? myhmm.c

?? 一個馬爾可夫模型的源碼
?? C
?? 第 1 頁 / 共 3 頁
字號:
/************************************************************************
 *                                                                      *
 *  Program packages 'myHMM' :                                          *
 *                                                                      *
 *  myHMM.c                                                             *
 *   -the main program for myHMM hidden Markov model                    *
 *                                                                      *
 *  Version 0.1                                                         *
 *  Date: 3 May 2003                                                    *
 *                                                                      *
 *  NOTE: This program package is copyrighted in the sense that it      *
 *  may be used for scientific purposes. The package as a whole, or     *
 *  parts thereof, cannot be included or used in any commercial         *
 *  application without written permission granted by its producents.   *
 *  No programs contained in this package may be copied for commercial  *
 *  distribution.                                                       *
 *                                                                      *
 *  All comments concerning this program package may be sent to the     *
 *  e-mail address 'dcslgl@nus.edu.sg'.                                 *
 *                                                                      *
 ************************************************************************/

/*************************************************************************
NAME
     myHMM - train the hidden Markov model

SYNOPSIS
     myHMM -hmm [HMM] [-train trainFile] [-trainedHmm trainedHmmFile]
     [-test testFile] [-tested testedFile] [-mode mode]

DESCRIPTION

     Input:
       required: pre-defined hidden Markov model
       optional: training data, testing data

     Output:
       optional: trained hidden Markov model, tested result

     Global variables:
       N: integer, number of states
       M: integer, number of observations
       T: integer, the length of the longest input sequence
       S: integer, finite set of possible states
       O: integer, finite set of possible observations
       t: double, transition matrix: N x N
       e: integer, emission matrix: N x M
       pi: integer, initial state distribution: 1 x N

       %% A hidden Markov model (HMM) is a five-tuple (S,O,t,e,pi).
       %% Let lambda = {t,e,pi} denote the parameters for a given HMM
       %% with fixed S and O.

       %% There are three basic problems in HMM:
       %% 1) probability of the observation sequence given the HMM model
       %%   -> forward algorithm & backward algorithm
       %% 2) the most probable state path of the observation sequence
       %%   given the HMM model
       %%   -> Viterbi algorithm
       %% 3) Building the model given a training set of sequence
       %%   -> Baum-Welch algorithm

OPTIONS
     -hmm [HMM]
         pre-defined hidden Markov Model

     -train trainFile
         training file

     -trainedHmmFile trainedHmmFile
         trained hidden Markov model

     -test testFile
         testing file

     -tested testedFile
         tested result file

     -mode mode
         mode must be one of 1, 2 or 3. '1' represents TRAINING, '2'
         represents TESTING, and '3' represents TRAINING_TESTING.
*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include "tools.h"
#include "myhmm.h"
#include "arg.h"

static char usage[] =
	"myHMM - hidden Markov model\n"
	"command: myHMM -hmm <hmm> [option]\n"
	"Required parameters:\n"
	"  -hmm filename        the file for hmm definition\n"
	"Optional parameters:\n"
	"  -train filename      the file for training\n"
	"  -test filename       the file for testing\n"
	"  -trainedHmm filename the file for trained hmm\n"
	"  -tested filename     the file for tested result\n"
	"  -mode int            the mode for training, testing or both\n"
   "                       1: training, 2: testing, 3: both\n";

/************************************************************************
NAME
     main - main function in the program

DESCRIPTION
     This function extracts parameters from the argument list and perform
     the specified actions by the parameters.

     Input:
       arguments in the command line
	 Output:
	   specified output(s): trained HMM file and/or tesed result file
	 Global variable list:
	   iTrain, iTest, T, trainData, testData, observationDefined,
	   extraSpace.
*************************************************************************/
int main(int argc, char** argv)
{
    char* hmmFile;
    char* trainFile;
    char* testFile;
    char* trainedHmmFile;
    char* testedFile;
    int mode;

    /*---------- get the parameters ----------*/
    if (argc <= 1 || extract_parameter(argc, argv, "-help", OPTION2) || extract_parameter(argc, argv, "-h", OPTION2) )
    {
        printf("%s", usage);
        exit(0);
    }
    hmmFile   = extract_parameter(argc, argv, HMM_FILE, ALWAYS);
    trainFile = extract_parameter(argc, argv, TRAINING_FILE, OPTION);
    testFile  = extract_parameter(argc, argv, TESTING_FILE, OPTION);
    trainedHmmFile = extract_parameter(argc, argv, TRAINED_HMM_FILE, OPTION);
    testedFile    = extract_parameter(argc, argv, TESTED_FILE, OPTION);
    mode = oatoi(extract_parameter(argc, argv, MODE, OPTION), TRAINING);

    /* check whether the mode is consistant with the provided files */
    check_mode(mode, trainFile, testFile);

    /* default values for trainedHmmFile and testedFile */
    if(trainedHmmFile == NULL)
        trainedHmmFile = defTrainedHmmFile;
    if(testedFile == NULL)
        testedFile = defTestedFile;

    /*---------- load HMM ----------*/
    loadHMM(hmmFile);

    /*---------- train the HMM model ----------*/
    if(mode == TRAINING || mode == TRAINING_TESTING)
    {
        /*---------- load training data ----------*/
        iTrain = cal_lines(trainFile);
        T = getLengthOfLongestLine(trainFile) + extraSpace;
        loadSeq(&trainData, trainFile);
        if(observationsDefined == FALSE)
        {
            readObservations(trainData);
        }
        /*---------- initialization ----------*/
        init();
        /*---------- train HMM ----------*/
        baumWelch();
        /*---------- save trained HMM ----------*/
        saveHmm(trainedHmmFile);
    }

    /*---------- test with the HMM model ----------*/
    if(mode == TESTING || mode == TRAINING_TESTING)
    {
        /*---------- 5. load testing data ----------*/
        if(mode == TESTING)
        {
            T = getLengthOfLongestLine(testFile) + extraSpace;
            init();
        }
        iTest = cal_lines(testFile);
        loadSeq(&testData, testFile);
        /*---------- test ----------*/
        test(testedFile);
    }
    /* post-processing */
    post_processing();

    return 0;
}

/************************************************************************
NAME
     post_processing - post processing after the main process

DESCRIPTION
     This function releases the allocated memery for the global variables.

     Input:
	   pointers to global variables.
	 Output:
	   None.
	 Global variable list:
	   transitions, emissions, pi, observations, alpha, beta, trainData,
	   testData.
*************************************************************************/
void post_processing()
{
    int i;

    /* free space for transition matrix */
    if(transitions != NULL)
    {
        for(i=0; i < N; i++)
        {
            if(transitions[i] != NULL) free(transitions[i]);
        }
        free(transitions);
    }
    /* free space for emission matrix */
    if(emissions != NULL)
    {
        for(i=0; i < N; i++)
        {
            if(emissions[i] != NULL) free(emissions[i]);
        }
        free(emissions);
    }
    /* free space for initial distribution vector */
    if(pi != NULL) free(pi);
    /* free space for observation list */
    if(observations != NULL) free(observations);
    /* free space for matrix alpha, used in forward algorithm */
    if(alpha != NULL)
    {
        for(i=0; i < T; i++)
        {
            if(alpha[i] != NULL) free(alpha[i]);
        }
        free(alpha);
    }
    /* free space for matrix beta, used in backward algorithm */
    if(beta != NULL)
    {
        for(i=0; i < T; i++)
        {
            if(beta[i] != NULL) free(beta[i]);
        }
        free(beta);
    }
    /* free space for training data */
    if(trainData != NULL)
    {
        for(i=0; i < iTrain; i++)
        {
            if(trainData[i] != NULL) free(trainData[i]);
        }
        free(trainData);
    }
    /* free space for testing data */
    if(testData != NULL)
    {
        for(i=0; i < iTest; i++)
        {
            if(testData[i] != NULL) free(testData[i]);
        }
        free(testData);
    }
}

/************************************************************************
NAME
     saveHmm - save the trained hidden Markov model in pre-defined format

DESCRIPTION
     This function saves the trained hidden Markov model in pre-defined
	 format.

     Input:
	   trained HMM & output file name
	 Output:
	   output file
	 Global variables:
	   N, M, T, transitions, emissions, pi.
*************************************************************************/
void saveHmm(char *out)
{
    FILE *fp;
    int i, j;

    /* Open for write (will fail if inputfile does not exist) */
    if( (fp  = fopen( out, "w" )) == NULL )
    {
        printf( "The file '%s' was not opened\n", out);
        exit(1);
    }
    fprintf(fp, "%d %d %d\n", N, M, T);
    fprintf(fp, "// Observation list\n");
    fprintf(fp, "O: ");
    for(i=0; i < M; i++)
    {
        fprintf(fp, "%c ", observations[i]);
    }
    fprintf(fp, "\n");
    /* transition matrix */
    fprintf(fp, "// Transition matrix\n");
    for(i=0; i < N; i++)
    {
        for(j=0; j < N; j++)
        {
            fprintf(fp, "%6.4f ", transitions[i][j]);
        }
        fputc('\n', fp);
    }
    /* emission matrix */
    fprintf(fp, "// Emission matrix\n");
    // emission matrix
    for(i=0; i < N; i++)
    {
        for(j=0; j < M; j++)
        {
            fprintf(fp, "%6.4f ", emissions[i][j]);
        }
        fputc('\n', fp);
    }
    /* initial state distribution */
    fprintf(fp, "// Initial state distribution\n");
    for(i=0; i < N; i++)
    {
        fprintf(fp, "%6.4f ", pi[i]);
    }
    fputc('\n', fp);

    fclose( fp );
}

/************************************************************************
NAME
     test - test the test file with given hidden Markov model

DESCRIPTION
     This function tests the test file with given hidden Markov model.

     Input:
	   trained HMM & tested file name
	 Output:
	   tested file
	 Global variables list:
	   T, iTest, testData, extraObservations.
*************************************************************************/
void test(char* testedFile)
{
    FILE *out_fp;
    int i;
    double prob;
    /* the most probable state path of the current observation sequence */
    char *buffer;
    /* the most probable state paths of all the observation sequences */
    double score;

    buffer = (char *) malloc(T * sizeof(char));

    /* Open for write (will fail if inputfile does not exist) */
    if( (out_fp  = fopen( testedFile, "w" )) == NULL )
    {
        printf( "The file '%s' was not opened\n", testedFile);
        exit(1);
    }

    for(i=0; i < iTest; i++)
    {
        prob = forward( testData[i] );
        /* score is equal to the natural logarithm of the probability
           divided by the length */
        score = log(prob) / (double)(strlen(testData[i]) - extraObservations);
        viterbi(testData[i], buffer);
        /* output probability and the most probable path */
        fprintf(out_fp, "%f\n%s\n%s\n", score, testData[i], buffer);
    }
    
    free( buffer );

    fclose( out_fp );
}

/************************************************************************
NAME
     getSymbol - get the symbol corresponding to each state

DESCRIPTION
     This function .. There are only 52 distinct symbols in this function.

     Input:
	   state number
	 Output:
	   corresponding symbol.
	 Global variables list:
	   None.
*************************************************************************/
char getSymbol(int k)
{
    char result;
    
    if(k >= 0 && k < 26)
        result = 'a' + k;
    else if(k >= 26 && k < 52)
        result = 'A' + k - 26;
    else
        result = '-';
    
    return result;
}

/************************************************************************
NAME
     getObservation - get the order of the specified observation

DESCRIPTION
     This function gets the order of the specified observation in the
	 observation list with binary search 

     Input:
	   specified observation
	 Output:
	   the order of the specified observation
	 Global variables list:
	   M.
*************************************************************************/
int getObservation(char ch)
{
    int start, end, middle;
    /* initial value */
    start = 0;
    end = M-1;
    middle= -1;
    //exception: the input observation is not in the observation list
    if(ch < observations[start] || ch > observations[end])
    {
        printf("the input observation is not in the observation list!\n");
        exit(1);
    }

    if(observations[start] == ch)
        return start;
    if(observations[end] == ch)
        return end;
    middle = (start + end)/2;

    /* why middle != start:
        if middle == start, it means end = start + 1, and 'ch' is a
        character which lies between observation 'start' and
        observation 'end' */
    while(observations[middle]!=ch && start < end && middle != start)
    {
        if(observations[middle] < ch)
            start = middle;
        else
            end = middle;

        middle = (start + end) / 2;
    }

    if(observations[middle] != ch)
    {
        printf("The char is not in observation list!\n");
        exit(1);
    }

    return middle;
}


/************************************************************************
NAME
     forward - forward algorithm

DESCRIPTION
     This function calculates the probability of the observation sequence
	 with forward algorithm given the HMM model.

     Input:
	   HMM model & the observation sequence
	 Output:
	   Probability
	 Global variables list:
	   N, alpha, pi, transitions, emissions.
*************************************************************************/
double forward(char *line)
{
    int i, j, t;
    int length = strlen( line );
    /* the order of the real observation in the observation set */
    int iObservation;
    /* transition probability to state i given the observation sequence */
    double sumTransProb;
    double prob;

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产午夜精品一区二区三区视频| 99视频精品在线| 欧美久久久久中文字幕| 亚洲综合成人在线视频| 91精品办公室少妇高潮对白| 一区二区三区欧美视频| 欧美日韩一卡二卡| 免费的国产精品| 久久久噜噜噜久噜久久综合| 国产精品99久久久久久有的能看| 欧美国产视频在线| 色欧美88888久久久久久影院| 亚洲一级电影视频| 91精品国产欧美一区二区18| 精东粉嫩av免费一区二区三区| 久久女同性恋中文字幕| 99在线精品一区二区三区| 一区二区三区在线影院| 欧美一区二区日韩| 成人免费毛片app| 亚洲一区在线视频| 欧美精品一区二区久久婷婷| 不卡视频一二三| 婷婷一区二区三区| 欧美v日韩v国产v| 91在线视频免费91| 日韩在线卡一卡二| 欧美激情一区在线观看| 欧美午夜一区二区三区| 国产制服丝袜一区| 亚洲欧美日韩国产一区二区三区 | 国产精品久久网站| 欧美亚洲国产一卡| 国产精品自拍三区| 亚洲欧美日韩国产另类专区| 日韩欧美一区电影| 97aⅴ精品视频一二三区| 青青草国产成人av片免费| 国产精品伦理在线| 777精品伊人久久久久大香线蕉| 国产精品综合视频| 午夜精品一区二区三区电影天堂 | 午夜精品免费在线| 久久色在线观看| 欧美写真视频网站| 成人黄色a**站在线观看| 日韩高清不卡一区二区| 1区2区3区欧美| 精品国产91乱码一区二区三区 | 一区二区欧美视频| 国产亚洲精品aa| 欧美一区二区三区日韩视频| 一本到一区二区三区| 国产精品一区二区你懂的| 丝袜脚交一区二区| 国产精品污www在线观看| 4hu四虎永久在线影院成人| 北条麻妃一区二区三区| 国产精品资源在线| 日本aⅴ精品一区二区三区| 一区二区三区成人在线视频| 日本一区二区成人| 26uuu亚洲综合色| 日韩亚洲欧美一区二区三区| 欧美日韩三级一区二区| 91麻豆福利精品推荐| 成人动漫在线一区| 国产乱淫av一区二区三区 | 麻豆国产欧美一区二区三区| 亚洲国产成人va在线观看天堂| 日韩毛片一二三区| 国产精品免费视频一区| 国产亚洲精久久久久久| 久久久久久久久久久久电影| 欧美mv日韩mv亚洲| 欧美一区二区三区电影| 欧美一区二区久久久| 欧美精品 日韩| 欧美精三区欧美精三区| 欧美视频精品在线| 欧美日韩在线免费视频| 欧美日韩aaa| 欧美麻豆精品久久久久久| 欧美日韩一区国产| 色欧美乱欧美15图片| 欧美视频一区二区三区在线观看| 在线观看欧美日本| 欧美日韩一区中文字幕| 宅男在线国产精品| 日韩久久免费av| 久久亚洲一区二区三区四区| 久久精品男人的天堂| 国产女同互慰高潮91漫画| 国产精品色呦呦| 亚洲精品videosex极品| 亚洲国产一区在线观看| 丝袜a∨在线一区二区三区不卡| 五月综合激情婷婷六月色窝| 六月丁香婷婷色狠狠久久| 国产综合久久久久久鬼色 | 欧美日韩三级一区| 日韩欧美国产wwwww| 国产亚洲综合在线| 国产精品久久久久aaaa| 亚洲一区二区三区四区在线 | 成人激情黄色小说| 色婷婷国产精品久久包臀| 欧美日韩一二区| 欧美不卡视频一区| 国产精品入口麻豆九色| 亚洲黄色在线视频| 蜜臀精品一区二区三区在线观看| 国产一区二区美女诱惑| 972aa.com艺术欧美| 欧美高清视频一二三区 | 欧美日韩午夜影院| 精品欧美一区二区在线观看| 国产精品福利一区| 日韩中文字幕亚洲一区二区va在线 | 日韩欧美你懂的| 亚洲国产精品二十页| 亚洲第一狼人社区| 国产激情偷乱视频一区二区三区 | 欧美性猛片aaaaaaa做受| 日韩免费一区二区三区在线播放| 国产精品看片你懂得| 亚洲国产综合在线| 国产91丝袜在线观看| 欧美日韩日日夜夜| 中文字幕一区三区| 久久电影网电视剧免费观看| 色偷偷久久人人79超碰人人澡| 欧美一级精品在线| 亚洲欧美日韩国产成人精品影院| 久久国产精品色| 在线一区二区三区四区五区 | 69堂成人精品免费视频| 中日韩av电影| 久久国产福利国产秒拍| 日本道精品一区二区三区| 久久久久久一级片| 日韩成人免费电影| 91国偷自产一区二区三区成为亚洲经典| 欧美成人一级视频| 午夜久久久久久久久| 91在线视频网址| 欧美激情一区二区三区在线| 男男gaygay亚洲| 欧美日韩美少妇| 亚洲欧美国产毛片在线| 成人视屏免费看| 精品国产凹凸成av人网站| 日韩国产精品久久| 欧美日韩亚洲综合在线| 最新热久久免费视频| 国产a精品视频| 亚洲精品一区二区三区四区高清| 视频一区二区三区中文字幕| 91久久线看在观草草青青| 136国产福利精品导航| 高清成人免费视频| 国产人成亚洲第一网站在线播放| 美女久久久精品| 欧美一区二区播放| 日韩国产欧美一区二区三区| 7777精品伊人久久久大香线蕉的| 亚洲一级二级在线| 精品视频在线看| 亚洲一二三四久久| 欧美性大战久久久久久久蜜臀| 亚洲伦理在线精品| 色婷婷久久久亚洲一区二区三区| 国产精品欧美一级免费| 成人精品电影在线观看| 国产精品久久久一本精品 | 中文字幕va一区二区三区| 国产精品一区在线观看乱码| 久久久91精品国产一区二区精品 | 国产成人午夜片在线观看高清观看| 精品国产一区久久| 国产一区二区91| 日本一区二区三区视频视频| 成人性色生活片免费看爆迷你毛片| 国产欧美一区二区三区沐欲| 成人激情av网| 一区二区三区蜜桃网| 欧美大肚乱孕交hd孕妇| 蜜臀久久99精品久久久久宅男| 日韩午夜激情电影| 国产高清无密码一区二区三区| 欧美国产国产综合| 91久久线看在观草草青青| 天天综合天天综合色| 精品久久久久久久久久久久包黑料| 国产一区二区三区高清播放| 国产精品欧美精品| 欧美日韩国产一区二区三区地区| 日韩精品乱码免费| 国产亚洲成年网址在线观看| 91在线国产福利|