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

ComputeCost() public method

Compute the error cost of the given Theta parameter for the training and label sets
public ComputeCost ( Vector theta ) : double
theta numl.Math.LinearAlgebra.Vector Learning Theta parameters
return double
        public override double ComputeCost(Vector theta)
        {
            double j = 0.0;

            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);

            j = (1.0 / 2.0) * ((ThetaY * ThetaX.T).T - YReformed).Each(i => System.Math.Pow(i, 2.0)).Each((v, r, c) => v * R[r, c]).Sum();

            if (Lambda != 0)
            {
                j = j + ((Lambda / 2.0) * (ThetaY.Each(i => System.Math.Pow(i, 2.0)).Sum()) + (Lambda / 2.0 * ThetaX.Each(i => System.Math.Pow(i, 2.0)).Sum()));
            }
            return j;
        }

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::ComputeCost