Exemple #1
0
		/// <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");
		}
Exemple #2
0
		/// <summary>
		/// Specifies the test model
		/// </summary>
		/// <param name="s">The name of the test model</param>
		/// <returns>A <see cref="BPMVarsForTest"/> instance</returns>
		private BPMVarsForTest SpecifyTestModel(string s)
		{
			// The number of test items - this will be set by the calling program
			Variable<int> nItem = Variable.New<int>().Named("nItem" + s);
			// A range over the items
			Range item = new Range(nItem).Named("item" + s);
			// An array of feature vectors - their observed values will be
			// set by the calling program
			VariableArray<Vector> xValues = Variable.Array<Vector>(item);
			// The weight vectors for each component
			Variable<Vector>[] w = new Variable<Vector>[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"/>
			Variable<VectorGaussian>[] wPrior = new Variable<VectorGaussian>[nClass];
			// Loop over the classes
			for (int c = 0; c < nClass; c++)
			{
				// The priors will be set by the calling program
				wPrior[c] = Variable.New<VectorGaussian>();
				// The weights are sampled from the prior distributions
				w[c] = Variable<Vector>.Random(wPrior[c]);
			}
			// Loop over the data 
			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], NoisePrec);
				// The constraints on the output variable
				ytest[item] = Variable.DiscreteUniform(nClass);
				BPMUtils.ConstrainMaximum(ytest[item], score, nClass);
			}
			// Store the variables 
			BPMVarsForTest bpmVar = new BPMVarsForTest();
			bpmVar.ie = new InferenceEngine();
			bpmVar.xValues = xValues;
			bpmVar.y = ytest;
			bpmVar.nItems = nItem;
			bpmVar.w = w;
			bpmVar.wPrior = wPrior;
			return bpmVar;
		}