/// <summary> /// Computes Backward probabilities for a given potential function and a set of observations. /// </summary> /// public static double[,] Backward(IPotentialFunction function, int[] observations) { int states = function.States; int T = observations.Length; double[,] bwd = new double[T, states]; // 1. Initialization for (int i = 0; i < states; i++) bwd[T - 1, i] = 1.0; // 2. Induction for (int t = T - 2; t >= 0; t--) { for (int i = 0; i < states; i++) { double sum = 0; for (int j = 0; j < states; j++) sum += function.Compute(i, j, observations, t) * bwd[t + 1, j]; bwd[t, i] += sum; } } return bwd; }
/// <summary> /// Computes Forward probabilities for a given potential function and a set of observations. /// </summary> /// public static double[,] Forward(IPotentialFunction function, int[] observations) { int states = function.States; int T = observations.Length; double[,] fwd = new double[T, states]; // 1. Initialization for (int j = 0; j < states; j++) fwd[0, j] = function.Compute(-1, j, observations, 0); // 2. Induction for (int t = 1; t < T; t++) { int obs = observations[t]; for (int j = 0; j < states; j++) { double sum = 0.0; for (int i = 0; i < states; i++) sum += function.Compute(i, j, observations, t) * fwd[t - 1, i]; fwd[t, j] = sum; } } return fwd; }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="factorIndex">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="dimensions">The number of dimensions for the multivariate observations.</param> /// <param name="edgeIndex">The index of the first edge feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="edgeCount">The number of edge features in this factor.</param> /// <param name="stateIndex">The index of the first state feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="stateCount">The number of state features in this factor.</param> /// public MultivariateNormalMarkovModelFactor(IPotentialFunction <double[]> owner, int states, int factorIndex, int dimensions, int edgeIndex, int edgeCount, int stateIndex, int stateCount) : base(owner, states, factorIndex, edgeIndex, edgeCount, stateIndex, stateCount) { this.dimensions = dimensions; }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// /// <returns> /// A new object that is a copy of this instance. /// </returns> /// public IFeature <double[]> Clone(IPotentialFunction <double[]> newOwner) { var clone = (MultivariateEmissionFeature)MemberwiseClone(); clone.Owner = newOwner; return(clone); }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// /// <returns> /// A new object that is a copy of this instance. /// </returns> /// public IFeature <double> Clone(IPotentialFunction <double> newOwner) { var clone = (SecondMomentFeature)MemberwiseClone(); clone.Owner = newOwner; return(clone); }
/// <summary> /// Constructs a state transition feature. /// </summary> /// /// <param name="owner">The potential function to which this feature belongs.</param> /// <param name="factorIndex">The index of the potential factor to which this feature belongs.</param> /// <param name="previous">The originating state.</param> /// <param name="current">The destination state.</param> /// public TransitionFeature(IPotentialFunction <T> owner, int factorIndex, int previous, int current) : base(owner, factorIndex) { this.previous = previous; this.current = current; }
/// <summary> /// Constructs a new symbol emission feature. /// </summary> /// /// <param name="owner">The potential function to which this feature belongs.</param> /// <param name="factorIndex">The index of the potential factor to which this feature belongs.</param> /// <param name="state">The state for the emission.</param> /// <param name="dimension">The dimension of the multidimensional /// observation this feature should respond to.</param> /// public MultivariateSecondMomentFeature(IPotentialFunction <double[]> owner, int factorIndex, int state, int dimension) : base(owner, factorIndex) { this.state = state; this.dimension = dimension; }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// /// <returns> /// A new object that is a copy of this instance. /// </returns> /// public IFeature <int> Clone(IPotentialFunction <int> newOwner) { var clone = (EmissionFeature)MemberwiseClone(); clone.Owner = newOwner; return(clone); }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// /// <returns> /// A new object that is a copy of this instance. /// </returns> /// public IFeature <T> Clone(IPotentialFunction <T> newOwner) { var clone = (OccupancyFeature <T>)MemberwiseClone(); clone.Owner = newOwner; return(clone); }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// /// <returns> /// A new object that is a copy of this instance. /// </returns> /// public FactorPotential <T> Clone(IPotentialFunction <T> newOwner) { var clone = (FactorPotential <T>) this.MemberwiseClone(); clone.Owner = newOwner; return(clone); }
private static double p(int previous, int next, T[] x, int t, double[,] fwd, double[,] bwd, IPotentialFunction <T> function) { // This is the probability of beginning at the start of the // sequence, evaluating the sequence until the instant at // which the label "previous" happened, computing a transition // from "previous" to "next" and then continuing until the end // of the sequence. double p = 1.0; // Evaluate until the instant t - 1 if (t > 0 && previous >= 0) { p *= fwd[t - 1, previous]; } else if (t == 0 && previous == -1) { p = 1.0; } else { return(0); } // Compute a transition from "previous" to "next" p *= Math.Exp(function.Factors[0].Compute(previous, next, x, t)); // Continue until the end of the sequence p *= bwd[t, next]; return(p); }
/// <summary> /// Constructs a new symbol emission feature. /// </summary> /// /// <param name="owner">The potential function to which this feature belongs.</param> /// <param name="factorIndex">The index of the potential factor to which this feature belongs.</param> /// <param name="state">The state for the emission.</param> /// <param name="symbol">The emission symbol.</param> /// public EmissionFeature(IPotentialFunction <int> owner, int factorIndex, int state, int symbol) : base(owner, factorIndex) { this.state = state; this.symbol = symbol; }
/// <summary> /// Computes Backward probabilities for a given potential function and a set of observations. /// </summary> /// public static double[,] Backward(IPotentialFunction function, int[] observations) { int states = function.States; int T = observations.Length; double[,] bwd = new double[T, states]; // 1. Initialization for (int i = 0; i < states; i++) { bwd[T - 1, i] = 1.0; } // 2. Induction for (int t = T - 2; t >= 0; t--) { for (int i = 0; i < states; i++) { double sum = 0; for (int j = 0; j < states; j++) { sum += function.Compute(i, j, observations, t) * bwd[t + 1, j]; } bwd[t, i] += sum; } } return(bwd); }
/// <summary> /// Computes Forward probabilities for a given potential function and a set of observations. /// </summary> /// public static double[,] Forward(IPotentialFunction function, int[] observations) { int states = function.States; int T = observations.Length; double[,] fwd = new double[T, states]; // 1. Initialization for (int j = 0; j < states; j++) { fwd[0, j] = function.Compute(-1, j, observations, 0); } // 2. Induction for (int t = 1; t < T; t++) { int obs = observations[t]; for (int j = 0; j < states; j++) { double sum = 0.0; for (int i = 0; i < states; i++) { sum += function.Compute(i, j, observations, t) * fwd[t - 1, i]; } fwd[t, j] = sum; } } return(fwd); }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="index">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="features">The number of features in this factor potential.</param> /// public FactorPotential(IPotentialFunction <T> owner, int index, int states, int features) { Owner = owner; ParameterIndex = index; FeatureCount = features; States = states; }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="factorIndex">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="symbols">The number of symbols in the discrete alphabet.</param> /// <param name="classIndex">The index of the first class label feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="classCount">The number of class label features in this factor.</param> /// <param name="edgeIndex">The index of the first edge feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="edgeCount">The number of edge features in this factor.</param> /// <param name="stateIndex">The index of the first state feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="stateCount">The number of state features in this factor.</param> /// public MarkovDiscreteFactor(IPotentialFunction <int> owner, int states, int factorIndex, int symbols, int edgeIndex, int edgeCount, int stateIndex, int stateCount, int classIndex = 0, int classCount = 0) : base(owner, states, factorIndex, edgeIndex, edgeCount, stateIndex, stateCount, classIndex, classCount) { this.Symbols = symbols; }
/// <summary> /// Constructs a new symbol emission feature. /// </summary> /// /// <param name="owner">The potential function to which this feature belongs.</param> /// <param name="factorIndex">The index of the potential factor to which this feature belongs.</param> /// <param name="state">The state for the emission.</param> /// <param name="symbol">The emission symbol.</param> /// <param name="dimension">The observation dimension this emission feature applies to.</param> /// public MultivariateEmissionFeature(IPotentialFunction <double[]> owner, int factorIndex, int state, int symbol, int dimension) : base(owner, factorIndex) { this.state = state; this.symbol = symbol; this.dimension = dimension; }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="factorIndex">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="stateTable">The lookup table of states where the independent distributions begin.</param> /// <param name="classIndex">The index of the first class label feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="classCount">The number of class label features in this factor.</param> /// <param name="edgeIndex">The index of the first edge feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="edgeCount">The number of edge features in this factor.</param> /// <param name="stateIndex">The index of the first state feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="stateCount">The number of state features in this factor.</param> /// public MarkovIndependentFactor(IPotentialFunction <double[]> owner, int states, int factorIndex, int[][] stateTable, int edgeIndex, int edgeCount, int stateIndex, int stateCount, int classIndex = 0, int classCount = 0) : base(owner, states, factorIndex, edgeIndex, edgeCount, stateIndex, stateCount, classIndex, classCount) { this.stateTable = stateTable; }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="factorIndex">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="edgeIndex">The index of the first edge feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="edgeCount">The number of edge features in this factor.</param> /// <param name="stateIndex">The index of the first state feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="stateCount">The number of state features in this factor.</param> /// <param name="classIndex">The index of the first class feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="classCount">The number of class features in this factor.</param> /// public FactorPotential(IPotentialFunction <T> owner, int states, int factorIndex, int edgeIndex, int edgeCount, int stateIndex, int stateCount, int classIndex = 0, int classCount = 0) : this(owner, states, factorIndex) { EdgeParameters = new ArraySegment <double>(owner.Weights, edgeIndex, edgeCount); StateParameters = new ArraySegment <double>(owner.Weights, stateIndex, stateCount); OutputParameters = new ArraySegment <double>(owner.Weights, classIndex, classCount); FactorParameters = new ArraySegment <double>(owner.Weights, Math.Min(Math.Min(edgeIndex, stateIndex), classIndex), edgeCount + stateCount + classCount); }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="factorIndex">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="classIndex">The index of the first class label feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="classCount">The number of class label features in this factor.</param> /// <param name="edgeIndex">The index of the first edge feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="edgeCount">The number of edge features in this factor.</param> /// <param name="stateIndex">The index of the first state feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="stateCount">The number of state features in this factor.</param> /// public NormalMarkovModelFactor(IPotentialFunction <double> owner, int states, int factorIndex, int classIndex, int classCount, int edgeIndex, int edgeCount, int stateIndex, int stateCount) : base(owner, states, factorIndex, edgeIndex, edgeCount, stateIndex, stateCount) { ClassParameterIndex = classIndex; ClassParameterCount = classCount; ParameterIndex = Math.Min(Math.Min(edgeIndex, stateIndex), classIndex); ParameterCount = edgeCount + stateCount + classCount; }
/// <summary> /// Initializes a new instance of the <see cref="ForwardBackwardGradient{T}"/> class. /// </summary> /// /// <param name="model">The model to be trained.</param> /// public ForwardBackwardGradient(HiddenConditionalRandomField<T> model) { this.model = model; this.function = model.Function; this.gradient = new ThreadLocal<double[]>(); this.lnZx = new ThreadLocal<double[]>(); this.lnZxy = new ThreadLocal<double[]>(); this.inputs = new ThreadLocal<T[][]>(); this.outputs = new ThreadLocal<int[]>(); this.logLikelihoods = new ThreadLocal<double[][]>(); this.error = new ThreadLocal<double>(); }
/// <summary> /// Initializes a new instance of the <see cref="BaseHiddenConditionalRandomFieldLearning<T>"/> class. /// </summary> /// /// <param name="model">The model to be trained.</param> /// protected BaseHiddenConditionalRandomFieldLearning(HiddenConditionalRandomField<T> model) { this.model = model; this.function = model.Function; this.gradient = new ThreadLocal<double[]>(); this.lnZx = new ThreadLocal<double[]>(); this.lnZxy = new ThreadLocal<double[]>(); this.inputs = new ThreadLocal<T[][]>(); this.outputs = new ThreadLocal<int[]>(); this.logLikelihoods = new ThreadLocal<double[][]>(); this.error = new ThreadLocal<double>(); }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="factorIndex">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="symbols">The number of symbols in the discrete alphabet.</param> /// <param name="classIndex">The index of the first class label feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="classCount">The number of class label features in this factor.</param> /// <param name="edgeIndex">The index of the first edge feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="edgeCount">The number of edge features in this factor.</param> /// <param name="stateIndex">The index of the first state feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="stateCount">The number of state features in this factor.</param> /// public DiscreteMarkovModelFactor(IPotentialFunction <int> owner, int states, int factorIndex, int symbols, int classIndex, int classCount, int edgeIndex, int edgeCount, int stateIndex, int stateCount) : base(owner, states, factorIndex, edgeIndex, edgeCount, stateIndex, stateCount) { this.Symbols = symbols; ClassParameterIndex = classIndex; ClassParameterCount = classCount; ParameterIndex = Math.Min(Math.Min(edgeIndex, stateIndex), classIndex); ParameterCount = edgeCount + stateCount + classCount; }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="factorIndex">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="dimensions">The number of dimensions for the multivariate observations.</param> /// <param name="classIndex">The index of the first class label feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="classCount">The number of class label features in this factor.</param> /// <param name="edgeIndex">The index of the first edge feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="edgeCount">The number of edge features in this factor.</param> /// <param name="stateIndex">The index of the first state feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="stateCount">The number of state features in this factor.</param> /// public MultivariateNormalMarkovModelFactor(IPotentialFunction <double[]> owner, int states, int factorIndex, int dimensions, int classIndex, int classCount, int edgeIndex, int edgeCount, int stateIndex, int stateCount) : base(owner, states, factorIndex, edgeIndex, edgeCount, stateIndex, stateCount) { this.dimensions = dimensions; ClassParameterIndex = classIndex; ClassParameterCount = classCount; ParameterIndex = Math.Min(Math.Min(edgeIndex, stateIndex), classIndex); ParameterCount = edgeCount + stateCount + classCount; }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="factorIndex">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="edgeIndex">The index of the first edge feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="edgeCount">The number of edge features in this factor.</param> /// <param name="stateIndex">The index of the first state feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="stateCount">The number of state features in this factor.</param> /// public FactorPotential(IPotentialFunction <T> owner, int states, int factorIndex, int edgeIndex, int edgeCount, int stateIndex, int stateCount) : this(owner, states, factorIndex) { EdgeParameterIndex = edgeIndex; EdgeParameterCount = edgeCount; EdgeParameterEnd = edgeIndex + edgeCount; StateParameterIndex = stateIndex; StateParameterCount = stateCount; StateParameterEnd = stateIndex + stateCount; ParameterIndex = Math.Min(edgeIndex, stateIndex); ParameterCount = edgeCount + stateCount; ParameterEnd = ParameterIndex + ParameterCount; }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="factorIndex">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="edgeIndex">The index of the first edge feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="edgeCount">The number of edge features in this factor.</param> /// <param name="stateIndex">The index of the first state feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="stateCount">The number of state features in this factor.</param> /// public NormalMarkovModelFactor(IPotentialFunction <double> owner, int states, int factorIndex, int edgeIndex, int edgeCount, int stateIndex, int stateCount) : base(owner, states, factorIndex, edgeIndex, edgeCount, stateIndex, stateCount) { }
private static double p(int previous, int next, int[] x, int t, double[,] fwd, double[,] bwd, IPotentialFunction function) { // This is the probability of beginning at the start of the // sequence, evaluating the sequence until the instant at // which the label "previous" happened, computing a transition // from "previous" to "next" and then continuing until the end // of the sequence. double p = 1.0; // Evaluate until the instant t - 1 if (t > 0 && previous >= 0) p *= fwd[t - 1, previous]; else if (t == 0 && previous == -1) p = 1.0; else return 0; // Compute a transition from "previous" to "next" p *= function.Compute(previous, next, x, t); // Continue until the end of the sequence p *= bwd[t, next]; return p; }
/// <summary> /// Constructs a new first moment emission feature. /// </summary> /// /// <param name="owner">The potential function to which this feature belongs.</param> /// <param name="factorIndex">The index of the potential factor to which this feature belongs.</param> /// <param name="state">The state for the emission.</param> /// public FirstMomentFeature(IPotentialFunction <double> owner, int factorIndex, int state) : base(owner, factorIndex) { this.state = state; }
/// <summary> /// Creates a new feature. /// </summary> /// /// <param name="owner">The potential function to which this feature belongs.</param> /// <param name="factorIndex">The index of the potential factor to which this feature belongs.</param> /// protected FeatureBase(IPotentialFunction <T> owner, int factorIndex) { this.Owner = owner; this.OwnerFactorIndex = factorIndex; }
/// <summary> /// Creates a new factor (clique) potential function. /// </summary> /// /// <param name="owner">The owner <see cref="IPotentialFunction{T}"/>.</param> /// <param name="states">The number of states in this clique potential.</param> /// <param name="factorIndex">The index of this factor potential in the <paramref name="owner"/>.</param> /// <param name="classIndex">The index of the first class label feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="classCount">The number of class label features in this factor.</param> /// <param name="edgeIndex">The index of the first edge feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="edgeCount">The number of edge features in this factor.</param> /// <param name="stateIndex">The index of the first state feature in the <paramref name="owner"/>'s parameter vector.</param> /// <param name="stateCount">The number of state features in this factor.</param> /// public MarkovNormalFactor(IPotentialFunction <double> owner, int states, int factorIndex, int edgeIndex, int edgeCount, int stateIndex, int stateCount, int classIndex = 0, int classCount = 0) : base(owner, states, factorIndex, edgeIndex, edgeCount, stateIndex, stateCount, classIndex, classCount) { }
/// <summary> /// Initializes a new instance of the <see cref="ConditionalRandomField"/> class. /// </summary> /// /// <param name="states">The number of states for the model.</param> /// <param name="function">The potential function to be used by the model.</param> /// public ConditionalRandomField(int states, IPotentialFunction function) { this.States = states; this.Function = function; }
/// <summary> /// Constructs a state occupancy feature. /// </summary> /// /// <param name="owner">The potential function to which this feature belongs.</param> /// <param name="factorIndex">The index of the potential factor to which this feature belongs.</param> /// <param name="state">The current state.</param> /// public OccupancyFeature(IPotentialFunction <T> owner, int factorIndex, int state) : base(owner, factorIndex) { this.current = state; }
/// <summary> /// Initializes a new instance of the <see cref="HiddenConditionalRandomField{T}"/> class. /// </summary> /// /// <param name="function">The potential function to be used by the model.</param> /// public HiddenConditionalRandomField(IPotentialFunction<T> function) { this.Function = function; }