Esempio n. 1
0
		/// <summary>
		/// Constructs a multi-component Bayes Point Machine using shared variables for chunking data
		/// </summary>
		/// <param name="nClass">Number of components (classes)</param>
		/// <param name="nFeatures">Number of features</param>
		/// <param name="noisePrec">Noise precision</param>
		/// <param name="trainChunkSize">Chunk size for training</param>
		/// <param name="testChunkSize">Chunk size for testing</param>
		public BPM_Shared(int nClass, int nFeatures, double noisePrec, int trainChunkSize, int testChunkSize)
		{
			this.nClass = nClass;
			this.nFeatures = nFeatures;
			this.trainChunkSize = trainChunkSize;
			this.testChunkSize = testChunkSize;
			NoisePrec = noisePrec;

			feature = new Range(nFeatures).Named("feature");

			// The set of weight vectors (one for each component) are shared between all data chunks
			w = new SharedVariable<Vector>[nClass];
			VectorGaussian wPrior0 = VectorGaussian.PointMass(Vector.Zero(nFeatures));
			VectorGaussian wPrior = VectorGaussian.FromMeanAndPrecision(Vector.Zero(nFeatures), PositiveDefiniteMatrix.Identity(nFeatures));
			for (int c = 0; c < nClass; c++)
			{
				w[c] = (c == 0)
					? SharedVariable<Vector>.Random(VectorGaussian.PointMass(Vector.Zero(nFeatures))).Named("w_" + c)
					: SharedVariable<Vector>.Random(wPrior).Named("w_" + c);
			}
			trainModel = SpecifyTrainModel("_train", trainChunkSize);
			testModel = SpecifyTestModel("_test", testChunkSize);
		}
Esempio n. 2
0
		/// <summary>
		/// Specify the training model
		/// </summary>
		/// <param name="s">The name of the test model</param>
		/// <param name="nChunks">The number of chunks</param>
		/// <returns>A <see cref="BPMVarsModelForTest"/> instance</returns>
		private BPMVarsModelForTest SpecifyTestModel(string s, int nChunks)
		{
			// 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 model identifier for the shared variables
			Model model = new Model(nChunks).Named("model" + s);
			// The weight vector for each submodel
			Variable<Vector>[] wModel = new Variable<Vector>[nClass];
			for (int c = 0; c < nClass; c++)
			{
				// Get a copy of the shared weight vector variable for the submodel
				wModel[c] = w[c].GetCopyFor(model).Named("wModel_" + c + s);
			}
			// Loop over 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(wModel, xValues[item], NoisePrec);
				// The constraints on the output variable
				ytest[item] = Variable.DiscreteUniform(nClass);
				BPMUtils.ConstrainMaximum(ytest[item], score, nClass);
			}
			// Store the variables 
			BPMVarsModelForTest bpmVar = new BPMVarsModelForTest();
			bpmVar.ie = new InferenceEngine();
			bpmVar.xValues = xValues;
			bpmVar.y = ytest;
			bpmVar.nItems = nItem;
			bpmVar.model = model;
			return bpmVar;
		}