/// <summary> /// Constructs a Sparse Bayes point machine instance from number of components and number of features /// </summary> /// <param name="nClass">Number of components (classes)</param> /// <param name="nFeatures">Number of features</param> /// <param name="noisePrec">noisePrec</param> public BPMSparse(int nClass, int nFeatures, double noisePrec) { this.nClass = nClass; this.nFeatures = nFeatures; NoisePrec = noisePrec; feature = new Range(nFeatures).Named("feature"); 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="BPMSparseVarsForTrain"/> instance</returns> private BPMSparseVarsForTrain SpecifyTrainModel(string s) { // Place to maintain variables for each component BPMDataVars[] dataVars= new BPMDataVars[nClass]; // The weight vector for each component VariableArray<double>[] w = new VariableArray<double>[nClass]; // The prior weight distributions for each component VariableArray<Gaussian>[] wInit = new VariableArray<Gaussian>[nClass]; for (int c = 0; c < nClass; c++) { // Weight variables are an array over feature range w[c] = Variable.Array<double>(feature).Named("w_" + c + s); // The prior weight distributions will be set by the calling program wInit[c] = Variable.Array<Gaussian>(feature).Named("wInit_" + c + s); // The weight for this component and feature w[c][feature] = Variable<double>.Random(wInit[c][feature]); } // Loop over the components for (int c = 0; c < nClass; c++) { Variable<int> nItem = Variable.New<int>().Named("nItem_" + c + s); Range item = new Range(nItem).Named("item_" + c + s); // Array of feature counts per item VariableArray<int> xValueCount = Variable.Array<int>(item).Named("xCount_" + c + s); // Range over features - size is based on variable feature count Range itemFeature = new Range(xValueCount[item]).Named("itemFeature_" + c + s); // Jagged array of values - each item is an array of data values whose indices // are given by the corresping xIndices[item] VariableArray<VariableArray<double>, double[][]> xValues = Variable.Array(Variable.Array<double>(itemFeature), item).Named("xValues_" + c + s); // Jagged array of indices for the items VariableArray<VariableArray<int>, int[][]> xIndices = Variable.Array(Variable.Array<int>(itemFeature), item).Named("xIndices_" + c + s); // Loop over the data items using (Variable.ForEach(item)) { // The score for this item across all components Variable<double>[] score = BPMUtils.ComputeClassScores(w, xValues[item], xIndices[item], itemFeature, NoisePrec); // The constraint imposed by the observed component BPMUtils.ConstrainArgMax(c, score); } // Store the data information dataVars[c] = new BPMDataVars(nItem, item, xIndices, xValueCount, xValues); } // Store the variables BPMSparseVarsForTrain bpmVar = new BPMSparseVarsForTrain(); bpmVar.dataVars = dataVars; bpmVar.ie = new InferenceEngine(); bpmVar.ie.ModelName = "BPMSparse_train"; bpmVar.w = w; bpmVar.wInit = wInit; return bpmVar; }