Beispiel #1
0
        static void testMatrix()
        {
            RandomUtils random = new RandomUtils();

            int matrix_P_M = 3;
            int matrix_P_N = 4;

            int matrix_Q_M = 4;
            int matrix_Q_N = 7;

            Matrix matrix_P = newMatrix(matrix_P_M, matrix_P_N, random);
            Matrix matrix_Q = newMatrix(matrix_Q_M, matrix_Q_N, random);
            Matrix matrix_R = (matrix_P * (matrix_Q * (~matrix_Q))) + matrix_P;

            writeMatrix(matrix_P, "matrix_P");
            writeMatrix(matrix_Q, "matrix_Q");
            writeMatrix(matrix_R, "matrix_R");

            Matrix matrix_A = newMatrix(5, 6, random);
            Matrix matrix_B = newMatrix(5, 6, random);
            Matrix matrix_H = MatrixUtils.hadamardProduct(matrix_A, matrix_B);

            writeMatrix(matrix_A, "matrix_A");
            writeMatrix(matrix_B, "matrix_B");
            writeMatrix(matrix_H, "matrix_H");

            Console.ReadLine();
        }
Beispiel #2
0
        public Tuple <List <Matrix>, List <Matrix> > computeSingletonGradient(Matrix X, Matrix Y)
        {
            List <Matrix> wSingletonGradientList = new List <Matrix>();
            List <Matrix> bSingletonGradientList = new List <Matrix>();
            List <Matrix> deltaList = new List <Matrix>();

            //Feed forward:
            Tuple <List <Matrix>, List <Matrix> > zaLists = feedForward(X);
            List <Matrix> zList = zaLists.Item1;
            List <Matrix> aList = zaLists.Item2;

            //Back propagate:
            //BP1:
            deltaList.Add(
                MatrixUtils.hadamardProduct(
                    gradientCostOutput(aList.Last(), Y),
                    derivativeActivation(zList.Last())
                    )
                );

            //BP2:
            Matrix BP2Matrix = null;

            for (int l = layerSizes.Count - 2; l > 0; l--)
            {
                BP2Matrix =
                    MatrixUtils.hadamardProduct(
                        (~(wList.ElementAt(l + 1))) * deltaList.Last(),
                        derivativeActivation(zList.ElementAt(l))
                        );
                deltaList.Add(BP2Matrix);
            }
            deltaList.Add(null);
            deltaList.Reverse();

            //BP3, BP4:
            wSingletonGradientList.Add(null);
            bSingletonGradientList.Add(null);

            for (int l = 1; l < layerSizes.Count; l++)
            {
                wSingletonGradientList.Add(deltaList.ElementAt(l) * (~(aList.ElementAt(l - 1))));
                bSingletonGradientList.Add(deltaList.ElementAt(l));
            }

            return(Tuple.Create(wSingletonGradientList, bSingletonGradientList));
        }