numl.Math.Functions.Cost.CofiCostFunction.ComputeGradient C# (CSharp) Method

ComputeGradient() public method

Compute the error cost of the given Theta parameter for the training and label sets
public ComputeGradient ( Vector theta ) : Vector
theta numl.Math.LinearAlgebra.Vector Learning Theta parameters
return numl.Math.LinearAlgebra.Vector
        public override Vector ComputeGradient(Vector theta)
        {
            Matrix ThetaX = theta.Slice(0, (R.Rows * CollaborativeFeatures) - 1).Reshape(CollaborativeFeatures, VectorType.Col);
            Matrix ThetaY = theta.Slice((R.Rows * CollaborativeFeatures), theta.Length - 1).Reshape(CollaborativeFeatures, VectorType.Col);

            Matrix A = ((ThetaY * ThetaX.T).T - YReformed);
            Matrix S = A.Each(R, (i, j) => i * j);

            Matrix gradX = (S * ThetaY) + (Lambda * ThetaX);
            Matrix gradTheta = (S.T * ThetaX) + (Lambda * ThetaY);

            return Vector.Combine(gradX.Unshape(), gradTheta.Unshape());
        }

Usage Example

コード例 #1
0
        public void Test_Cofi_CostFunction_Regularized()
        {
            Matrix rMat = Y.ToBinary(i => i > 0d);

            ICostFunction costFunction = new CofiCostFunction() { R = rMat, X = X, Y = Y.Unshape(), Lambda = 1.5, Regularizer = null, CollaborativeFeatures = X.Cols };
            costFunction.Initialize();
            double cost = costFunction.ComputeCost(Vector.Combine(X.Unshape(), Theta.Unshape()));
            Vector grad = costFunction.ComputeGradient(Vector.Combine(X.Unshape(), Theta.Unshape()));

            Almost.Equal(55.172, System.Math.Round(cost, 3), 0.0011);

            this.CheckCofiGradient(1.5);
        }
All Usage Examples Of numl.Math.Functions.Cost.CofiCostFunction::ComputeGradient