示例#1
0
		/// <summary>
		/// Constructs a multi-component sparse Bayes Point Machine using shared variables for chunking data
		/// </summary>
		/// <param name="nClass">Number of components (classes)</param>
		/// <param name="featureCount">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 BPMSparse_Shared(int nClass, int featureCount, double noisePrec, int trainChunkSize, int testChunkSize)
		{
			nComponents = nClass;
			nFeatures = featureCount;
			NoisePrec = noisePrec;
			this.trainChunkSize = trainChunkSize;
			this.testChunkSize = testChunkSize;
			feature = new Range(nFeatures).Named("feature");
			w = new SharedVariableArray<double>[nComponents];
			IDistribution<double[]> wPrior0 = Distribution<double>.Array(nFeatures,
				delegate(int index) { return Gaussian.PointMass(0); });
			IDistribution<double[]> wPrior = Distribution<double>.Array(nFeatures,
				delegate(int index) { return Gaussian.FromMeanAndPrecision(0.0, 1.0); });
			for (int c = 0; c < nComponents; c++)
			{
				w[c] = (c == 0)
					? SharedVariable<double>.Random(feature, (DistributionStructArray<Gaussian,double>)wPrior0).Named("w_" + c)
					: SharedVariable<double>.Random(feature, (DistributionStructArray<Gaussian,double>)wPrior).Named("w_" + c);
			}

			trainModel = SpecifyTrainModel("_train", trainChunkSize);
			testModel = SpecifyTestModel("_test", testChunkSize);
		}
示例#2
0
		/// <summary>
		/// Specify the training model
		/// </summary>
		/// <param name="s">The name of the training model</param>
		/// <param name="nChunks">The number of chunks</param>
		/// <returns>A <see cref="BPMVarsModelForTrain"/> instance</returns>
		private BPMModelVarsForTrain SpecifyTrainModel(string s, int nChunks)
		{
			BPMDataVars[] dataVars = new BPMDataVars[nComponents];
			// The model identifier for the shared variables
			Model model = new Model(nChunks).Named("model" + s);
			// The weight vector within a submodel
			VariableArray<double>[] wModel = new VariableArray<double>[nComponents];
			for (int c = 0; c < nComponents; c++)
			{
				// Get a copy of the shared weight vector variable for the submodel
				wModel[c] = w[c].GetCopyFor(model).Named("wModel_" + c + s);
			}
			for (int c = 0; c < nComponents; c++)
			{
				Variable<int> nItem = Variable.New<int>().Named("nItem_" + c + s);
				Range item = new Range(nItem).Named("item_" + c + s);
				VariableArray<int> xValueCount = Variable.Array<int>(item).Named("xCount_" + c + s);
				Range itemFeature = new Range(xValueCount[item]).Named("itemFeature_" + c + s);
				VariableArray<VariableArray<double>, double[][]> xValues = Variable.Array(Variable.Array<double>(itemFeature), item).Named("xValues_" + c + s);
				VariableArray<VariableArray<int>, int[][]> xIndices = Variable.Array(Variable.Array<int>(itemFeature), item).Named("xIndices_" + c + s);
				using (Variable.ForEach(item))
				{
					// The score for this item across all components
					Variable<double>[] score =BPMUtils.ComputeClassScores(wModel, xValues[item], xIndices[item], itemFeature, NoisePrec);
					BPMUtils.ConstrainArgMax(c, score);
				}
				dataVars[c] = new BPMDataVars(nItem, item, xIndices, xValueCount, xValues);

			}

			BPMModelVarsForTrain bpmVar = new BPMModelVarsForTrain();
			bpmVar.ie = new InferenceEngine();
			bpmVar.dataVars = new BPMDataVars[nComponents];
			bpmVar.model = model;
			bpmVar.dataVars = dataVars;
			return bpmVar;
		}