/// <summary> /// Constructs an instance from number of components and number of features /// </summary> /// <param name="nClass">Number of classes</param> /// <param name="nFeatures">Number of features</param> /// <param name="noisePrec">Noise precision</param> public BPM(int nClass, int nFeatures, double noisePrec) { this.nClass = nClass; this.nFeatures = nFeatures; NoisePrec = noisePrec; trainModel = SpecifyTrainModel("_train"); testModel = SpecifyTestModel("_test"); }
/// <summary> /// Specifies the training model /// </summary> /// <param name="s">The name of the training model</param> /// <returns>A <see cref="BPMVarsForTrain"/> instance</returns> private BPMVarsForTrain SpecifyTrainModel(string s) { // An array of feature vectors - their observed values will be // set by the calling program VariableArray<Vector>[] xValues = new VariableArray<Vector>[nClass]; // The number of items for each component Variable<int>[] nItem = new Variable<int>[nClass]; // Ranges over the items for each component Range[] item = new Range[nClass]; // The weight vector for each component Variable<Vector>[] w = new Variable<Vector>[nClass]; // The prior weight distributions for each component Variable<VectorGaussian>[] wInit = new Variable<VectorGaussian>[nClass]; for (int c = 0; c < nClass; c++) { // The prior distributions will be set by the calling program wInit[c] = Variable.New<VectorGaussian>(); // The weight vectors are drawn from the prior distribution w[c] = Variable<Vector>.Random(wInit[c]); } // Loop over the components for (int c = 0; c < nClass; c++) { // The number of items for each component - set by the calling program nItem[c] = Variable.New<int>().Named("nItem_" + c + s); // The item range for each component item[c] = new Range(nItem[c]).Named("item_" + c + s); // The items for each component - set by the calling program xValues[c] = Variable.Array<Vector>(item[c]); // Loop over the items using (Variable.ForEach(item[c])) { // The score for this item across all components Variable<double>[] score = BPMUtils.ComputeClassScores(w, xValues[c][item[c]], NoisePrec); // The constraint imposed by the observed component BPMUtils.ConstrainArgMax(c, score); } } // Store the variables BPMVarsForTrain bpmVar = new BPMVarsForTrain(); bpmVar.ie = new InferenceEngine(); bpmVar.xValues = xValues; bpmVar.nItems = nItem; bpmVar.w = w; bpmVar.wInit = wInit; return bpmVar; }