示例#1
0
        public virtual void TestElementwiseProduct(ConcatVectorTest.DenseTestVector d1, ConcatVectorTest.DenseTestVector d2)
        {
            for (int i = 0; i < d1.values.Length; i++)
            {
                for (int j = 0; j < d1.values[i].Length; j++)
                {
                    Assume.AssumeTrue(d1.values[i][j] == d1.vector.GetValueAt(i, j));
                }
            }
            for (int i_1 = 0; i_1 < d2.values.Length; i_1++)
            {
                for (int j = 0; j < d2.values[i_1].Length; j++)
                {
                    Assume.AssumeTrue(d2.values[i_1][j] == d2.vector.GetValueAt(i_1, j));
                }
            }
            ConcatVector clone = d1.vector.DeepClone();

            clone.ElementwiseProductInPlace(d2.vector);
            for (int i_2 = 0; i_2 < d1.values.Length; i_2++)
            {
                for (int j = 0; j < d1.values[i_2].Length; j++)
                {
                    double val = 0.0f;
                    if (i_2 < d2.values.Length)
                    {
                        if (j < d2.values[i_2].Length)
                        {
                            val = d1.values[i_2][j] * d2.values[i_2][j];
                        }
                    }
                    NUnit.Framework.Assert.AreEqual(clone.GetValueAt(i_2, j), 5.0e-4, val);
                }
            }
        }
示例#2
0
        public virtual void TestNewEmptyClone(ConcatVectorTest.DenseTestVector d1)
        {
            ConcatVector empty      = new ConcatVector(d1.vector.GetNumberOfComponents());
            ConcatVector emptyClone = d1.vector.NewEmptyClone();

            NUnit.Framework.Assert.IsTrue(empty.ValueEquals(emptyClone, 1.0e-5));
        }
示例#3
0
            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);
            }
示例#4
0
        public virtual void TestAddVector(ConcatVectorTest.DenseTestVector d1, ConcatVectorTest.DenseTestVector d2, ConcatVectorTest.DenseTestVector d3)
        {
            // Test the invariant x^Tz + 0.7*y^Tz == (x+0.7*y)^Tz
            double       expected = d1.vector.DotProduct(d3.vector) + (0.7f * d2.vector.DotProduct(d3.vector));
            ConcatVector clone    = d1.vector.DeepClone();

            clone.AddVectorInPlace(d2.vector, 0.7f);
            NUnit.Framework.Assert.AreEqual(clone.DotProduct(d3.vector), 5.0e-4, expected);
        }
示例#5
0
        public virtual ConcatVector ToVector(IDictionary <int, int> featureMap)
        {
            ConcatVector vector = new ConcatVector(20);

            foreach (int i in featureMap.Keys)
            {
                vector.SetSparseComponent(i, featureMap[i], 1.0);
            }
            return(vector);
        }
示例#6
0
        /// <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);
        }
示例#7
0
        public virtual ConcatVector ToNamespaceVector(ConcatVectorNamespace @namespace, IDictionary <int, int> featureMap)
        {
            ConcatVector newVector = @namespace.NewVector();

            foreach (int i in featureMap.Keys)
            {
                string feature = "feat" + i;
                string sparse  = "index" + featureMap[i];
                @namespace.SetSparseFeature(newVector, feature, sparse, 1.0);
            }
            return(newVector);
        }
示例#8
0
        public virtual void TestDeepCloneGetValueAt(ConcatVectorTest.DenseTestVector d1)
        {
            ConcatVector mv    = d1.vector;
            ConcatVector clone = d1.vector.DeepClone();

            for (int i = 0; i < d1.values.Length; i++)
            {
                for (int j = 0; j < d1.values[i].Length; j++)
                {
                    NUnit.Framework.Assert.AreEqual(clone.GetValueAt(i, j), 1.0e-10, mv.GetValueAt(i, j));
                }
            }
        }
示例#9
0
        public virtual void TestAddSparseToSparse(int sparseIndex1, double val1, int sparseIndex2, double val2)
        {
            ConcatVector v1 = new ConcatVector(1);

            v1.SetSparseComponent(0, (int)sparseIndex1, val1);
            ConcatVector v2 = new ConcatVector(1);

            v2.SetSparseComponent(0, (int)sparseIndex2, val2);
            double expected = v1.DotProduct(v2) + 0.7f * (v2.DotProduct(v2));

            v1.AddVectorInPlace(v2, 0.7f);
            NUnit.Framework.Assert.AreEqual(v1.DotProduct(v2), 5.0e-3, expected);
        }
示例#10
0
        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);
        }
示例#11
0
        public virtual void TestCopyOnWrite(ConcatVectorTest.DenseTestVector d1)
        {
            ConcatVector v2 = d1.vector.DeepClone();

            v2.AddVectorInPlace(v2, 1.0);
            for (int i = 0; i < d1.values.Length; i++)
            {
                for (int j = 0; j < d1.values[i].Length; j++)
                {
                    NUnit.Framework.Assert.AreEqual(d1.vector.GetValueAt(i, j), 5.0e-4, d1.values[i][j]);
                    NUnit.Framework.Assert.AreEqual(v2.GetValueAt(i, j), 5.0e-4, d1.values[i][j] * 2);
                }
            }
        }
示例#12
0
        public virtual void TestGetSparseIndex(int sparse1, double sparse1Val, int sparse2, double sparse2Val)
        {
            ConcatVector v1 = new ConcatVector(2);
            ConcatVector v2 = new ConcatVector(2);

            v1.SetSparseComponent(0, (int)sparse1, sparse1Val);
            v1.SetSparseComponent(1, (int)sparse2, sparse1Val);
            v2.SetSparseComponent(0, (int)sparse2, sparse2Val);
            v2.SetSparseComponent(1, (int)sparse1, sparse2Val);
            NUnit.Framework.Assert.AreEqual(sparse1, v1.GetSparseIndex(0));
            NUnit.Framework.Assert.AreEqual(sparse2, v1.GetSparseIndex(1));
            NUnit.Framework.Assert.AreEqual(sparse2, v2.GetSparseIndex(0));
            NUnit.Framework.Assert.AreEqual(sparse1, v2.GetSparseIndex(1));
        }
示例#13
0
        public virtual void TestProtoVector(ConcatVectorTest.DenseTestVector d1, ConcatVectorTest.DenseTestVector d2)
        {
            double expected = d1.vector.DotProduct(d2.vector);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            System.Diagnostics.Debug.Assert((d1.vector.GetType() == typeof(ConcatVector)));
            d1.vector.WriteToStream(byteArrayOutputStream);
            byteArrayOutputStream.Close();
            byte[] bytes = byteArrayOutputStream.ToByteArray();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            ConcatVector         recovered            = ConcatVector.ReadFromStream(byteArrayInputStream);

            NUnit.Framework.Assert.AreEqual(recovered.DotProduct(d2.vector), 5.0e-4, expected);
        }
示例#14
0
            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));
            }
示例#15
0
        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);
        }
示例#16
0
        public virtual void TestAppendSparseComponent(int sparse1, double sparse1Val, int sparse2, double sparse2Val)
        {
            ConcatVector v1 = new ConcatVector(1);
            ConcatVector v2 = new ConcatVector(1);

            v1.SetSparseComponent(0, (int)sparse1, sparse1Val);
            v2.SetSparseComponent(0, (int)sparse2, sparse2Val);
            if (sparse1 == sparse2)
            {
                NUnit.Framework.Assert.AreEqual(v1.DotProduct(v2), 5.0e-4, sparse1Val * sparse2Val);
            }
            else
            {
                NUnit.Framework.Assert.AreEqual(v1.DotProduct(v2), 5.0e-4, 0.0);
            }
        }
示例#17
0
        public virtual void TestValueEquals(ConcatVectorTest.DenseTestVector d1)
        {
            ConcatVector clone = d1.vector.DeepClone();

            NUnit.Framework.Assert.IsTrue(clone.ValueEquals(d1.vector, 1.0e-5));
            NUnit.Framework.Assert.IsTrue(d1.vector.ValueEquals(clone, 1.0e-5));
            NUnit.Framework.Assert.IsTrue(d1.vector.ValueEquals(d1.vector, 1.0e-5));
            NUnit.Framework.Assert.IsTrue(clone.ValueEquals(clone, 1.0e-5));
            Random r    = new Random();
            int    size = clone.GetNumberOfComponents();

            if (size > 0)
            {
                clone.AddVectorInPlace(d1.vector, 1.0);
                // If the clone is a 0 vector
                bool isZero = true;
                foreach (double[] arr in d1.values)
                {
                    foreach (double d in arr)
                    {
                        if (d != 0)
                        {
                            isZero = false;
                        }
                    }
                }
                if (isZero)
                {
                    NUnit.Framework.Assert.IsTrue(clone.ValueEquals(d1.vector, 1.0e-5));
                    NUnit.Framework.Assert.IsTrue(d1.vector.ValueEquals(clone, 1.0e-5));
                }
                else
                {
                    NUnit.Framework.Assert.IsFalse(clone.ValueEquals(d1.vector, 1.0e-5));
                    NUnit.Framework.Assert.IsFalse(d1.vector.ValueEquals(clone, 1.0e-5));
                }
                NUnit.Framework.Assert.IsTrue(d1.vector.ValueEquals(d1.vector, 1.0e-5));
                NUnit.Framework.Assert.IsTrue(clone.ValueEquals(clone, 1.0e-5));
                // refresh the clone
                clone = d1.vector.DeepClone();
                int tinker = r.NextInt(size);
                d1.vector.SetDenseComponent(tinker, new double[] { 0, 0, 1 });
                clone.SetSparseComponent(tinker, 2, 1);
                NUnit.Framework.Assert.IsTrue(d1.vector.ValueEquals(clone, 1.0e-5));
                NUnit.Framework.Assert.IsTrue(clone.ValueEquals(d1.vector, 1.0e-5));
            }
        }
示例#18
0
 /// <summary>This writes a feature's individual value, using the human readable name if possible, to a StringBuilder</summary>
 /// <exception cref="System.IO.IOException"/>
 private void DebugFeatureValue(string feature, int index, ConcatVector vector, BufferedWriter bw)
 {
     bw.Write("\t");
     if (sparseFeatureIndex.Contains(feature) && sparseFeatureIndex[feature].Values.Contains(index))
     {
         // we can map this index to an interpretable string, so we do
         bw.Write(reverseSparseFeatureIndex[feature][index]);
     }
     else
     {
         // we can't map this to a useful string, so we default to the number
         bw.Write(int.ToString(index));
     }
     bw.Write(": ");
     bw.Write(double.ToString(vector.GetValueAt(featureToIndex[feature], index)));
     bw.Write("\n");
 }
示例#19
0
        public virtual void TestResizeOnSetComponent(IDictionary <int, int> featureMap1, IDictionary <int, int> featureMap2)
        {
            ConcatVectorNamespace @namespace = new ConcatVectorNamespace();
            ConcatVector          namespace1 = ToNamespaceVector(@namespace, (IDictionary <int, int>)featureMap1);
            ConcatVector          namespace2 = ToNamespaceVector(@namespace, (IDictionary <int, int>)featureMap2);
            ConcatVector          regular1   = ToVector((IDictionary <int, int>)featureMap1);
            ConcatVector          regular2   = ToVector((IDictionary <int, int>)featureMap2);

            NUnit.Framework.Assert.AreEqual(namespace1.DotProduct(namespace2), 1.0e-5, regular1.DotProduct(regular2));
            ConcatVector namespaceSum = namespace1.DeepClone();

            namespaceSum.AddVectorInPlace(namespace2, 1.0);
            ConcatVector regularSum = regular1.DeepClone();

            regularSum.AddVectorInPlace(regular2, 1.0);
            NUnit.Framework.Assert.AreEqual(namespace1.DotProduct(namespaceSum), 1.0e-5, regular1.DotProduct(regularSum));
            NUnit.Framework.Assert.AreEqual(namespaceSum.DotProduct(namespace2), 1.0e-5, regularSum.DotProduct(regular2));
        }
示例#20
0
        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);
            }
        }
示例#21
0
        public virtual void TestElementwiseSparseToSparse(int sparseIndex1, double val1, int sparseIndex2, double val2)
        {
            ConcatVector v1 = new ConcatVector(1);

            v1.SetSparseComponent(0, (int)sparseIndex1, val1);
            ConcatVector v2 = new ConcatVector(1);

            v2.SetSparseComponent(0, (int)sparseIndex2, val2);
            v1.ElementwiseProductInPlace(v2);
            for (int i = 0; i < 10; i++)
            {
                double expected = 0.0f;
                if (i == sparseIndex1 && i == sparseIndex2)
                {
                    expected = val1 * val2;
                }
                NUnit.Framework.Assert.AreEqual(v1.GetValueAt(0, i), 5.0e-4, expected);
            }
        }
示例#22
0
 /// <summary>This prints out a ConcatVector by mapping to the namespace, to make debugging learning algorithms easier.</summary>
 /// <param name="vector">the vector to print</param>
 /// <param name="bw">the output stream to write to</param>
 /// <exception cref="System.IO.IOException"/>
 public virtual void DebugVector(ConcatVector vector, BufferedWriter bw)
 {
     foreach (string key in featureToIndex.Keys)
     {
         bw.Write(key);
         bw.Write(":\n");
         int i = featureToIndex[key];
         if (vector.IsComponentSparse(i))
         {
             DebugFeatureValue(key, vector.GetSparseIndex(i), vector, bw);
         }
         else
         {
             double[] arr = vector.GetDenseComponent(i);
             for (int j = 0; j < arr.Length; j++)
             {
                 DebugFeatureValue(key, j, vector, bw);
             }
         }
     }
 }
            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);
            }
示例#24
0
 /// <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);
 }
示例#25
0
 /// <summary>
 /// This adds a sparse 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="index">the index of the one-hot vector to set, as a string, which we will translate into a mapping</param>
 /// <param name="value">the value we want to set this one-hot index to</param>
 public virtual void SetSparseFeature(ConcatVector vector, string featureName, string index, double value)
 {
     vector.SetSparseComponent(EnsureFeature(featureName), EnsureSparseFeature(featureName, index), value);
 }
示例#26
0
 public DenseTestVector(double[][] values, ConcatVector vector)
 {
     this.values = values;
     this.vector = vector;
 }