public override TableFactor Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { int numNeighbors = sourceOfRandomness.NextInt(1, 3); int[] neighbors = new int[numNeighbors]; int[] dimensions = new int[numNeighbors]; ICollection<int> usedNeighbors = new HashSet<int>(); for (int i = 0; i < neighbors.Length; i++) { while (true) { int neighbor = sourceOfRandomness.NextInt(0, 3); if (!usedNeighbors.Contains(neighbor)) { usedNeighbors.Add(neighbor); neighbors[i] = neighbor; dimensions[i] = variableSizes[neighbor]; break; } } } // Make sure we get some all-0 factor tables double multiple = sourceOfRandomness.NextDouble(); TableFactor factor = new TableFactor(neighbors, dimensions); foreach (int[] assignment in factor) { factor.SetAssignmentValue(assignment, multiple * sourceOfRandomness.NextDouble()); } return factor; }
private IDictionary <string, string> GenerateMetaData(SourceOfRandomness sourceOfRandomness, IDictionary <string, string> metaData) { int numPairs = sourceOfRandomness.NextInt(9); for (int i = 0; i < numPairs; i++) { int key = sourceOfRandomness.NextInt(); int value = sourceOfRandomness.NextInt(); metaData["key:" + key] = "value:" + value; } return(metaData); }
public override NDArrayTest.NDArrayWithGold <double> Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { NDArrayTest.NDArrayWithGold <double> testPair = new NDArrayTest.NDArrayWithGold <double>(); int numDimensions = sourceOfRandomness.NextInt(1, 5); int[] dimensions = new int[numDimensions]; for (int i = 0; i < dimensions.Length; i++) { dimensions[i] = sourceOfRandomness.NextInt(1, 4); } testPair.array = new NDArray <double>(dimensions); RecursivelyFillArray(new List <int>(), testPair, sourceOfRandomness); return(testPair); }
public override ConcatVectorTest.DenseTestVector Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { int length = sourceOfRandomness.NextInt(10); double[][] trueValues = new double[length][]; bool[] sparse = new bool[length]; int[] sizes = new int[length]; // Generate sizes in advance, so we can pass the clues on to the constructor for the multivector for (int i = 0; i < length; i++) { bool isSparse = sourceOfRandomness.NextBoolean(); sparse[i] = isSparse; if (isSparse) { sizes[i] = -1; } else { int componentLength = sourceOfRandomness.NextInt(SparseVectorLength); sizes[i] = componentLength; } } ConcatVector mv = new ConcatVector(length); for (int i_1 = 0; i_1 < length; i_1++) { if (sparse[i_1]) { trueValues[i_1] = new double[SparseVectorLength]; int sparseIndex = sourceOfRandomness.NextInt(SparseVectorLength); double sparseValue = sourceOfRandomness.NextDouble(); trueValues[i_1][sparseIndex] = sparseValue; mv.SetSparseComponent(i_1, sparseIndex, sparseValue); } else { trueValues[i_1] = new double[sizes[i_1]]; // Ensure we have some null components in our generated vector if (sizes[i_1] > 0) { for (int j = 0; j < sizes[i_1]; j++) { trueValues[i_1][j] = sourceOfRandomness.NextDouble(); } mv.SetDenseComponent(i_1, trueValues[i_1]); } } } return(new ConcatVectorTest.DenseTestVector(trueValues, mv)); }
public override IDictionary <int, int> Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { int numFeatures = sourceOfRandomness.NextInt(1, 15); IDictionary <int, int> featureMap = new Dictionary <int, int>(); for (int i = 0; i < numFeatures; i++) { int featureValue = sourceOfRandomness.NextInt(20); while (featureMap.Contains(featureValue)) { featureValue = sourceOfRandomness.NextInt(20); } featureMap[featureValue] = sourceOfRandomness.NextInt(2); } return(featureMap); }
public override GraphicalModel Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { GraphicalModel model = new GraphicalModel(); // Create the variables and factors int[] variableSizes = new int[20]; for (int i = 0; i < 20; i++) { variableSizes[i] = sourceOfRandomness.NextInt(1, 5); } int numFactors = sourceOfRandomness.NextInt(12); for (int i_1 = 0; i_1 < numFactors; i_1++) { int[] neighbors = new int[sourceOfRandomness.NextInt(1, 3)]; int[] neighborSizes = new int[neighbors.Length]; for (int j = 0; j < neighbors.Length; j++) { neighbors[j] = sourceOfRandomness.NextInt(20); neighborSizes[j] = variableSizes[neighbors[j]]; } ConcatVectorTable table = new ConcatVectorTable(neighborSizes); foreach (int[] assignment in table) { int numComponents = sourceOfRandomness.NextInt(7); // Generate a vector ConcatVector v = new ConcatVector(numComponents); for (int x = 0; x < numComponents; x++) { if (sourceOfRandomness.NextBoolean()) { v.SetSparseComponent(x, sourceOfRandomness.NextInt(32), sourceOfRandomness.NextDouble()); } else { double[] val = new double[sourceOfRandomness.NextInt(12)]; for (int y = 0; y < val.Length; y++) { val[y] = sourceOfRandomness.NextDouble(); } v.SetDenseComponent(x, val); } } // set vec in table table.SetAssignmentValue(assignment, null); } model.AddFactor(table, neighbors); } // Add metadata to the variables, factors, and model GenerateMetaData(sourceOfRandomness, model.GetModelMetaDataByReference()); for (int i_2 = 0; i_2 < 20; i_2++) { GenerateMetaData(sourceOfRandomness, model.GetVariableMetaDataByReference(i_2)); } foreach (GraphicalModel.Factor factor in model.factors) { GenerateMetaData(sourceOfRandomness, factor.GetMetaDataByReference()); } return(model); }
public override TableFactorTest.PartiallyObservedConstructorData Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { int len = sourceOfRandomness.NextInt(1, 4); ICollection <int> taken = new HashSet <int>(); int[] neighborIndices = new int[len]; int[] dimensions = new int[len]; int[] observations = new int[len]; int numObserved = 0; for (int i = 0; i < len; i++) { int j = sourceOfRandomness.NextInt(8); while (taken.Contains(j)) { j = sourceOfRandomness.NextInt(8); } taken.Add(j); neighborIndices[i] = j; dimensions[i] = sourceOfRandomness.NextInt(1, 3); if (sourceOfRandomness.NextBoolean() && numObserved + 1 < dimensions.Length) { observations[i] = sourceOfRandomness.NextInt(dimensions[i]); numObserved++; } else { observations[i] = -1; } } ConcatVectorTable t = new ConcatVectorTable(dimensions); TableFactorTest.ConcatVectorGenerator gen = new TableFactorTest.ConcatVectorGenerator(typeof(ConcatVector)); foreach (int[] assn in t) { ConcatVector vec = gen.Generate(sourceOfRandomness, generationStatus); t.SetAssignmentValue(assn, null); } TableFactorTest.PartiallyObservedConstructorData data = new TableFactorTest.PartiallyObservedConstructorData(); data.factor = new GraphicalModel.Factor(t, neighborIndices); data.observations = observations; return(data); }
public override ModelBatch Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { int length = sourceOfRandomness.NextInt(0, 50); ModelBatch batch = new ModelBatch(); for (int i = 0; i < length; i++) { batch.Add(modelGenerator.Generate(sourceOfRandomness, generationStatus)); } return(batch); }
///////////////////////////////////////////////////////////////////////////// // // A copy of these generators exists in GradientSourceTest in the learning module. If any bug fixes are made here, // remember to update that code as well by copy-paste. // ///////////////////////////////////////////////////////////////////////////// public override ConcatVector Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { ConcatVector v = new ConcatVector(ConcatVecComponents); for (int x = 0; x < ConcatVecComponents; x++) { if (sourceOfRandomness.NextBoolean()) { v.SetSparseComponent(x, sourceOfRandomness.NextInt(ConcatVecComponentLength), sourceOfRandomness.NextDouble()); } else { double[] val = new double[sourceOfRandomness.NextInt(ConcatVecComponentLength)]; for (int y = 0; y < val.Length; y++) { val[y] = sourceOfRandomness.NextDouble(); } v.SetDenseComponent(x, val); } } return(v); }
public override GraphicalModel Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { GraphicalModel model = new GraphicalModel(); // Create the variables and factors. These are deliberately tiny so that the brute force approach is tractable int[] variableSizes = new int[8]; for (int i = 0; i < variableSizes.Length; i++) { variableSizes[i] = sourceOfRandomness.NextInt(1, 3); } // Traverse in a randomized BFS to ensure the generated graph is a tree GenerateCliques(variableSizes, new List <int>(), new HashSet <int>(), model, sourceOfRandomness); // Add metadata to the variables, factors, and model GenerateMetaData(sourceOfRandomness, model.GetModelMetaDataByReference()); for (int i_1 = 0; i_1 < 20; i_1++) { GenerateMetaData(sourceOfRandomness, model.GetVariableMetaDataByReference(i_1)); } foreach (GraphicalModel.Factor factor in model.factors) { GenerateMetaData(sourceOfRandomness, factor.GetMetaDataByReference()); } // Observe a few of the variables foreach (GraphicalModel.Factor f in model.factors) { for (int i_2 = 0; i_2 < f.neigborIndices.Length; i_2++) { if (sourceOfRandomness.NextDouble() > 0.8) { int obs = sourceOfRandomness.NextInt(f.featuresTable.GetDimensions()[i_2]); model.GetVariableMetaDataByReference(f.neigborIndices[i_2])[CliqueTree.VariableObservedValue] = string.Empty + obs; } } } return(model); }
public override GraphicalModel[] Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { GraphicalModel[] dataset = new GraphicalModel[sourceOfRandomness.NextInt(1, 10)]; for (int i = 0; i < dataset.Length; i++) { dataset[i] = modelGenerator.Generate(sourceOfRandomness, generationStatus); foreach (GraphicalModel.Factor f in dataset[i].factors) { for (int j = 0; j < f.neigborIndices.Length; j++) { int n = f.neigborIndices[j]; int dim = f.featuresTable.GetDimensions()[j]; dataset[i].GetVariableMetaDataByReference(n)[LogLikelihoodDifferentiableFunction.VariableTrainingValue] = string.Empty + sourceOfRandomness.NextInt(dim); } } } return(dataset); }
public override ConcatVector[][][] Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { int l = sourceOfRandomness.NextInt(10) + 1; int m = sourceOfRandomness.NextInt(10) + 1; int n = sourceOfRandomness.NextInt(10) + 1; ConcatVector[][][] factor3 = new ConcatVector[l][][]; for (int i = 0; i < factor3.Length; i++) { for (int j = 0; j < factor3[0].Length; j++) { for (int k = 0; k < factor3[0][0].Length; k++) { int numComponents = sourceOfRandomness.NextInt(7); ConcatVector v = new ConcatVector(numComponents); for (int x = 0; x < numComponents; x++) { if (sourceOfRandomness.NextBoolean()) { v.SetSparseComponent(x, sourceOfRandomness.NextInt(32), sourceOfRandomness.NextDouble()); } else { double[] val = new double[sourceOfRandomness.NextInt(12)]; for (int y = 0; y < val.Length; y++) { val[y] = sourceOfRandomness.NextDouble(); } v.SetDenseComponent(x, val); } } factor3[i][j][k] = v; } } } return(factor3); }
private void GenerateCliques(int[] variableSizes, IList <int> startSet, ICollection <int> alreadyRepresented, GraphicalModel model, SourceOfRandomness randomness) { if (alreadyRepresented.Count == variableSizes.Length) { return; } // Generate the clique variable set IList <int> cliqueContents = new List <int>(); Sharpen.Collections.AddAll(cliqueContents, startSet); Sharpen.Collections.AddAll(alreadyRepresented, startSet); while (true) { if (alreadyRepresented.Count == variableSizes.Length) { break; } if (cliqueContents.Count == 0 || randomness.NextDouble(0, 1) < 0.7) { int gen; do { gen = randomness.NextInt(variableSizes.Length); }while (alreadyRepresented.Contains(gen)); alreadyRepresented.Add(gen); cliqueContents.Add(gen); } else { break; } } // Create the actual table int[] neighbors = new int[cliqueContents.Count]; int[] neighborSizes = new int[neighbors.Length]; for (int j = 0; j < neighbors.Length; j++) { neighbors[j] = cliqueContents[j]; neighborSizes[j] = variableSizes[neighbors[j]]; } ConcatVectorTable table = new ConcatVectorTable(neighborSizes); foreach (int[] assignment in table) { // Generate a vector ConcatVector v = new ConcatVector(ConcatVecComponents); for (int x = 0; x < ConcatVecComponents; x++) { if (randomness.NextBoolean()) { v.SetSparseComponent(x, randomness.NextInt(32), randomness.NextDouble()); } else { double[] val = new double[randomness.NextInt(12)]; for (int y = 0; y < val.Length; y++) { val[y] = randomness.NextDouble(); } v.SetDenseComponent(x, val); } } // set vec in table table.SetAssignmentValue(assignment, null); } model.AddFactor(table, neighbors); // Pick the number of children IList <int> availableVariables = new List <int>(); Sharpen.Collections.AddAll(availableVariables, cliqueContents); availableVariables.RemoveAll(startSet); int numChildren = randomness.NextInt(0, availableVariables.Count); if (numChildren == 0) { return; } IList <IList <int> > children = new List <IList <int> >(); for (int i = 0; i < numChildren; i++) { children.Add(new List <int>()); } // divide up the shared variables across the children int cursor = 0; while (true) { if (availableVariables.Count == 0) { break; } if (children[cursor].Count == 0 || randomness.NextBoolean()) { int gen = randomness.NextInt(availableVariables.Count); children[cursor].Add(availableVariables[gen]); availableVariables.Remove(availableVariables[gen]); } else { break; } cursor = (cursor + 1) % numChildren; } foreach (IList <int> shared1 in children) { foreach (int i_1 in shared1) { foreach (IList <int> shared2 in children) { System.Diagnostics.Debug.Assert((shared1 == shared2 || !shared2.Contains(i_1))); } } } foreach (IList <int> shared in children) { if (shared.Count > 0) { GenerateCliques(variableSizes, shared, alreadyRepresented, model, randomness); } } }
public override GraphicalModel Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { GraphicalModel model = new GraphicalModel(); // Create the variables and factors. These are deliberately tiny so that the brute force approach is tractable int[] variableSizes = new int[8]; for (int i = 0; i < variableSizes.Length; i++) { variableSizes[i] = sourceOfRandomness.NextInt(1, 3); } // Traverse in a randomized BFS to ensure the generated graph is a tree if (sourceOfRandomness.NextBoolean()) { GenerateCliques(variableSizes, new List <int>(), new HashSet <int>(), model, sourceOfRandomness); } else { // Or generate a linear chain CRF, because our random BFS doesn't generate these very often, and they're very // common in practice, so worth testing densely for (int i_1 = 0; i_1 < variableSizes.Length; i_1++) { // Add unary factor GraphicalModel.Factor unary = model.AddFactor(new int[] { i_1 }, new int[] { variableSizes[i_1] }, null); // "Cook" the randomly generated feature vector thunks, so they don't change as we run the system foreach (int[] assignment in unary.featuresTable) { ConcatVector randomlyGenerated = unary.featuresTable.GetAssignmentValue(assignment).Get(); unary.featuresTable.SetAssignmentValue(assignment, null); } // Add binary factor if (i_1 < variableSizes.Length - 1) { GraphicalModel.Factor binary = model.AddFactor(new int[] { i_1, i_1 + 1 }, new int[] { variableSizes[i_1], variableSizes[i_1 + 1] }, null); // "Cook" the randomly generated feature vector thunks, so they don't change as we run the system foreach (int[] assignment_1 in binary.featuresTable) { ConcatVector randomlyGenerated = binary.featuresTable.GetAssignmentValue(assignment_1).Get(); binary.featuresTable.SetAssignmentValue(assignment_1, null); } } } } // Add metadata to the variables, factors, and model GenerateMetaData(sourceOfRandomness, model.GetModelMetaDataByReference()); for (int i_2 = 0; i_2 < 20; i_2++) { GenerateMetaData(sourceOfRandomness, model.GetVariableMetaDataByReference(i_2)); } foreach (GraphicalModel.Factor factor in model.factors) { GenerateMetaData(sourceOfRandomness, factor.GetMetaDataByReference()); } // Observe a few of the variables foreach (GraphicalModel.Factor f in model.factors) { for (int i_1 = 0; i_1 < f.neigborIndices.Length; i_1++) { if (sourceOfRandomness.NextDouble() > 0.8) { int obs = sourceOfRandomness.NextInt(f.featuresTable.GetDimensions()[i_1]); model.GetVariableMetaDataByReference(f.neigborIndices[i_1])[CliqueTree.VariableObservedValue] = string.Empty + obs; } } } return(model); }