Example #1
0
        public void ExampleTest3()
        {
            // Suppose we would like to map the continuous values in the
            // second column to the integer values in the first column.
            var inputs = new[]
            {
                new[] { 1.0, 1.0, 1.0, 1.0 },
                new[] { 2.0, 4.0, 8.0, 16.0 },
                new[] { 3.0, 9.0, 27.0, 81.0 },
                new[] { 4.0, 16.0, 64.0, 256.0 },
            };

            var ouputs = new[] { 0.73, 3.24, 8.31, 16.72 };

            var regression = new MultipleLinearRegression(4);
            var nnls       = new NonNegativeLeastSquares(regression)
            {
                MaxIterations = 100
            };

            nnls.Run(inputs, ouputs);
            Assert.AreEqual(0.1, nnls.Coefficients[0], 1e-3);
            Assert.AreEqual(0.5, nnls.Coefficients[1], 1e-3);
            Assert.AreEqual(0.13, nnls.Coefficients[2], 1e-3);
            Assert.AreEqual(0, nnls.Coefficients[3], 1e-3);
        }
Example #2
0
        public void ExampleTest3()
        {
            var inputs = new[]
            {
                new[] { 1.0, 1.0, 1.0, 1.0 },
                new[] { 2.0, 4.0, 8.0, 16.0 },
                new[] { 3.0, 9.0, 27.0, 81.0 },
                new[] { 4.0, 16.0, 64.0, 256.0 },
            };

            var ouputs = new[] { 0.73, 3.24, 8.31, 16.72 };

            var regression = new MultipleLinearRegression(4);
            var nnls       = new NonNegativeLeastSquares(regression)
            {
                MaxIterations = 100
            };

            nnls.Run(inputs, ouputs);
            Assert.AreEqual(0.1, nnls.Coefficients[0], 1e-3);
            Assert.AreEqual(0.5, nnls.Coefficients[1], 1e-3);
            Assert.AreEqual(0.13, nnls.Coefficients[2], 1e-3);
            Assert.AreEqual(0, nnls.Coefficients[3], 1e-3);

            Assert.AreEqual(0.1, regression.Coefficients[0], 1e-3);
            Assert.AreEqual(0.5, regression.Coefficients[1], 1e-3);
            Assert.AreEqual(0.13, regression.Coefficients[2], 1e-3);
        }
        public void ExampleTest2()
        {
            // Suppose we would like to map the continuous values in the
            // second column to the integer values in the first column.
            var inputs = new[]
            {
                new[] { 1.0, 1.0, 1.0 },
                new[] { 2.0, 4.0, 8.0 },
                new[] { 3.0, 9.0, 27.0 },
                new[] { 4.0, 16.0, 64.0 },
            };

            var ouputs = new[] { 0.73, 3.24, 8.31, 16.72 };

            var regression = new MultipleLinearRegression(3);
            var nnls = new NonNegativeLeastSquares(regression)
            {
                MaxIterations = 100
            };

            nnls.Run(inputs, ouputs);
            Assert.AreEqual(0.1, nnls.Coefficients[0], 1e-3);
            Assert.AreEqual(0.5, nnls.Coefficients[1], 1e-3);
            Assert.AreEqual(0.13, nnls.Coefficients[2], 1e-3);
        }
Example #4
0
        public void learn_test()
        {
            #region doc_learn
            // Declare training samples
            var inputs = new double[][]
            {
                new[] { 1.0, 1.0, 1.0 },
                new[] { 2.0, 4.0, 8.0 },
                new[] { 3.0, 9.0, 27.0 },
                new[] { 4.0, 16.0, 64.0 },
            };

            var outputs = new double[] { 0.23, 1.24, 3.81, 8.72 };

            // Create a NN LS learning algorithm
            var nnls = new NonNegativeLeastSquares()
            {
                MaxIterations = 100
            };

            // Use the algorithm to learn a multiple linear regression
            MultipleLinearRegression regression = nnls.Learn(inputs, outputs);

            // None of the regression coefficients should be negative:
            double[] coefficients = regression.Weights; // should be

            // Check the quality of the regression:
            double[] prediction = regression.Transform(inputs);

            double error = new SquareLoss(expected: outputs)
                           .Loss(actual: prediction); // should be

            #endregion

            Assert.AreEqual(0, error, 1e-10);

            Assert.AreEqual(0.1, nnls.Coefficients[0], 1e-3);
            Assert.AreEqual(0, nnls.Coefficients[1], 1e-3);
            Assert.AreEqual(0.13, nnls.Coefficients[2], 1e-3);

            Assert.AreEqual(0.1, regression.Coefficients[0], 1e-3);
            Assert.AreEqual(0, regression.Coefficients[1], 1e-3);
            Assert.AreEqual(0.13, regression.Coefficients[2], 1e-3);
        }
Example #5
0
        public void ExampleTest()
        {
            var inputs = new[]
            {
                new[] { 1.0, 1.0 },
                new[] { 2.0, 4.0 },
                new[] { 3.0, 9.0 },
                new[] { 4.0, 16.0 },
            };

            var ouputs = new[] { 0.6, 2.2, 4.8, 8.4 };

            var regression = new MultipleLinearRegression(2);
            var nnls       = new NonNegativeLeastSquares(regression)
            {
                MaxIterations = 100
            };

            nnls.Run(inputs, ouputs);
            Assert.AreEqual(0.1, nnls.Coefficients[0], 1e-3);
            Assert.AreEqual(0.5, nnls.Coefficients[1], 1e-3);
        }
Example #6
0
            public void Execute()
            {
                int k = frameIndex;

                var tmpPositions = new UnityEngine.Vector3[sharedJobData.numVertices];
                var tmpNormals   = new UnityEngine.Vector3[sharedJobData.numVertices];

                var edgeLengths    = null as float[];
                var edgeCurvatures = null as float[];

                // prepare b
                var b = new double[sharedJobData.numEquations];
                {
                    Array.Copy(sharedJobData.frames[k].deltaPositions, tmpPositions, tmpPositions.Length);
                    Array.Copy(sharedJobData.frames[k].deltaNormals, tmpNormals, tmpNormals.Length);

                    switch (sharedJobData.fittingParam)
                    {
                    case Param.DeltaPosition:
                        for (int i = 0; i != sharedJobData.numVertices; i++)
                        {
                            b[i * 3 + 0] = tmpPositions[i].x;
                            b[i * 3 + 1] = tmpPositions[i].y;
                            b[i * 3 + 2] = tmpPositions[i].z;
                        }
                        break;

                    case Param.OutputEdgeLength:
                        for (int i = 0; i != sharedJobData.numVertices; i++)
                        {
                            tmpPositions[i] += sharedJobData.meshBuffers.vertexPositions[i];
                        }
                        sharedJobData.meshEdges.ComputeLengths(ref edgeLengths, tmpPositions);
                        for (int i = 0; i != sharedJobData.numEquations; i++)
                        {
                            b[i] = edgeLengths[i];
                        }
                        break;

                    case Param.OutputEdgeCurvature:
                        for (int i = 0; i != sharedJobData.numVertices; i++)
                        {
                            tmpPositions[i] += sharedJobData.meshBuffers.vertexPositions[i];
                            tmpNormals[i]   += sharedJobData.meshBuffers.vertexNormals[i];
                            tmpNormals[i].Normalize();
                        }
                        sharedJobData.meshEdges.ComputeCurvatures(ref edgeCurvatures, tmpPositions, tmpNormals);
                        for (int i = 0; i != sharedJobData.numEquations; i++)
                        {
                            b[i] = edgeCurvatures[i];
                        }
                        break;
                    }
                }

                // compute x*
                var x = new double[sharedJobData.numVariables];

                {
                    switch (sharedJobData.fittingMethod)
                    {
                    case Method.LinearLeastSquares:
                        sharedJobData.At_A_inv_At.Dot(b, x);                                // stores result in x
                        break;

                    case Method.NonNegativeLeastSquares:
                    {
                        var nnlsSolver = new NonNegativeLeastSquares()
                        {
                            MaxIterations = 200, Tolerance = 0.000000001
                        };
                        var nnlsOutput = nnlsSolver.Learn(sharedJobData.A_jagged, b);
                        Array.Copy(nnlsOutput.Weights, x, x.Length);
                    }
                    break;
                    }
                    //UnityEngine.Debug.Log("frame " + k + ": x* = " + x);
                }

                // remap weights to shape indices
                for (int j = 0; j != sharedJobData.numVariables; j++)
                {
                    sharedJobData.frames[k].fittedWeights[sharedJobData.blendShapeIndices[j]] = (float)x[j];
                }
            }