/// <summary> /// Initializes a new instance of the <see cref="BPMSparseShared"/> class. /// </summary> /// <param name="numClasses">The number of classes.</param> /// <param name="noisePrecision">The precision of the noise.</param> /// <param name="numFeatures">The number of features.</param> /// <param name="numChunksTraining">The number of training set chunks.</param> /// <param name="numChunksTesting">The number of test set chunks.</param> public BPMSparseShared(int numClasses, double noisePrecision, int numFeatures, int numChunksTraining, int numChunksTesting) { // Ranges over classes and features. this.c = new Range(numClasses).Named("c"); this.f = new Range(numFeatures).Named("f"); // Setup weights and weights' prior. this.weightsPrior = InitializePrior(numClasses, numFeatures); this.weights = SharedVariable<double>.Random(Variable.Array<double>(this.f), this.c, this.weightsPrior).Named("w"); // Configure models. this.trainModel = new Model(this.weights, this.c, numChunksTraining); this.testModel = new Model(this.weights, this.c, numChunksTesting); // Observe the noise precision. this.trainModel.noisePrecision.ObservedValue = noisePrecision; this.testModel.noisePrecision.ObservedValue = noisePrecision; }
public Model(ISharedVariableArray<VariableArray<double>, double[][]> w, Range c, int numChunks) { // Items. numItems = Variable.New<int>().Named("numItems"); i = new Range(numItems).Named("i"); i.AddAttribute(new Sequential()); // Features per item. numFeaturesPerItem = Variable.Array<int>(i).Named("numFeaturesPerItem"); fItem = new Range(numFeaturesPerItem[i]).Named("fItem"); // The model identifier for the shared variables. model = new MicrosoftResearch.Infer.Models.Model(numChunks).Named("model"); // The weight vector for each submodel. wModel = w.GetCopyFor(model).Named("wModel"); noisePrecision = Variable.New<double>().Named("noisePrecision"); // Jagged array of feature values - each item is an array of data values // whose indices are given by the corresponding indices[i]. values = Variable.Array(Variable.Array<double>(fItem), i).Named("values"); // Jagged array of indices for the items. indices = Variable.Array(Variable.Array<int>(fItem), i).Named("indices"); // Labels. y = Variable.Array<int>(i).Named("y"); // For all items... using (Variable.ForEach(i)) { // ...compute the score of this item across all classes... score = BPMUtils.ComputeClassScores(wModel, values[i], indices[i], fItem, noisePrecision); y[i] = Variable.DiscreteUniform(c); // ... and constrain the output. BPMUtils.ConstrainMaximum(y[i], score); } // Inference engine settings (EP). engine.Compiler.UseSerialSchedules = true; engine.ShowProgress = false; }