StandardTrainersCatalog.Sdca 方法

定义

重载

Sdca(RegressionCatalog+RegressionTrainers, SdcaRegressionTrainer+Options)

使用高级选项创建 SdcaRegressionTrainer ,该选项使用线性回归模型预测目标。

Sdca(RegressionCatalog+RegressionTrainers, String, String, String, ISupportSdcaRegressionLoss, Nullable<Single>, Nullable<Single>, Nullable<Int32>)

创建 SdcaRegressionTrainer,该模型使用线性回归模型预测目标。

Sdca(RegressionCatalog+RegressionTrainers, SdcaRegressionTrainer+Options)

使用高级选项创建 SdcaRegressionTrainer ,该选项使用线性回归模型预测目标。

public static Microsoft.ML.Trainers.SdcaRegressionTrainer Sdca (this Microsoft.ML.RegressionCatalog.RegressionTrainers catalog, Microsoft.ML.Trainers.SdcaRegressionTrainer.Options options);
static member Sdca : Microsoft.ML.RegressionCatalog.RegressionTrainers * Microsoft.ML.Trainers.SdcaRegressionTrainer.Options -> Microsoft.ML.Trainers.SdcaRegressionTrainer
<Extension()>
Public Function Sdca (catalog As RegressionCatalog.RegressionTrainers, options As SdcaRegressionTrainer.Options) As SdcaRegressionTrainer

参数

catalog
RegressionCatalog.RegressionTrainers

回归目录训练器对象。

options
SdcaRegressionTrainer.Options

教练选项。

返回

示例

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Trainers;

namespace Samples.Dynamic.Trainers.Regression
{
    public static class SdcaWithOptions
    {
        public static void Example()
        {
            // Create a new context for ML.NET operations. It can be used for
            // exception tracking and logging, as a catalog of available operations
            // and as the source of randomness. Setting the seed to a fixed number
            // in this example to make outputs deterministic.
            var mlContext = new MLContext(seed: 0);

            // Create a list of training data points.
            var dataPoints = GenerateRandomDataPoints(1000);

            // Convert the list of data points to an IDataView object, which is
            // consumable by ML.NET API.
            var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints);

            // Define trainer options.
            var options = new SdcaRegressionTrainer.Options
            {
                LabelColumnName = nameof(DataPoint.Label),
                FeatureColumnName = nameof(DataPoint.Features),
                // Make the convergence tolerance tighter. It effectively leads to
                // more training iterations.
                ConvergenceTolerance = 0.02f,
                // Increase the maximum number of passes over training data. Similar
                // to ConvergenceTolerance, this value specifics the hard iteration
                // limit on the training algorithm.
                MaximumNumberOfIterations = 30,
                // Increase learning rate for bias.
                BiasLearningRate = 0.1f
            };

            // Define the trainer.
            var pipeline =
                mlContext.Regression.Trainers.Sdca(options);

            // Train the model.
            var model = pipeline.Fit(trainingData);

            // Create testing data. Use different random seed to make it different
            // from training data.
            var testData = mlContext.Data.LoadFromEnumerable(
                GenerateRandomDataPoints(5, seed: 123));

            // Run the model on test data set.
            var transformedTestData = model.Transform(testData);

            // Convert IDataView object to a list.
            var predictions = mlContext.Data.CreateEnumerable<Prediction>(
                transformedTestData, reuseRowObject: false).ToList();

            // Look at 5 predictions for the Label, side by side with the actual
            // Label for comparison.
            foreach (var p in predictions)
                Console.WriteLine($"Label: {p.Label:F3}, Prediction: {p.Score:F3}");

            // Expected output:
            //   Label: 0.985, Prediction: 0.927
            //   Label: 0.155, Prediction: 0.062
            //   Label: 0.515, Prediction: 0.439
            //   Label: 0.566, Prediction: 0.500
            //   Label: 0.096, Prediction: 0.078

            // Evaluate the overall metrics
            var metrics = mlContext.Regression.Evaluate(transformedTestData);
            PrintMetrics(metrics);

            // Expected output:
            //   Mean Absolute Error: 0.05
            //   Mean Squared Error: 0.00
            //   Root Mean Squared Error: 0.06
            //   RSquared: 0.97 (closer to 1 is better. The worst case is 0)
        }

        private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
            int seed = 0)
        {
            var random = new Random(seed);
            for (int i = 0; i < count; i++)
            {
                float label = (float)random.NextDouble();
                yield return new DataPoint
                {
                    Label = label,
                    // Create random features that are correlated with the label.
                    Features = Enumerable.Repeat(label, 50).Select(
                        x => x + (float)random.NextDouble()).ToArray()
                };
            }
        }

        // Example with label and 50 feature values. A data set is a collection of
        // such examples.
        private class DataPoint
        {
            public float Label { get; set; }
            [VectorType(50)]
            public float[] Features { get; set; }
        }

        // Class used to capture predictions.
        private class Prediction
        {
            // Original label.
            public float Label { get; set; }
            // Predicted score from the trainer.
            public float Score { get; set; }
        }

        // Print some evaluation metrics to regression problems.
        private static void PrintMetrics(RegressionMetrics metrics)
        {
            Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
            Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
            Console.WriteLine(
                "Root Mean Squared Error: " + metrics.RootMeanSquaredError);

            Console.WriteLine("RSquared: " + metrics.RSquared);
        }
    }
}

适用于

Sdca(RegressionCatalog+RegressionTrainers, String, String, String, ISupportSdcaRegressionLoss, Nullable<Single>, Nullable<Single>, Nullable<Int32>)

创建 SdcaRegressionTrainer,该模型使用线性回归模型预测目标。

public static Microsoft.ML.Trainers.SdcaRegressionTrainer Sdca (this Microsoft.ML.RegressionCatalog.RegressionTrainers catalog, string labelColumnName = "Label", string featureColumnName = "Features", string exampleWeightColumnName = default, Microsoft.ML.Trainers.ISupportSdcaRegressionLoss lossFunction = default, float? l2Regularization = default, float? l1Regularization = default, int? maximumNumberOfIterations = default);
static member Sdca : Microsoft.ML.RegressionCatalog.RegressionTrainers * string * string * string * Microsoft.ML.Trainers.ISupportSdcaRegressionLoss * Nullable<single> * Nullable<single> * Nullable<int> -> Microsoft.ML.Trainers.SdcaRegressionTrainer
<Extension()>
Public Function Sdca (catalog As RegressionCatalog.RegressionTrainers, Optional labelColumnName As String = "Label", Optional featureColumnName As String = "Features", Optional exampleWeightColumnName As String = Nothing, Optional lossFunction As ISupportSdcaRegressionLoss = Nothing, Optional l2Regularization As Nullable(Of Single) = Nothing, Optional l1Regularization As Nullable(Of Single) = Nothing, Optional maximumNumberOfIterations As Nullable(Of Integer) = Nothing) As SdcaRegressionTrainer

参数

catalog
RegressionCatalog.RegressionTrainers

回归目录训练器对象。

labelColumnName
String

标签列的名称。 列数据必须是 Single

featureColumnName
String

功能列的名称。 列数据必须是已知大小的向量 Single

exampleWeightColumnName
String

示例权重列的名称 (可选) 。

lossFunction
ISupportSdcaRegressionLoss

训练过程中最小化的 损失 函数。 例如,使用默认值 SquaredLoss 会导致最小平方的训练器。

l2Regularization
Nullable<Single>

正则化的 L2 权重。

l1Regularization
Nullable<Single>

L1 正则化 超参数。 较高的值往往会导致更稀疏的模型。

maximumNumberOfIterations
Nullable<Int32>

要对数据执行的最大传递数。

返回

示例

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic.Trainers.Regression
{
    public static class Sdca
    {
        public static void Example()
        {
            // Create a new context for ML.NET operations. It can be used for
            // exception tracking and logging, as a catalog of available operations
            // and as the source of randomness. Setting the seed to a fixed number
            // in this example to make outputs deterministic.
            var mlContext = new MLContext(seed: 0);

            // Create a list of training data points.
            var dataPoints = GenerateRandomDataPoints(1000);

            // Convert the list of data points to an IDataView object, which is
            // consumable by ML.NET API.
            var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints);

            // Define the trainer.
            var pipeline = mlContext.Regression.Trainers.Sdca(
                labelColumnName: nameof(DataPoint.Label),
                featureColumnName: nameof(DataPoint.Features));

            // Train the model.
            var model = pipeline.Fit(trainingData);

            // Create testing data. Use different random seed to make it different
            // from training data.
            var testData = mlContext.Data.LoadFromEnumerable(
                GenerateRandomDataPoints(5, seed: 123));

            // Run the model on test data set.
            var transformedTestData = model.Transform(testData);

            // Convert IDataView object to a list.
            var predictions = mlContext.Data.CreateEnumerable<Prediction>(
                transformedTestData, reuseRowObject: false).ToList();

            // Look at 5 predictions for the Label, side by side with the actual
            // Label for comparison.
            foreach (var p in predictions)
                Console.WriteLine($"Label: {p.Label:F3}, Prediction: {p.Score:F3}");

            // Expected output:
            //   Label: 0.985, Prediction: 0.960
            //   Label: 0.155, Prediction: 0.072
            //   Label: 0.515, Prediction: 0.455
            //   Label: 0.566, Prediction: 0.500
            //   Label: 0.096, Prediction: 0.079

            // Evaluate the overall metrics
            var metrics = mlContext.Regression.Evaluate(transformedTestData);
            PrintMetrics(metrics);

            // Expected output:
            //   Mean Absolute Error: 0.05
            //   Mean Squared Error: 0.00
            //   Root Mean Squared Error: 0.06
            //   RSquared: 0.97 (closer to 1 is better. The worst case is 0)
        }

        private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
            int seed = 0)
        {
            var random = new Random(seed);
            for (int i = 0; i < count; i++)
            {
                float label = (float)random.NextDouble();
                yield return new DataPoint
                {
                    Label = label,
                    // Create random features that are correlated with the label.
                    Features = Enumerable.Repeat(label, 50).Select(
                        x => x + (float)random.NextDouble()).ToArray()
                };
            }
        }

        // Example with label and 50 feature values. A data set is a collection of
        // such examples.
        private class DataPoint
        {
            public float Label { get; set; }
            [VectorType(50)]
            public float[] Features { get; set; }
        }

        // Class used to capture predictions.
        private class Prediction
        {
            // Original label.
            public float Label { get; set; }
            // Predicted score from the trainer.
            public float Score { get; set; }
        }

        // Print some evaluation metrics to regression problems.
        private static void PrintMetrics(RegressionMetrics metrics)
        {
            Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
            Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
            Console.WriteLine(
                "Root Mean Squared Error: " + metrics.RootMeanSquaredError);

            Console.WriteLine("RSquared: " + metrics.RSquared);
        }
    }
}

适用于