Default Collaborative Filtering cost function.
Inheritance: CostFunction
コード例 #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);
        }
コード例 #2
0
        private void CheckCofiGradient(double lambda)
        {
            Matrix X_s = Matrix.Rand(4, 3);
            Matrix T_s = Matrix.Rand(5, 3);

            Matrix Y_s = (X_s * T_s.T);
            Matrix R_s = Matrix.Zeros(Y_s.Rows, Y_s.Cols);

            Y_s[Matrix.Rand(Y_s.Rows, Y_s.Cols) > 0.5] = 0.0;
            R_s[Y_s == 0] = 1.0;

            Matrix X_ts = Matrix.NormRand(X_s.Rows, X_s.Cols);
            Matrix T_ts = Matrix.NormRand(T_s.Rows, T_s.Cols);

            ICostFunction costFunction = new CofiCostFunction() { R = R_s, X = X_ts, Y = Y_s.Unshape(), Lambda = 0, Regularizer = null, CollaborativeFeatures = 3 };
            costFunction.Initialize();

            Vector grad = costFunction.ComputeGradient(Vector.Combine(X_ts.Unshape(), T_ts.Unshape()));

            Vector numericalGrad = this.ComputeNumericalGradient(
                f => costFunction.ComputeCost(f),
                Vector.Combine(X_ts.Unshape(), T_ts.Unshape()));

            Assert.True(this.CheckNumericalGradient(numericalGrad, grad) < 0.0000000001);
        }