/// <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 test model /// </summary> /// <param name="s">The name of the test model</param> /// <returns>A <see cref="BPMSparseVarsForTest"/> instance</returns> private BPMSparseVarsForTest SpecifyTestModel(string s) { // The weight vector for each component VariableArray<double>[] w = new VariableArray<double>[nClass]; // The prior distribution for weight vector for each component. When // <see cref="Test"/> is called, this is set to the posterior weight // distributions from <see cref="Train"/> VariableArray<Gaussian>[] wPrior = new VariableArray<Gaussian>[nClass]; for (int c = 0; c < nClass; c++) { // Weight variables are an array over feature range wPrior[c] = Variable.Array<Gaussian>(feature); w[c] = Variable.Array<double>(feature).Named("w" + c + s); // The weight for this component and feature w[c][feature] = Variable<double>.Random(wPrior[c][feature]); } // The number of items Variable<int> nItem = Variable.New<int>().Named("nItem_" +s); // Range over the number of items Range item = new Range(nItem).Named("item_" +s); // Array of feature counts per item VariableArray<int> xValueCount = Variable.Array<int>(item).Named("xCount_" + s); // Range over features - size is based on variable feature count Range itemFeature = new Range(xValueCount[item]).Named("itemFeature" + 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" + s); // Jagged array of indices for the items VariableArray<VariableArray<int>, int[][]> xIndices = Variable.Array(Variable.Array<int>(itemFeature), item).Named("xIndices" + s); VariableArray<int> ytest = Variable.Array<int>(item).Named("ytest" + s); using (Variable.ForEach(item)) { // The score for this item across all components Variable<double>[] score = BPMUtils.ComputeClassScores(w, xValues[item], xIndices[item], itemFeature, NoisePrec); ytest[item] = Variable.Discrete(Vector.Constant(nClass, 1.0 / nClass)); // The constraints on the output variable BPMUtils.ConstrainMaximum(ytest[item], score, nClass); } // Store the variables BPMSparseVarsForTest bpmVar = new BPMSparseVarsForTest(); bpmVar.ie = new InferenceEngine(); bpmVar.ie.ModelName = "BPMSparse_test"; bpmVar.dataVars = new BPMDataVars(nItem, item, xIndices, xValueCount, xValues); ; bpmVar.y = ytest; bpmVar.w = w; bpmVar.wPrior = wPrior; return bpmVar; }