示例#1
0
        Matrix <double> Y_Matrix()
        {
            var             a        = X.ToArray();
            int             m        = a.GetUpperBound(0) + 1;
            Matrix <double> y_matrix = MathNetHelpers.Zeros(m, num_labels);

            for (int i = 0; i < m; i++)
            {
                y_matrix[i, (int)(Y[i] + 0.5)] = 1.0;
            }
            return(y_matrix);
        }
示例#2
0
        Matrix <double>[] Gradients()
        {
            var a = X.ToArray();
            int m = a.GetUpperBound(0) + 1;

            Matrix <double>[] grad = new Matrix <double> [Theta.Length];
            for (int i = 0; i < grad.Length; i++)
            {
                grad[i] = MathNetHelpers.Zeros(Theta[i].RowCount, Theta[i].ColumnCount);
            }

            Matrix <double>[] activations     = new Matrix <double> [Layers.Length];
            Matrix <double>   prev_activation = X.Transpose(); //0th (Input) Layer's activationk

            prev_activation = MathNetHelpers.AddRowOfOnes(prev_activation);
            activations[0]  = prev_activation;
            for (int i = 1; i < Layers.Length - 1; i++)
            {
                var z = Theta[i - 1] * prev_activation;
                prev_activation = MathNetHelpers.AddRowOfOnes(MathNetHelpers.Sigmoid(z));
                activations[i]  = prev_activation;
            }
            var _z = Theta[Theta.Length - 1] * prev_activation;

            prev_activation = MathNetHelpers.Sigmoid(_z);
            activations[activations.Length - 1] = prev_activation;

            Matrix <double>[] delta = new Matrix <double> [Layers.Length];
            delta[delta.Length - 1] = (prev_activation - Y_Matrix().Transpose()); //The delta of the output layer
            for (int i = delta.Length - 2; i > 0; i--)
            {
                var temp = MathNetHelpers.RemoveColumn(activations[i]);
                temp     = MathNetHelpers.AddColumnOfOnes(temp);
                delta[i] = (Theta[i].Transpose() * delta[i + 1]).PointwiseMultiply(temp).PointwiseMultiply(1 - temp);
                delta[i] = MathNetHelpers.RemoveRow(delta[i]);
            }
            Matrix <double>[] Delta = new Matrix <double> [Theta.Length];
            for (int i = 0; i < Delta.Length; i++)
            {
                Delta[i] = (delta[i + 1] * activations[i].Transpose());
            }
            for (int i = 0; i < grad.Length; i++)
            {
                var z = MathNetHelpers.RemoveRow(Theta[i]);
                z       = MathNetHelpers.AddRowOf(z, 0);
                grad[i] = (Delta[i] + lambda * z) / m;
            }
            return(grad);
        }