當前位置: 首頁>>代碼示例>>C#>>正文


C# Vector.Copy方法代碼示例

本文整理匯總了C#中numl.Math.LinearAlgebra.Vector.Copy方法的典型用法代碼示例。如果您正苦於以下問題:C# Vector.Copy方法的具體用法?C# Vector.Copy怎麽用?C# Vector.Copy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在numl.Math.LinearAlgebra.Vector的用法示例。


在下文中一共展示了Vector.Copy方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Logistic_Regression_Test_CostFunction_2_WithoutRegularization

        public void Logistic_Regression_Test_CostFunction_2_WithoutRegularization()
        {
            Matrix X = new[,] {
             { 8, 1, 6 },
             { 3, 5 ,7 },
             { 4, 9, 2 }};

            Vector y = new Vector(new double[] { 1, 1, 0 });
            Vector theta = new Vector(new double[] { 0, 1, 0 });

            ICostFunction logisticCostFunction = new LogisticCostFunction()
            {
                X = X,
                Y = y,
                Lambda = 0,
            };

            double cost = logisticCostFunction.ComputeCost(theta.Copy());

            theta = logisticCostFunction.ComputeGradient(theta.Copy());

            Assert.Equal(3.1067d, System.Math.Round(cost, 4));

            Assert.Equal(0.6093d, System.Math.Round(theta[0], 4));
            Assert.Equal(2.8988d, System.Math.Round(theta[1], 4));
            Assert.Equal(0.1131d, System.Math.Round(theta[2], 4));
        }
開發者ID:sethjuarez,項目名稱:numl,代碼行數:27,代碼來源:LogisticRegressionTests.cs

示例2: Run

        /// <summary>
        ///     Performs gradient descent to optomise theta parameters.
        /// </summary>
        /// <param name="theta">Initial Theta (Zeros)</param>
        /// <param name="x">Training set</param>
        /// <param name="y">Training labels</param>
        /// <param name="maxIterations">Maximum number of iterations to run gradient descent</param>
        /// <param name="learningRateAlpha">The learning rate (Alpha)</param>
        /// <param name="costFunction">Cost function to use for gradient descent</param>
        /// <param name="lambda">The regularization constant to apply</param>
        /// <param name="regularizer">The regularization function to apply</param>
        /// <returns></returns>
        public static Tuple<double, Vector> Run(
            Vector theta, 
            Matrix x, 
            Vector y, 
            int maxIterations, 
            double learningRateAlpha, 
            ICostFunction costFunction, 
            double lambda, 
            IRegularizer regularizer)
        {
            var bestTheta = theta.Copy();
            var bestCost = double.PositiveInfinity;

            double currentCost = 0;
            var currentGradient = theta.Copy();

            for (var i = 0; i <= maxIterations; i++)
            {
                currentCost = costFunction.ComputeCost(bestTheta, x, y, lambda, regularizer);
                currentGradient = costFunction.ComputeGradient(bestTheta, x, y, lambda, regularizer);

                if (currentCost < bestCost)
                {
                    bestTheta = bestTheta - learningRateAlpha * currentGradient;
                    bestCost = currentCost;
                }
                else
                {
                    learningRateAlpha = learningRateAlpha * 0.99;
                }
            }

            return new Tuple<double, Vector>(bestCost, bestTheta);
        }
開發者ID:ChewyMoon,項目名稱:Cupcake,代碼行數:46,代碼來源:GradientDescent.cs

示例3: Logistic_Regression_Test_CostFunction_1

        public void Logistic_Regression_Test_CostFunction_1()
        {
            Matrix X = new[,]
            {{ 1, 1, 1 },
             { 1, 1, 1 },
             { 1, 1, 1 },
             { 8, 1, 6 },
             { 3, 5 ,7 },
             { 4, 9, 2 }};

            Vector y = new Vector(new double[] { 1, 0, 1, 0, 1, 0 });
            Vector theta = new Vector(new double[] { 0, 1, 0 });

            ICostFunction logisticCostFunction = new LogisticCostFunction()
            {
                X = X,
                Y = y,
                Lambda = 3,
                Regularizer = new L2Regularizer()
            };

            double cost = logisticCostFunction.ComputeCost(theta.Copy());

            theta = logisticCostFunction.ComputeGradient(theta.Copy());

            Assert.Equal(2.2933d, System.Math.Round(cost, 4));

            Assert.Equal(1.6702d, System.Math.Round(theta[0], 4));
            Assert.Equal(2.1483d, System.Math.Round(theta[1], 4));
            Assert.Equal(1.0887d, System.Math.Round(theta[2], 4));
        }
開發者ID:sethjuarez,項目名稱:numl,代碼行數:31,代碼來源:LogisticRegressionTests.cs

示例4: Calc

 public static Vector Calc(Vector v, Func<int, double, double> f)
 {
     var result = v.Copy();
     for (int i = 0; i < v.Length; i++)
         result[i] = f(i, result[i]);
     return result;
 }
開發者ID:budbjames,項目名稱:numl,代碼行數:7,代碼來源:VectorStatics.cs

示例5: IncreaseDimensions

 /// <summary>
 /// Adds a specified number of polynomial features to the training / test Vector.
 /// </summary>
 /// <param name="x">Training / Testing record</param>
 /// <param name="polynomialFeatures">Number of polynomial features to add</param>
 /// <returns></returns>
 public static Vector IncreaseDimensions(Vector x, int polynomialFeatures)
 {
     Vector xtemp = x.Copy();
     int maxCols = xtemp.Length;
     for (int j = 0; j < maxCols - 1; j++)
     {
         for (int k = 0; k <= polynomialFeatures; k++)
         {
             for (int m = 0; m <= k; m++)
             {
                 double v = (System.Math.Pow(xtemp[j], k - m) * System.Math.Pow(xtemp[j + 1], m));
                 xtemp = xtemp.Insert(xtemp.Length - 1, v);
             }
         }
     }
     return xtemp;
 }
開發者ID:sethjuarez,項目名稱:numl,代碼行數:23,代碼來源:LogisticRegressionModel.cs

示例6: IncreaseDimensions

        /// <summary>
        ///     Adds a specified number of polynomial features to the training / test Vector.
        /// </summary>
        /// <param name="x">Training / Testing record</param>
        /// <param name="polynomialFeatures">Number of polynomial features to add</param>
        /// <returns></returns>
        public static Vector IncreaseDimensions(Vector x, int polynomialFeatures)
        {
            var xtemp = x.Copy();
            var maxCols = xtemp.Length;
            for (var j = 0; j < maxCols - 1; j++)
            {
                for (var k = 0; k <= polynomialFeatures; k++)
                {
                    for (var m = 0; m <= k; m++)
                    {
                        var v = Math.Pow(xtemp[j], (double)(k - m)) * Math.Pow(xtemp[j + 1], (double)m);
                        xtemp = xtemp.Insert(xtemp.Length - 1, v);
                    }
                }
            }

            return xtemp;
        }
開發者ID:ChewyMoon,項目名稱:Cupcake,代碼行數:24,代碼來源:FeatureDimensions.cs

示例7: Logistic_Regression_Test_CostFunction_1

        public void Logistic_Regression_Test_CostFunction_1()
        {
            Matrix X = new[,] 
            {{ 1, 1, 1 },
             { 1, 1, 1 },
             { 1, 1, 1 },
             { 8, 1, 6 },
             { 3, 5 ,7 },
             { 4, 9, 2 }};

            Vector y = new Vector(new double[] { 1, 0, 1, 0, 1, 0 });
            Vector theta = new Vector(new double[] { 0, 1, 0 });

            numl.Math.Functions.Cost.ICostFunction logisticCostFunction = new numl.Math.Functions.Cost.LogisticCostFunction();
            numl.Math.Functions.Regularization.IRegularizer regularizer = new numl.Math.Functions.Regularization.Regularization();

            double cost = logisticCostFunction.ComputeCost(theta.Copy(), X, y, 3, regularizer);
            
            theta = logisticCostFunction.ComputeGradient(theta.Copy(), X, y, 3, regularizer);

            Assert.AreEqual(2.2933d, System.Math.Round(cost, 4));

            Assert.AreEqual(1.6702d, System.Math.Round(theta[0], 4));
            Assert.AreEqual(2.1483d, System.Math.Round(theta[1], 4));
            Assert.AreEqual(1.0887d, System.Math.Round(theta[2], 4));
        }
開發者ID:m-abubakar,項目名稱:numl,代碼行數:26,代碼來源:LogisticRegressionTests.cs


注:本文中的numl.Math.LinearAlgebra.Vector.Copy方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。