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); }
/// <summary> /// This constructs a fresh vector that is sized correctly to accommodate all the known sparse values for vectors /// that are possibly sparse. /// </summary> /// <returns> /// a new, internally correctly sized ConcatVector that will work correctly as weights for features from /// this namespace; /// </returns> public virtual ConcatVector NewWeightsVector() { ConcatVector vector = new ConcatVector(featureToIndex.Count); foreach (string s in sparseFeatureIndex.Keys) { int size = sparseFeatureIndex[s].Count; vector.SetDenseComponent(EnsureFeature(s), new double[size]); } return(vector); }
public virtual void TestAddSparseToDense(double[] dense1, int sparseIndex, double v) { ConcatVector v1 = new ConcatVector(1); v1.SetDenseComponent(0, dense1); ConcatVector v2 = new ConcatVector(1); v2.SetSparseComponent(0, (int)sparseIndex, v); double expected = v1.DotProduct(v2) + 0.7f * (v2.DotProduct(v2)); v1.AddVectorInPlace(v2, 0.7f); NUnit.Framework.Assert.AreEqual(v1.DotProduct(v2), 5.0e-4, expected); }
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 virtual void TestAppendDenseComponent(double[] vector1, double[] vector2) { ConcatVector v1 = new ConcatVector(1); ConcatVector v2 = new ConcatVector(1); v1.SetDenseComponent(0, vector1); v2.SetDenseComponent(0, vector2); double sum = 0.0f; for (int i = 0; i < Math.Min(vector1.Length, vector2.Length); i++) { sum += vector1[i] * vector2[i]; } NUnit.Framework.Assert.AreEqual(v1.DotProduct(v2), 5.0e-4, sum); }
public virtual void TestElementwiseSparseToDense(double[] dense1, int sparseIndex, double v) { ConcatVector v1 = new ConcatVector(1); v1.SetDenseComponent(0, dense1); ConcatVector v2 = new ConcatVector(1); v2.SetSparseComponent(0, (int)sparseIndex, v); v1.ElementwiseProductInPlace(v2); for (int i = 0; i < dense1.Length; i++) { double expected = 0.0f; if (i == sparseIndex) { expected = dense1[i] * v; } NUnit.Framework.Assert.AreEqual(v1.GetValueAt(0, i), 5.0e-4, expected); } }
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); }
/// <summary> /// This adds a dense feature to a vector, setting the appropriate component of the given vector to the passed in /// value. /// </summary> /// <param name="vector">the vector</param> /// <param name="featureName">the feature whose value to set</param> /// <param name="value">the value we want to set this vector to</param> public virtual void SetDenseFeature(ConcatVector vector, string featureName, double[] value) { vector.SetDenseComponent(EnsureFeature(featureName), value); }