本文整理汇总了C#中System.Array.Max方法的典型用法代码示例。如果您正苦于以下问题:C# Array.Max方法的具体用法?C# Array.Max怎么用?C# Array.Max使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Array
的用法示例。
在下文中一共展示了Array.Max方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: run
private double run(Array[] observations)
{
// Baum-Welch algorithm.
// The Baum–Welch algorithm is a particular case of a generalized expectation-maximization
// (GEM) algorithm. It can compute maximum likelihood estimates and posterior mode estimates
// for the parameters (transition and emission probabilities) of an HMM, when given only
// emissions as training data.
// The algorithm has two steps:
// - Calculating the forward probability and the backward probability for each HMM state;
// - On the basis of this, determining the frequency of the transition-emission pair values
// and dividing it by the probability of the entire string. This amounts to calculating
// the expected count of the particular transition-emission pair. Each time a particular
// transition is found, the value of the quotient of the transition divided by the probability
// of the entire string goes up, and this value can then be made the new value of the transition.
// Grab model information
int states = model.States;
var logA = model.Transitions;
var logP = model.Probabilities;
// Initialize the algorithm
int N = observations.Length;
double logN = Math.Log(N);
LogKsi = new double[N][][,];
LogGamma = new double[N][,];
for (int i = 0; i < observations.Length; i++)
{
int T = observations[i].Length;
LogKsi[i] = new double[T][,];
LogGamma[i] = new double[T, states];
for (int t = 0; t < LogKsi[i].Length; t++)
LogKsi[i][t] = new double[states, states];
}
bool stop = false;
int TMax = observations.Max(x => x.Length);
double[,] lnFwd = new double[TMax, states];
double[,] lnBwd = new double[TMax, states];
// Initialize the model log-likelihoods
double newLogLikelihood = Double.NegativeInfinity;
convergence.NewValue = Double.NegativeInfinity;
int itersLeft = 30;
do // Until convergence or max iterations is reached
{
itersLeft--;
// For each sequence in the observations input
for (int i = 0; i < observations.Length; i++)
{
int T = observations[i].Length;
double[,] logGamma = LogGamma[i];
double w = LogWeights[i];
// 1st step - Calculating the forward probability and the
// backward probability for each HMM state.
ComputeForwardBackward(i, lnFwd, lnBwd);
// 2nd step - Determining the frequency of the transition-emission pair values
// and dividing it by the probability of the entire string.
// Calculate gamma values for next computations
for (int t = 0; t < T; t++)
{
double lnsum = Double.NegativeInfinity;
for (int k = 0; k < states; k++)
{
logGamma[t, k] = lnFwd[t, k] + lnBwd[t, k] + w;
lnsum = Special.LogSum(lnsum, logGamma[t, k]);
}
// System.Diagnostics.Debug.Assert(!Double.IsNaN(lnsum));
// Normalize if different from zero
if (lnsum != Double.NegativeInfinity)
for (int k = 0; k < states; k++)
logGamma[t, k] = logGamma[t, k] - lnsum;
}
// Calculate ksi values for next computations
ComputeKsi(i, lnFwd, lnBwd);
// Compute log-likelihood for the given sequence
for (int j = 0; j < states; j++)
newLogLikelihood = Special.LogSum(newLogLikelihood, lnFwd[T - 1, j]);
}
// Average the likelihood for all sequences
newLogLikelihood /= observations.Length;
convergence.NewValue = newLogLikelihood;
// Check for convergence
if (!convergence.HasConverged)
{
//.........这里部分代码省略.........