private void Backprop(double[,] result, double[,] expected)
        {
            int hs = hidden.Length;

            double[][,] DeltaHiddenWeights = new double[hs][, ];
            DeltaHiddenWeights[hs - 1]     = new double[1, output.GetLength(1)];
            for (int i = hs - 2; i >= 0; i--)
            {
                DeltaHiddenWeights[i] = new double[1, hs];
            }
            double[,] DeltaInputWeights = new double[1, hs];

            var err = Error(result, expected);                                                                 //вродь правильно

            DeltaHiddenWeights[hs - 1]   = Elementwise.Multiply(sigmoid(output, true), err);                   //вродь правильно
            double[,] PreviousLayerError = DeltaHiddenWeights[hs - 1].Dot(hidden_weights[hs - 1].Transpose()); //вродь правильно

            for (int i = hs - 2; i >= 0; i--)
            {
                DeltaHiddenWeights[i] = Elementwise.Multiply(sigmoid(hidden[i + 1].RemoveColumn(hidden[i].Columns() - 1), true), PreviousLayerError);
                PreviousLayerError    = DeltaHiddenWeights[i].Dot(hidden_weights[i].Transpose());
            }

            DeltaInputWeights = Elementwise.Multiply(sigmoid(hidden[0].RemoveColumn(hidden[0].Columns() - 1), true), PreviousLayerError);

            for (int i = hs - 1; i >= 0; i--)
            {
                hidden_weights[i] = hidden_weights[i].Add(hidden[i].Transpose().Dot(DeltaHiddenWeights[i].Multiply(alpha)));
            }
            input_weights = input_weights.Add(input.Transpose().Dot(DeltaInputWeights.Multiply(alpha)));
        }
Ejemplo n.º 2
0
        public void Learn(List <double[]> vectors, List <double> weights)
        {
            if (vectors.Count <= 2)
            {
                return;
            }

            Mean = new double[Mean.Length];

            for (int index = 0; index < vectors.Count; index++)
            {
                var vector = vectors[index];
                Mean = Mean.Add(vector.Multiply(weights[index]));
            }

            Mean = Mean.Divide(weights.Sum());

            CovarianceMatrix = new double[vectors[0].Length, vectors[0].Length];

            for (int index = 0; index < vectors.Count; index++)
            {
                var vector     = vectors[index];
                var diffVector = vector.Subtract(Mean).ToMatrix();
                var covMatirix = diffVector.Transpose().Multiply(diffVector);
                CovarianceMatrix = CovarianceMatrix.Add(covMatirix.Multiply(weights[index]));
            }

            CovarianceMatrix = CovarianceMatrix.Divide(weights.Sum());

            EigenMatrix = CovarianceMatrix.GetEigenMatrix();

            Learned = true;
        }
Ejemplo n.º 3
0
        public void Convolute_TestArrayWithModifiedKernel_EqualsInputArray(string pattern, string paddingMethod)
        {
            // Arrange
            testImg.New(pattern);
            for (int k = 0; k < 100; k += 10)
            {
                float e = (float)(0 + k * 0.000000001);

                // Random kernel
                int w = 5;
                kernel = new double[w, w];
                Random r = new Random();
                for (int i = 0; i < w; i++)
                {
                    for (int j = 0; j < w; j++)
                    {
                        kernel[i, j] = r.Next(0, 1000);
                    }
                }
                double[,] kernel2 = kernel.Add(e); // Second kernel with residual

                // Act
                double[,] convResult  = Functions.Convolution2D(kernel, testImg.Image.ToDouble(), paddingMethod);
                double[,] convResult2 = Functions.Convolution2D(kernel2, testImg.Image.ToDouble(), paddingMethod);

                // Assert
                for (int i = 0; i < convResult.GetLength(0); i++)
                {
                    for (int j = 0; j < convResult.GetLength(1); j++)
                    {
                        Assert.Equal(convResult[i, j], convResult2[i, j], 4);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void Train(double[][] data, double[][] labels, double learningRate)
        {
            for (int i = 0; i < data.Length; i++)
            {
                var feedforward       = Fetch(data[i]);
                var output            = feedforward.output;
                var weightedSumOutput = feedforward.weightedSumOutput;

                var Labels = new double[][]
                {
                    new double[] { 0 },
                };

                Labels[0][0] = GenerateXORLabel(data[i][0], data[i][1]);



                //This is basically the cost function
                var errors_output = Labels[0].Subtract(output); //How good is to output compared to the target

                /*---------------------------------------------------------------
                * -- Calculate Delta of the weight between hidden and output --
                *  ---------------------------------------------------------------*/
                var HiddenTransposed  = Hidden.Transpose();
                var deltaWeightOutput = HiddenTransposed.Dot(errors_output);
                double[,] deltaWeightOutput2D = Matrix.Create(deltaWeightOutput); //Convert to Matrix

                /*---------------------------------------------------------------
                * -- Calculate Delta of the weight between input and hidden --
                *  ---------------------------------------------------------------*/
                //First we have to calculate the Error in the hidden nodes ...
                //Transposed because we are going Backwards through the Network
                var WHOTransposed = WeightsHiddenOutput.Transpose();
                //Moves the Error to the output layer
                var errors_hidden = WHOTransposed.Dot(errors_output);
                //Element Wise multiplication (schur product)
                var weightedSumHidden = feedforward.weightedSumHidden;
                weightedSumHidden = ApplyDerivativeReLU(weightedSumHidden);
                //Moves the Error backthrough the Neuron
                errors_hidden = errors_hidden.Multiply(weightedSumHidden);

                //... then we can Calculate the Delta
                var InputTransposed   = Inputs.Transpose();
                var deltaWeightHidden = InputTransposed.Dot(errors_hidden);
                double[,] deltaWeightHidden2D = Matrix.Create(deltaWeightHidden); //Convert to Matrix
                deltaWeightHidden2D           = Inputs.Transpose().Dot(deltaWeightHidden2D);

                /*---------------------------------------------------------------
                * --        Adjust Weights and Biases using the delta         --
                *  ---------------------------------------------------------------*/
                //The Biases just get adjusted by adding the Errors multiplied by the learning rate
                BiasOutput          = BiasOutput.Add(errors_output.Multiply(learningRate)); //Output Bias
                WeightsHiddenOutput = WeightsHiddenOutput.Add(deltaWeightOutput2D.Multiply(learningRate));
                BiasHidden          = BiasHidden.Add(errors_hidden.Multiply(learningRate)); //Hidden Bias
                WeightsInputHidden  = WeightsInputHidden.Add(deltaWeightHidden2D.Multiply(learningRate));
            }
        }
        // ----------------------------------------------------------------------------------------

        // Helper function for updating weight values during backpropagation.
        private void UpdateWeigths(double[,] layer1, double[,] layer2, ref double[,] weigths, double[] errorList)
        {
            // Convert errors list (one-dimensional) to two-dimensional, one-column matrix.
            double[,] errors = Matrix.Create <double>(errorList.Length, 1);
            errors.SetColumn(0, errorList);

            // Calculate delta matrix (differences for updating weight values).
            double[,] deltas = Elementwise.Multiply(errors, layer2.Apply(x => x * (1 - x)))
                               .Dot(layer1.Transpose())
                               .Apply(x => x * .1);

            // Apply delta matrix.
            weigths = weigths.Add(deltas);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///   Registers the occurrence of a value.
        /// </summary>
        ///
        /// <param name="x">The x-coordinate of the value to be registered.</param>
        /// <param name="y">The y-coordinate of the value to be registered.</param>
        ///
        public void Push(double x, double y)
        {
            double[,] Qloc = { { x }, { y } };

            // Predict next state
            Q_estimate = Matrix.Dot(A, Q_estimate).Add(B.Multiply(acceleration));

            // Predict Covariances
            P = Matrix.Dot(A, P.DotWithTransposed(A)).Add(Ex);

            Aux = Matrix.Dot(C, P.DotWithTransposed(C)).Add(Ez).PseudoInverse();

            // Kalman Gain
            K          = P.Dot(C.TransposeAndDot(Aux));
            Q_estimate = Q_estimate.Add(K.Dot(Qloc.Subtract(C.Dot(Q_estimate))));

            // Update P (Covariances)
            P = Matrix.Dot(diagonal.Subtract(Matrix.Dot(K, C)), P);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///   Registers the occurrence of a value.
        /// </summary>
        ///
        /// <param name="value">The value to be registered.</param>
        ///
        public void Push(DoublePoint value)
        {
            double[,] Qloc = { { value.X }, { value.Y } };

            // Predict next state
            Q_estimate = Matrix.Dot(A, Q_estimate).Add(B.Multiply(acceleration));

            // Predict Covariances
            P = Matrix.Dot(A, P.DotWithTransposed(A)).Add(Ex);

            Aux = Matrix.Dot(C, P.DotWithTransposed(C).Add(Ez)).PseudoInverse();

            // Kalman Gain
            K          = Matrix.Dot(Matrix.DotWithTransposed(P, C), Aux);
            Q_estimate = Q_estimate.Add(Matrix.Dot(K, Qloc.Subtract(Matrix.Dot(C, Q_estimate))));

            // Update P (Covariances)
            P = Matrix.Dot(diagonal.Subtract(Matrix.Dot(K, C)), P);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Method to standardize image grayscale values using previously defined inputs for gaussian kernels.
        /// Method string defines padding method.
        /// </summary>
        public void Standardize(ref double[,] image, string method)
        {
            // Get Gaussian kernels
            double[,] kernel1 = GaussianKernel(w1, s1),
            kernel2           = GaussianKernel(w2, s2);

            // Blurring with kernels
            double[,] blurredImage1 = Functions.Convolution2D(kernel1, image, method),
            blurredImage2           = Functions.Convolution2D(kernel2, image, method); // Alternative blurring, not used currently

            // Centering
            double[,] centered = image.Subtract(blurredImage1);

            // Standardization
            double[,] std = Functions.Convolution2D(kernel2, centered.Pow(2), method).Sqrt();
            image         = centered // This function standardizes and modifies original given image
                            .Divide(
                std.Add(1E-09));
        }
Ejemplo n.º 9
0
        /// <summary>
        ///   Registers the occurrence of a value.
        /// </summary>
        ///
        /// <param name="value">The value to be registered.</param>
        ///
        public void Push(DoublePoint value)
        {
            double[,] Qloc = { { value.X }, { value.Y } };

            // Predict next state
            Q_estimate = (A.Multiply(Q_estimate)).Add(B.Multiply(acceleration));

            // Predict Covariances
            P = (A.Multiply(P.Multiply(A.Transpose()))).Add(Ex);

            Aux = (C.Multiply(P.Multiply(C.Transpose())).Add(Ez)).PseudoInverse();

            // Kalman Gain
            K          = P.Multiply(C.Transpose().Multiply(Aux));
            Q_estimate = Q_estimate.Add(K.Multiply(Qloc.Subtract(C.Multiply(Q_estimate))));

            // Update P (Covariances)
            P = (diagonal.Subtract(K.Multiply(C))).Multiply(P);
        }
        /// <summary>
        /// Apply Black-Litterman master formula
        /// http://www.blacklitterman.org/cookbook.html
        /// </summary>
        /// <param name="Π">Prior/Posterior mean array</param>
        /// <param name="Σ">Prior/Posterior covariance matrix</param>
        /// <param name="P">A matrix that identifies the assets involved in the views (size: K x N)</param>
        /// <param name="Q">A view vector (size: K x 1)</param>
        private void ApplyBlackLittermanMasterFormula(ref double[] Π, ref double[,] Σ, double[,] P, double[] Q)
        {
            // Create the diagonal covariance matrix of error terms from the expressed views
            var eye = Matrix.Diagonal(Q.GetLength(0), 1);
            var Ω   = Elementwise.Multiply(P.Dot(Σ).DotWithTransposed(P).Multiply(_tau), eye);

            if (Ω.Determinant() != 0)
            {
                // Define matrices Στ and A to avoid recalculations
                var Στ = Σ.Multiply(_tau);
                var A  = Στ.DotWithTransposed(P).Dot(P.Dot(Στ).DotWithTransposed(P).Add(Ω).Inverse());

                // Compute posterior estimate of the mean: Black-Litterman "master equation"
                Π = Π.Add(A.Dot(Q.Subtract(P.Dot(Π))));

                // Compute posterior estimate of the uncertainty in the mean
                var M = Στ.Subtract(A.Dot(P).Dot(Στ));
                Σ = Σ.Add(M).Multiply(_delta);

                // Compute posterior weights based on uncertainty in mean
                var W = Π.Dot(Σ.Inverse());
            }
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            #region 1. Declaring matrices

            // 1.1 Using standard .NET declaration
            double[,] A =
            {
                { 1, 2, 3 },
                { 6, 2, 0 },
                { 0, 0, 1 }
            };

            double[,] B =
            {
                { 2, 0, 0 },
                { 0, 2, 0 },
                { 0, 0, 2 }
            };

            {
                // 1.2 Using Accord extension methods
                double[,] Bi = Matrix.Identity(3).Multiply(2);
                double[,] Bj = Matrix.Diagonal(3, 2.0); // both are equal to B

                // 1.2 Using Accord extension methods with implicit typing
                var I = Matrix.Identity(3);
            }
            #endregion



            #region 2. Matrix Operations
            {
                // 2.1 Addition
                var C = A.Add(B);

                // 2.2 Subtraction
                var D = A.Subtract(B);

                // 2.3 Multiplication
                {
                    // 2.3.1 By a scalar
                    var halfM = A.Multiply(0.5);

                    // 2.3.2 By a vector
                    double[] m = A.Multiply(new double[] { 1, 2, 3 });

                    // 2.3.3 By a matrix
                    var M = A.Multiply(B);

                    // 2.4 Transposing
                    var At = A.Transpose();
                }
            }


            // 2.5 Elementwise operations

            // 2.5.1 Elementwise multiplication
            A.ElementwiseMultiply(B); // A.*B

            // 2.5.1 Elementwise division
            A.ElementwiseDivide(B); // A./B

            #endregion



            #region 3. Matrix characteristics
            {
                // 3.1 Calculating the determinant
                double det = A.Determinant();

                // 3.2 Calculating the trace
                double tr = A.Trace();

                // 3.3 Computing the sum vector
                {
                    double[] sumVector = A.Sum();

                    // 3.3.1 Computing the total sum of elements
                    double sum = sumVector.Sum();

                    // 3.3.2 Computing the sum along the rows
                    sumVector = A.Sum(0); // Equivalent to Octave's sum(A, 1)

                    // 3.3.2 Computing the sum along the columns
                    sumVector = A.Sum(1); // Equivalent to Octave's sum(A, 2)
                }
            }
            #endregion



            #region 4. Linear Algebra
            {
                // 4.1 Computing the inverse
                var invA = A.Inverse();

                // 4.2 Computing the pseudo-inverse
                var pinvA = A.PseudoInverse();

                // 4.3 Solving a linear system (Ax = B)
                var x = A.Solve(B);
            }
            #endregion



            #region 5. Special operators
            {
                // 5.1 Finding the indices of elements
                double[] v   = { 5, 2, 2, 7, 1, 0 };
                int[]    idx = v.Find(e => e > 2); // finding the index of every element in v higher than 2.

                // 5.2 Selecting elements by index
                double[] u = v.Submatrix(idx); // u is { 5, 7 }

                // 5.3 Converting between different matrix representations
                double[][] jaggedA = A.ToArray(); // from multidimensional to jagged array

                // 5.4 Extracting a column or row from the matrix
                double[] a = A.GetColumn(0); // retrieves the first column
                double[] b = B.GetRow(1);    // retrieves the second row

                // 5.5 Taking the absolute of a matrix
                var absA = A.Abs();

                // 5.6 Applying some function to every element
                var newv = v.Apply(e => e + 1);
            }
            #endregion



            #region 7. Vector operations
            {
                double[] u = { 1, 2, 3 };
                double[] v = { 4, 5, 6 };

                var w1 = u.InnerProduct(v);
                var w2 = u.OuterProduct(v);
                var w3 = u.CartesianProduct(v);


                double[] m = { 1, 2, 3, 4 };
                double[,] M = Matrix.Reshape(m, 2, 2);
            }
            #endregion


            #region Decompositions
            {
                // Singular value decomposition
                {
                    SingularValueDecomposition svd = new SingularValueDecomposition(A);
                    var U = svd.LeftSingularVectors;
                    var S = svd.Diagonal;
                    var V = svd.RightSingularVectors;
                }
                // or (please see documentation for details)
                {
                    SingularValueDecomposition svd = new SingularValueDecomposition(A.Transpose());
                    var U = svd.RightSingularVectors;
                    var S = svd.Diagonal;
                    var V = svd.LeftSingularVectors;
                }

                // Eigenvalue decomposition
                {
                    EigenvalueDecomposition eig = new EigenvalueDecomposition(A);
                    var V = eig.Eigenvectors;
                    var D = eig.DiagonalMatrix;
                }

                // QR decomposition
                {
                    QrDecomposition qr = new QrDecomposition(A);
                    var             Q  = qr.OrthogonalFactor;
                    var             R  = qr.UpperTriangularFactor;
                }

                // Cholesky decomposition
                {
                    CholeskyDecomposition chol = new CholeskyDecomposition(A);
                    var R = chol.LeftTriangularFactor;
                }

                // LU decomposition
                {
                    LuDecomposition lu = new LuDecomposition(A);
                    var             L  = lu.LowerTriangularFactor;
                    var             U  = lu.UpperTriangularFactor;
                }
            }
            #endregion
        }
Ejemplo n.º 12
0
 // update levenberg
 public layer lmupdate(double[] x)
 {
     output = (weights.Add(weightDelta)).Dot(x);
     return(this);
 }
Ejemplo n.º 13
0
        public double KALMANOUT(double Input)
        {
            A[0, 0] = 1;
            A[0,1]= dt;
            A[1, 0] = 0;
            A[1, 1] = 1;//{ { 1, dt }, { 0, 1 } };
            B[0, 0] = u * Math.Pow(dt, 2) / 2;
            B[1, 0] = u * dt;
            C[0, 0] = 1;
            C[0, 1] = 0;
               //     XT[0, 0] = position;
             //   XT[1, 0] = velocity;
             // double[,] B = { { u * Math.Pow(dt, 2) / 2 }, { u * dt } };
            //  double[,] C = { { 1 } , { 0 } };
            //       double[,] XT= { { position }, { velocity} };
            //     double[,] EX = { { Math.Pow(processNoise, 2) * (Math.Pow(dt, 4) / 4), Math.Pow(processNoise, 2) * (Math.Pow(dt, 3) / 2) }, { Math.Pow(processNoise, 2) * (Math.Pow(dt, 3) / 2), Math.Pow(processNoise, 2) * (Math.Pow(dt, 2)) } }; ;
            //    double[,] AT = { { 1, 0 }, { dt, 1 } };

            input = Input;
            A.Multiply(XT, tempX);
            //      double[,] tempX=Accord.Math.Matrix.Multiply(A, XT);
               XT= tempX.Add(B);
            //   double[,] XT=Accord.Math.Matrix.Add(tempX, B);
            C.Multiply(XT, ZT);
            A.Multiply(PT, AP);
            AP.Multiply(AT, APAT);
            PT=APAT.Add(EX);
            PT.Multiply(CT, tempPCT);
            C.Multiply(tempPCT, CPCT);
            S_ = 1 / (CPCT[0, 0] + measureNoise);
            tempPCT.Multiply(S_,K);

            error = Input - ZT[0, 0];
            K.Multiply(error,tempK);
            XTT = XT.Add(tempK);
            XT = XTT;
            position = XT[0, 0];
            velocity = XT[1, 0];

            I_KC = 1 - K[0,0];
            PT.Multiply(I_KC, PT);

            return XT[0, 0];
        }