当前位置: 首页>>代码示例>>C++>>正文


C++ Arc::get_log_prob方法代码示例

本文整理汇总了C++中Arc::get_log_prob方法的典型用法代码示例。如果您正苦于以下问题:C++ Arc::get_log_prob方法的具体用法?C++ Arc::get_log_prob怎么用?C++ Arc::get_log_prob使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Arc的用法示例。


在下文中一共展示了Arc::get_log_prob方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: viterbi


//.........这里部分代码省略.........
    //      Log probabilities should be base e, i.e., natural
    //      logarithms.
    //
    //      Here is an example of the syntax for accessing a chart
    //      cell log prob:
    //
    //      logProb = chart(frmIdx, stateIdx).get_log_prob();
    //
    //      Here is an example of setting the contents of a chart cell:
    //
    //      chart(frmIdx, stateIdx).assign(logProb, arcId);
    //
    //  Fill in Viterbi algorithm here.
    //
    //  The code for calculating the final probability and
    //  the best path is provided for you below.




    // Start Viterbi algorithm
    
    // Allocate a matrix for storing the log probability for the each current state giving the previous state
    // G(cur_state,pre_state) = chart(frmIdx-1,pre_state)*transformpro*gmmProb
    matrix<double> G;
    G.resize(stateCnt,stateCnt);
    for(int rowIndex=0; rowIndex<stateCnt; ++rowIndex)
	for(int colIndex=0; colIndex<stateCnt; ++colIndex)
		G(rowIndex,colIndex) = g_zeroLogProb;
    vector<double> maxProb(stateCnt); // store the max value for each row of the G matrix
    vector<int> maxIndex(stateCnt); // store the Index of the max element for each row of the G matrix


    // Initialize start state i.e. chart(0,(0 .. stateCnt - 1))
    for(int stateIdx=0; stateIdx<stateCnt; ++stateIdx)
	if(stateIdx==graph.get_start_state())
		chart(0,stateIdx).assign(0,-1);
        else
		chart(0,stateIdx).assign(g_zeroLogProb,-1);
    // End


    for(int frmIdx=0; frmIdx<frmCnt; ++frmIdx){
	for(int stateIdx=0; stateIdx<stateCnt; ++stateIdx){
		int arcCnt = graph.get_arc_count(stateIdx); // Get number of outgoing arcs for the stateIdx
		int arcId = graph.get_first_arc_id(stateIdx); // Get arc ID of the first outgoing arc

		for(int arcIdx=0; arcIdx<arcCnt;++arcIdx)
		{
			Arc arc;
			arcId = graph.get_arc(arcId,arc);
			int dstState = arc.get_dst_state(); // get the destination state of the stateIdx
			double transProb = arc.get_log_prob();
			int gmmId = arc.get_gmm();
			double gmmProb = gmmProbs(frmIdx,gmmId);
			G(dstState,stateIdx) = chart(frmIdx, stateIdx).get_log_prob() + transProb + gmmProb;
		}
	}
	for(int curStaIdx=0; curStaIdx<stateCnt; ++curStaIdx){
		maxProb[curStaIdx] = g_zeroLogProb;
		maxIndex[curStaIdx] = -1;
		for(int preStaIdx=0; preStaIdx<stateCnt; ++preStaIdx)
			{
				if(G(curStaIdx,preStaIdx) > maxProb[curStaIdx]){
					maxProb[curStaIdx] = G(curStaIdx,preStaIdx);
					int arcCnt = graph.get_arc_count(preStaIdx); // Get number of outgoing arcs for the stateIdx
					int arcId = graph.get_first_arc_id(preStaIdx); // Get arc ID of the first outgoing arc
					for(int arcIdx=0; arcIdx<arcCnt;++arcIdx)
						{
							Arc arc;
							arcId = graph.get_arc(arcId,arc);
							int dstState = arc.get_dst_state(); // get the destination state of the stateIdx
							if(dstState==curStaIdx)
								maxIndex[curStaIdx] = arcId-1;
						}
				}
			}
		
        }


	// assign the chart(frmIdx+1,0 .. stateCnt - 1)
	for(int stateIdx=0; stateIdx<stateCnt; ++stateIdx){
		chart(frmIdx+1,stateIdx).assign(maxProb[stateIdx],maxIndex[stateIdx]);
	}
	// clear the matrix G and the vectors
	for(int rowIndex=0; rowIndex<stateCnt; ++rowIndex)
		for(int colIndex=0; colIndex<stateCnt; ++colIndex)
			G(rowIndex,colIndex) = g_zeroLogProb;
	for(int i=0; i<stateCnt; ++i){
		maxProb[i] = g_zeroLogProb;
		maxIndex[i] = -1;
	}

    }
    //  END_LAB
    //  The code for calculating the final probability and  
    //  the best path is provided for you.
    return viterbi_backtrace(graph, chart, outLabelList, doAlign);
    }
开发者ID:CarpLY,项目名称:e6870,代码行数:101,代码来源:lab2_vit.C


注:本文中的Arc::get_log_prob方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。