コード例 #1
0
        /// <summary>
        ///     Train.  Single iteration.
        /// </summary>
        public void Iteration()
        {
            int rowCount      = _trainingData.Count;
            int inputColCount = _trainingData[0].Input.Length;

            Matrix <double> xMatrix = new DenseMatrix(rowCount, inputColCount + 1);
            Matrix <double> yMatrix = new DenseMatrix(rowCount, 1);

            for (int row = 0; row < _trainingData.Count; row++)
            {
                BasicData dataRow = _trainingData[row];
                int       colSize = dataRow.Input.Count();

                xMatrix[row, 0] = 1;
                for (int col = 0; col < colSize; col++)
                {
                    xMatrix[row, col + 1] = dataRow.Input[col];
                }
                yMatrix[row, 0] = dataRow.Ideal[0];
            }

            // Calculate the least squares solution
            QR qr = xMatrix.QR();
            Matrix <double> beta = qr.Solve(yMatrix);

            double sum = 0.0;

            for (int i = 0; i < inputColCount; i++)
            {
                sum += yMatrix[i, 0];
            }
            double mean = sum / inputColCount;

            for (int i = 0; i < inputColCount; i++)
            {
                double dev = yMatrix[i, 0] - mean;
                _sst += dev * dev;
            }

            Matrix <double> residuals = xMatrix.Multiply(beta).Subtract(yMatrix);

            _sse = residuals.L2Norm() * residuals.L2Norm();

            for (int i = 0; i < _algorithm.LongTermMemory.Length; i++)
            {
                _algorithm.LongTermMemory[i] = beta[i, 0];
            }

            // calculate error
            _errorCalculation.Clear();
            foreach (BasicData dataRow in _trainingData)
            {
                double[] output = _algorithm.ComputeRegression(dataRow.Input);
                _errorCalculation.UpdateError(output, dataRow.Ideal, 1.0);
            }
            _error = _errorCalculation.Calculate();
        }
コード例 #2
0
ファイル: Model.cs プロジェクト: dskim95/UpDimension
        public void create3DModel()
        {
            int i, j, k;
            int count;
            int n = 0;              // point number
            int m = 0;              // image number
            int maxPointNumber = 0;
            int var            = 0; // number of variables
            int eq             = 0; // number of equations

            for (i = 0; i < scene.Count; i++)
            {
                if (scene[i].vertexList.Count - 1 > maxPointNumber)
                {
                    maxPointNumber = scene[i].vertexList.Count - 1;
                }
            }
            Console.WriteLine("maxPointNumber: {0}", maxPointNumber);

            for (i = 0; i <= maxPointNumber; i++)
            {
                count = 0;

                for (j = 0; j < scene.Count; j++)
                {
                    if (i < scene[j].vertexList.Count)
                    {
                        if (scene[j].vertexList[i].coord.X != -1)
                        {
                            count++;
                        }
                    }
                }

                if (count >= 2)
                {
                    n++;
                }
            }

            int[] indexToPointNumber = new int[n];

            k = 0;
            for (i = 0; i <= maxPointNumber; i++)
            {
                count = 0;

                for (j = 0; j < scene.Count; j++)
                {
                    if (i < scene[j].vertexList.Count)
                    {
                        if (scene[j].vertexList[i].coord.X != -1)
                        {
                            count++;
                        }
                    }
                }

                if (count >= 2)
                {
                    indexToPointNumber[k++] = i;
                }
            }

            for (i = 0; i < scene.Count; i++)
            {
                count = 0;

                for (j = 0; j < n; j++)
                {
                    if (indexToPointNumber[j] < scene[i].vertexList.Count)
                    {
                        if (scene[i].vertexList[indexToPointNumber[j]].coord.X != -1)
                        {
                            count++;
                        }
                    }
                }

                if (count >= 2)
                {
                    m++;
                }
            }


            int[] indexToImageNumber = new int[n];

            k = 0;
            for (i = 0; i < scene.Count; i++)
            {
                count = 0;

                for (j = 0; j < n; j++)
                {
                    if (indexToPointNumber[j] < scene[i].vertexList.Count)
                    {
                        if (scene[i].vertexList[indexToPointNumber[j]].coord.X != -1)
                        {
                            count++;
                        }
                    }
                }

                if (count >= 2)
                {
                    indexToImageNumber[k++] = i;
                }
            }

            for (i = 0; i < n; i++)
            {
                for (j = 0; j < m; j++)
                {
                    if (indexToPointNumber[i] < scene[indexToImageNumber[j]].vertexList.Count)
                    {
                        if (scene[indexToImageNumber[j]].vertexList[indexToPointNumber[i]].coord.X != -1)
                        {
                            eq++;
                        }
                    }
                }
            }

            var = 3 * n - 3 + 3 * m - 3;
            eq  = 2 * eq - 2;

            Console.WriteLine("{0}, {1}, {2}, {3}", n, m, var, eq);

            Matrix <float> A = Matrix <float> .Build.Dense(eq, var);

            Vector <float> b = Vector <float> .Build.Dense(eq);

            float[] t = new float[3];

            count = 0;
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < m; j++)
                {
                    if (i == 0 && j == 0)
                    {
                        PointF point = scene[indexToImageNumber[j]].vertexList[indexToPointNumber[i]].coord;
                        t[0] = point.X;
                        t[1] = point.Y;
                        t[2] = 1;
                        continue;
                    }

                    if (indexToPointNumber[i] < scene[indexToImageNumber[j]].vertexList.Count)
                    {
                        if (scene[indexToImageNumber[j]].vertexList[indexToPointNumber[i]].coord.X != -1)
                        {
                            PointF         point = scene[indexToImageNumber[j]].vertexList[indexToPointNumber[i]].coord;
                            Matrix <float> R     = DenseMatrix.OfArray(new float[, ]
                            {
                                { 1, 1, 1 },
                                { 1, 1, 1 },
                                { 1, 1, 1 }
                            });
                            if (i != 0)
                            {
                                A[count, 3 * i - 3]     = point.X * R[2, 0] - R[0, 0];
                                A[count, 3 * i - 2]     = point.X * R[2, 1] - R[0, 1];
                                A[count, 3 * i - 1]     = point.X * R[2, 2] - R[0, 2];
                                A[count + 1, 3 * i - 3] = point.Y * R[2, 0] - R[1, 0];
                                A[count + 1, 3 * i - 2] = point.Y * R[2, 1] - R[1, 1];
                                A[count + 1, 3 * i - 1] = point.Y * R[2, 2] - R[1, 2];
                            }
                            if (j != 0)
                            {
                                A[count, 3 * n - 3 + 3 * j - 3]     = -1;
                                A[count, 3 * n - 3 + 3 * j - 1]     = point.X;
                                A[count + 1, 3 * n - 3 + 3 * j - 2] = -1;
                                A[count + 1, 3 * n - 3 + 3 * j - 1] = point.Y;
                            }
                            else
                            {
                                b[count]     = -t[0] + point.X * t[2];
                                b[count + 1] = -t[1] + point.Y * t[2];
                            }
                            Console.WriteLine("dd");
                            count = count + 2;
                        }
                    }
                }
            }

            QR <float> qr = A.QR(QRMethod.Full);
            var        x  = qr.Solve(b);

            printMatrix(A);

            Console.WriteLine(x);
        }