Пример #1
0
		/// <summary>
		/// Specifies the training model
		/// </summary>
		/// <param name="s">The name of the training model</param>
		/// <returns>A <see cref="BPMSparseVarsForTrain"/> instance</returns>
		private BPMSparseVarsForTrain SpecifyTrainModel(string s)
		{
			// Place to maintain variables for each component
			BPMDataVars[] dataVars= new BPMDataVars[nClass];
			// The weight vector for each component
			VariableArray<double>[] w = new VariableArray<double>[nClass];
			// The prior weight distributions for each component
			VariableArray<Gaussian>[] wInit = new VariableArray<Gaussian>[nClass];
			for (int c = 0; c < nClass; c++) {
				// Weight variables are an array over feature range
				w[c] = Variable.Array<double>(feature).Named("w_" + c + s);
				// The prior weight distributions will be set by the calling program
				wInit[c] = Variable.Array<Gaussian>(feature).Named("wInit_" + c + s);
				// The weight for this component and feature
				w[c][feature] = Variable<double>.Random(wInit[c][feature]);
			}
			// Loop over the components
			for (int c = 0; c < nClass; c++) {
				Variable<int> nItem = Variable.New<int>().Named("nItem_" + c + s);
				Range item = new Range(nItem).Named("item_" + c + s);
				// Array of feature counts per item
				VariableArray<int> xValueCount = Variable.Array<int>(item).Named("xCount_" + c + s);
				// Range over features - size is based on variable feature count
				Range itemFeature = new Range(xValueCount[item]).Named("itemFeature_" + c + 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_" + c + s);
				// Jagged array of indices for the items
				VariableArray<VariableArray<int>, int[][]> xIndices = Variable.Array(Variable.Array<int>(itemFeature), item).Named("xIndices_" + c + s);
				// Loop over the data items
				using (Variable.ForEach(item)) {
					// The score for this item across all components
					Variable<double>[] score = BPMUtils.ComputeClassScores(w, xValues[item], xIndices[item], itemFeature, NoisePrec);
					// The constraint imposed by the observed component
					BPMUtils.ConstrainArgMax(c, score);
				}
				// Store the data information
				dataVars[c] = new BPMDataVars(nItem, item, xIndices, xValueCount, xValues);
			}
			// Store the variables 
			BPMSparseVarsForTrain bpmVar = new BPMSparseVarsForTrain();
			bpmVar.dataVars = dataVars;
			bpmVar.ie = new InferenceEngine();
			bpmVar.ie.ModelName = "BPMSparse_train";
			bpmVar.w = w;
			bpmVar.wInit = wInit;
			return bpmVar;
		}
Пример #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;
		}