/// <summary> /// Predict a target using a linear binary classification model trained with the SDCA trainer, and a custom loss. /// Note that because we cannot be sure that all loss functions will produce naturally calibrated outputs, setting /// a custom loss function will not produce a calibrated probability column. /// </summary> /// <param name="catalog">The binary classification catalog trainer object.</param> /// <param name="label">The label, or dependent variable.</param> /// <param name="features">The features, or independent variables.</param> /// <param name="lossFunction">The custom loss.</param> /// <param name="weights">The optional example weights.</param> /// <param name="l2Regularization">The L2 regularization hyperparameter.</param> /// <param name="l1Threshold">The L1 regularization hyperparameter. Higher values will tend to lead to more sparse model.</param> /// <param name="numberOfIterations">The maximum number of passes to perform over the data.</param> /// <param name="onFit">A delegate that is called every time the /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive /// the linear model that was trained, as well as the calibrator on top of that model. Note that this action cannot change the /// result in any way; it is only a way for the caller to be informed about what was learnt.</param> /// <returns>The set of output columns including in order the predicted binary classification score (which will range /// from negative to positive infinity), and the predicted label.</returns> public static (Scalar <float> score, Scalar <bool> predictedLabel) SdcaNonCalibrated( this BinaryClassificationCatalog.BinaryClassificationTrainers catalog, Scalar <bool> label, Vector <float> features, ISupportSdcaClassificationLoss lossFunction, Scalar <float> weights = null, float?l2Regularization = null, float?l1Threshold = null, int?numberOfIterations = null, Action <LinearBinaryModelParameters> onFit = null) { Contracts.CheckValue(label, nameof(label)); Contracts.CheckValue(features, nameof(features)); Contracts.CheckValue(lossFunction, nameof(lossFunction)); Contracts.CheckValueOrNull(weights); Contracts.CheckParam(!(l2Regularization < 0), nameof(l2Regularization), "Must not be negative, if specified."); Contracts.CheckParam(!(l1Threshold < 0), nameof(l1Threshold), "Must not be negative, if specified."); Contracts.CheckParam(!(numberOfIterations < 1), nameof(numberOfIterations), "Must be positive if specified"); Contracts.CheckValueOrNull(onFit); var rec = new TrainerEstimatorReconciler.BinaryClassifierNoCalibration( (env, labelName, featuresName, weightsName) => { var trainer = new SdcaNonCalibratedBinaryTrainer(env, labelName, featuresName, weightsName, lossFunction, l2Regularization, l1Threshold, numberOfIterations); if (onFit != null) { return(trainer.WithOnFitDelegate(trans => { onFit(trans.Model); })); } return(trainer); }, label, features, weights); return(rec.Output); }
/// <summary> /// Predict a target using a linear binary classification model trained with the SDCA trainer, and a custom loss. /// Note that because we cannot be sure that all loss functions will produce naturally calibrated outputs, setting /// a custom loss function will not produce a calibrated probability column. /// </summary> /// <param name="catalog">The binary classification catalog trainer object.</param> /// <param name="label">The label, or dependent variable.</param> /// <param name="features">The features, or independent variables.</param> /// <param name="lossFunction">The custom loss.</param> /// <param name="weights">The optional example weights.</param> /// <param name="options">Advanced arguments to the algorithm.</param> /// <param name="onFit">A delegate that is called every time the /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive /// the linear model that was trained, as well as the calibrator on top of that model. Note that this action cannot change the /// result in any way; it is only a way for the caller to be informed about what was learnt.</param> /// <returns>The set of output columns including in order the predicted binary classification score (which will range /// from negative to positive infinity), and the predicted label.</returns> public static (Scalar <float> score, Scalar <bool> predictedLabel) SdcaNonCalibrated( this BinaryClassificationCatalog.BinaryClassificationTrainers catalog, Scalar <bool> label, Vector <float> features, Scalar <float> weights, ISupportSdcaClassificationLoss lossFunction, SdcaNonCalibratedBinaryTrainer.Options options, Action <LinearBinaryModelParameters> onFit = null) { Contracts.CheckValue(label, nameof(label)); Contracts.CheckValue(features, nameof(features)); Contracts.CheckValueOrNull(weights); Contracts.CheckValueOrNull(options); Contracts.CheckValueOrNull(onFit); var rec = new TrainerEstimatorReconciler.BinaryClassifierNoCalibration( (env, labelName, featuresName, weightsName) => { options.FeatureColumnName = featuresName; options.LabelColumnName = labelName; var trainer = new SdcaNonCalibratedBinaryTrainer(env, options); if (onFit != null) { return(trainer.WithOnFitDelegate(trans => { onFit(trans.Model); })); } return(trainer); }, label, features, weights); return(rec.Output); }
PredictSdcaClassification <TVal>(this Key <uint, TVal> label, Vector <float> features, ISupportSdcaClassificationLoss loss = null, Scalar <float> weights = null, float?l2Const = null, float?l1Threshold = null, int?maxIterations = null, Action <MulticlassLogisticRegressionPredictor> onFit = null) {
public SdcaMultiClassTrainer(IHostEnvironment env, Arguments args) : base(args, env, LoadNameValue) { _loss = args.LossFunction.CreateComponent(env); base.Loss = _loss; NeedShuffle = args.Shuffle; _args = args; }
public SdcaMultiClassTrainer(IHostEnvironment env, Arguments args, string featureColumn, string labelColumn, string weightColumn = null) : base(Contracts.CheckRef(env, nameof(env)).Register(LoadNameValue), args, MakeFeatureColumn(featureColumn), MakeLabelColumn(labelColumn), MakeWeightColumn(weightColumn)) { _loss = args.LossFunction.CreateComponent(env); Loss = _loss; _args = args; }
Sdca <TVal>(this MulticlassClassificationContext.MulticlassClassificationTrainers ctx, Key <uint, TVal> label, Vector <float> features, ISupportSdcaClassificationLoss loss = null, Scalar <float> weights = null, float?l2Const = null, float?l1Threshold = null, int?maxIterations = null, Action <MulticlassLogisticRegressionModelParameters> onFit = null) {
this MulticlassClassificationCatalog.MulticlassClassificationTrainers catalog, Key <uint, TVal> label, Vector <float> features, ISupportSdcaClassificationLoss loss = null, Scalar <float> weights = null, float?l2Regularization = null, float?l1Threshold = null, int?numberOfIterations = null, Action <MulticlassLogisticRegressionModelParameters> onFit = null) {
/// <summary> /// Predict a target using a linear binary classification model trained with the SDCA trainer, and a custom loss. /// Note that because we cannot be sure that all loss functions will produce naturally calibrated outputs, setting /// a custom loss function will not produce a calibrated probability column. /// </summary> /// <param name="ctx">The binary classification context trainer object.</param> /// <param name="label">The label, or dependent variable.</param> /// <param name="features">The features, or independent variables.</param> /// <param name="loss">The custom loss.</param> /// <param name="weights">The optional example weights.</param> /// <param name="l2Const">The L2 regularization hyperparameter.</param> /// <param name="l1Threshold">The L1 regularization hyperparameter. Higher values will tend to lead to more sparse model.</param> /// <param name="maxIterations">The maximum number of passes to perform over the data.</param> /// <param name="onFit">A delegate that is called every time the /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}"/> instance created out of this. This delegate will receive /// the linear model that was trained, as well as the calibrator on top of that model. Note that this action cannot change the /// result in any way; it is only a way for the caller to be informed about what was learnt.</param> /// <returns>The set of output columns including in order the predicted binary classification score (which will range /// from negative to positive infinity), and the predicted label.</returns> /// <seealso cref="Sdca(BinaryClassificationContext.BinaryClassificationTrainers, Scalar{bool}, Vector{float}, Scalar{float}, float?, float?, int?, Action{LinearBinaryPredictor, ParameterMixingCalibratedPredictor})"/> public static (Scalar <float> score, Scalar <bool> predictedLabel) Sdca( this BinaryClassificationContext.BinaryClassificationTrainers ctx, Scalar <bool> label, Vector <float> features, ISupportSdcaClassificationLoss loss, Scalar <float> weights = null, float?l2Const = null, float?l1Threshold = null, int?maxIterations = null, Action <LinearBinaryPredictor> onFit = null ) { Contracts.CheckValue(label, nameof(label)); Contracts.CheckValue(features, nameof(features)); Contracts.CheckValue(loss, nameof(loss)); Contracts.CheckValueOrNull(weights); Contracts.CheckParam(!(l2Const < 0), nameof(l2Const), "Must not be negative, if specified."); Contracts.CheckParam(!(l1Threshold < 0), nameof(l1Threshold), "Must not be negative, if specified."); Contracts.CheckParam(!(maxIterations < 1), nameof(maxIterations), "Must be positive if specified"); Contracts.CheckValueOrNull(onFit); bool hasProbs = loss is LogLoss; var args = new LinearClassificationTrainer.Arguments() { L2Const = l2Const, L1Threshold = l1Threshold, MaxIterations = maxIterations, LossFunction = new TrivialSdcaClassificationLossFactory(loss) }; var rec = new TrainerEstimatorReconciler.BinaryClassifierNoCalibration( (env, labelName, featuresName, weightsName) => { var trainer = new LinearClassificationTrainer(env, args, featuresName, labelName, weightsName); if (onFit != null) { return(trainer.WithOnFitDelegate(trans => { var model = trans.Model; if (model is ParameterMixingCalibratedPredictor cali) { onFit((LinearBinaryPredictor)cali.SubPredictor); } else { onFit((LinearBinaryPredictor)model); } })); } return(trainer); }, label, features, weights, hasProbs); return(rec.Output); }
Sdca <TVal>(this MulticlassClassificationContext.MulticlassClassificationTrainers ctx, Key <uint, TVal> label, Vector <float> features, ISupportSdcaClassificationLoss loss = null, Scalar <float> weights = null, float?l2Const = null, float?l1Threshold = null, int?maxIterations = null, Action <SdcaMultiClassTrainer.Arguments> advancedSettings = null, Action <MulticlassLogisticRegressionPredictor> onFit = null) {
public SdcaMultiClassTrainer(IHostEnvironment env, Arguments args, string featureColumn, string labelColumn, string weightColumn = null) : base(Contracts.CheckRef(env, nameof(env)).Register(LoadNameValue), args, TrainerUtils.MakeR4VecFeature(featureColumn), TrainerUtils.MakeU4ScalarLabel(labelColumn), TrainerUtils.MakeR4ScalarWeightColumn(weightColumn)) { Host.CheckValue(labelColumn, nameof(labelColumn)); Host.CheckValue(featureColumn, nameof(featureColumn)); _loss = args.LossFunction.CreateComponent(env); Loss = _loss; _args = args; }
internal SdcaNonCalibratedMulticlassTrainer(IHostEnvironment env, string labelColumn = DefaultColumnNames.Label, string featureColumn = DefaultColumnNames.Features, string weights = null, ISupportSdcaClassificationLoss loss = null, float?l2Const = null, float?l1Threshold = null, int?maxIterations = null) : base(env, labelColumn: labelColumn, featureColumn: featureColumn, weights: weights, loss: loss, l2Const: l2Const, l1Threshold: l1Threshold, maxIterations: maxIterations) { }
public SdcaMultiClassTrainer(IHostEnvironment env, Arguments args, string featureColumn, string labelColumn, string weightColumn = null) : base(Contracts.CheckRef(env, nameof(env)).Register(LoadNameValue), args, MakeFeatureColumn(featureColumn), MakeLabelColumn(labelColumn), MakeWeightColumn(weightColumn)) { _loss = args.LossFunction.CreateComponent(env); Loss = _loss; _args = args; OutputColumns = new[] { new SchemaShape.Column(DefaultColumnNames.Score, SchemaShape.Column.VectorKind.Vector, DataKind.R4, false), new SchemaShape.Column(DefaultColumnNames.PredictedLabel, SchemaShape.Column.VectorKind.Scalar, DataKind.U4, true) }; }
/// <summary> /// Predict a target using a linear multiclass classification model trained with the SDCA trainer. /// </summary> /// <param name="catalog">The multiclass classification catalog trainer object.</param> /// <param name="labelColumn">The labelColumn, or dependent variable.</param> /// <param name="featureColumn">The features, or independent variables.</param> /// <param name="loss">The optional custom loss.</param> /// <param name="weights">The optional example weights.</param> /// <param name="l2Const">The L2 regularization hyperparameter.</param> /// <param name="l1Threshold">The L1 regularization hyperparameter. Higher values will tend to lead to more sparse model.</param> /// <param name="maxIterations">The maximum number of passes to perform over the data.</param> public static SdcaMultiClassTrainer StochasticDualCoordinateAscent(this MulticlassClassificationCatalog.MulticlassClassificationTrainers catalog, string labelColumn = DefaultColumnNames.Label, string featureColumn = DefaultColumnNames.Features, string weights = null, ISupportSdcaClassificationLoss loss = null, float?l2Const = null, float?l1Threshold = null, int?maxIterations = null) { Contracts.CheckValue(catalog, nameof(catalog)); var env = CatalogUtils.GetEnvironment(catalog); return(new SdcaMultiClassTrainer(env, labelColumn, featureColumn, weights, loss, l2Const, l1Threshold, maxIterations)); }
/// <summary> /// Predict a target using a linear multiclass classification model trained with the SDCA trainer. /// </summary> /// <param name="ctx">The multiclass classification context trainer object.</param> /// <param name="label">The label, or dependent variable.</param> /// <param name="features">The features, or independent variables.</param> /// <param name="loss">The optional custom loss.</param> /// <param name="weights">The optional example weights.</param> /// <param name="l2Const">The L2 regularization hyperparameter.</param> /// <param name="l1Threshold">The L1 regularization hyperparameter. Higher values will tend to lead to more sparse model.</param> /// <param name="maxIterations">The maximum number of passes to perform over the data.</param> /// <param name="advancedSettings">A delegate to set more settings.</param> public static SdcaMultiClassTrainer StochasticDualCoordinateAscent(this MulticlassClassificationContext.MulticlassClassificationTrainers ctx, string label = DefaultColumnNames.Label, string features = DefaultColumnNames.Features, string weights = null, ISupportSdcaClassificationLoss loss = null, float?l2Const = null, float?l1Threshold = null, int?maxIterations = null, Action <SdcaMultiClassTrainer.Arguments> advancedSettings = null) { Contracts.CheckValue(ctx, nameof(ctx)); var env = CatalogUtils.GetEnvironment(ctx); return(new SdcaMultiClassTrainer(env, features, label, weights, loss, l2Const, l1Threshold, maxIterations, advancedSettings)); }
/// <summary> /// Initializes a new instance of <see cref="SdcaMultiClassTrainer"/> /// </summary> /// <param name="env">The environment to use.</param> /// <param name="featureColumn">The features, or independent variables.</param> /// <param name="labelColumn">The label, or dependent variable.</param> /// <param name="loss">The custom loss.</param> /// <param name="weightColumn">The optional example weights.</param> /// <param name="l2Const">The L2 regularization hyperparameter.</param> /// <param name="l1Threshold">The L1 regularization hyperparameter. Higher values will tend to lead to more sparse model.</param> /// <param name="maxIterations">The maximum number of passes to perform over the data.</param> /// <param name="advancedSettings">A delegate to set more settings. /// The settings here will override the ones provided in the direct method signature, /// if both are present and have different values. /// The columns names, however need to be provided directly, not through the <paramref name="advancedSettings"/>.</param> public SdcaMultiClassTrainer(IHostEnvironment env, string featureColumn, string labelColumn, string weightColumn = null, ISupportSdcaClassificationLoss loss = null, float?l2Const = null, float?l1Threshold = null, int?maxIterations = null, Action <Arguments> advancedSettings = null) : base(env, featureColumn, TrainerUtils.MakeU4ScalarColumn(labelColumn), TrainerUtils.MakeR4ScalarWeightColumn(weightColumn), advancedSettings, l2Const, l1Threshold, maxIterations) { Host.CheckNonEmpty(featureColumn, nameof(featureColumn)); Host.CheckNonEmpty(labelColumn, nameof(labelColumn)); _loss = loss ?? Args.LossFunction.CreateComponent(env); Loss = _loss; }
/// <summary> /// Predict a target using a linear binary classification model trained with the SDCA trainer, and a custom loss. /// Note that because we cannot be sure that all loss functions will produce naturally calibrated outputs, setting /// a custom loss function will not produce a calibrated probability column. /// </summary> /// <param name="catalog">The binary classification catalog trainer object.</param> /// <param name="label">The label, or dependent variable.</param> /// <param name="features">The features, or independent variables.</param> /// <param name="loss">The custom loss.</param> /// <param name="weights">The optional example weights.</param> /// <param name="l2Const">The L2 regularization hyperparameter.</param> /// <param name="l1Threshold">The L1 regularization hyperparameter. Higher values will tend to lead to more sparse model.</param> /// <param name="maxIterations">The maximum number of passes to perform over the data.</param> /// <param name="onFit">A delegate that is called every time the /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive /// the linear model that was trained, as well as the calibrator on top of that model. Note that this action cannot change the /// result in any way; it is only a way for the caller to be informed about what was learnt.</param> /// <returns>The set of output columns including in order the predicted binary classification score (which will range /// from negative to positive infinity), and the predicted label.</returns> public static (Scalar <float> score, Scalar <bool> predictedLabel) Sdca( this BinaryClassificationCatalog.BinaryClassificationTrainers catalog, Scalar <bool> label, Vector <float> features, ISupportSdcaClassificationLoss loss, Scalar <float> weights = null, float?l2Const = null, float?l1Threshold = null, int?maxIterations = null, Action <LinearBinaryModelParameters> onFit = null ) { Contracts.CheckValue(label, nameof(label)); Contracts.CheckValue(features, nameof(features)); Contracts.CheckValue(loss, nameof(loss)); Contracts.CheckValueOrNull(weights); Contracts.CheckParam(!(l2Const < 0), nameof(l2Const), "Must not be negative, if specified."); Contracts.CheckParam(!(l1Threshold < 0), nameof(l1Threshold), "Must not be negative, if specified."); Contracts.CheckParam(!(maxIterations < 1), nameof(maxIterations), "Must be positive if specified"); Contracts.CheckValueOrNull(onFit); bool hasProbs = loss is LogLoss; var rec = new TrainerEstimatorReconciler.BinaryClassifierNoCalibration( (env, labelName, featuresName, weightsName) => { var trainer = new SdcaBinaryTrainer(env, labelName, featuresName, weightsName, loss, l2Const, l1Threshold, maxIterations); if (onFit != null) { return(trainer.WithOnFitDelegate(trans => { var model = trans.Model; if (model is ParameterMixingCalibratedModelParameters <LinearBinaryModelParameters, PlattCalibrator> cali) { onFit(cali.SubModel); } else { onFit((LinearBinaryModelParameters)model); } })); } return(trainer); }, label, features, weights, hasProbs); return(rec.Output); }
/// <summary> /// Predict a target using a linear binary classification model trained with the SDCA trainer, and a custom loss. /// Note that because we cannot be sure that all loss functions will produce naturally calibrated outputs, setting /// a custom loss function will not produce a calibrated probability column. /// </summary> /// <param name="ctx">The binary classification context trainer object.</param> /// <param name="label">The label, or dependent variable.</param> /// <param name="features">The features, or independent variables.</param> /// <param name="loss">The custom loss.</param> /// <param name="weights">The optional example weights.</param> /// <param name="options">Advanced arguments to the algorithm.</param> /// <param name="onFit">A delegate that is called every time the /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive /// the linear model that was trained, as well as the calibrator on top of that model. Note that this action cannot change the /// result in any way; it is only a way for the caller to be informed about what was learnt.</param> /// <returns>The set of output columns including in order the predicted binary classification score (which will range /// from negative to positive infinity), and the predicted label.</returns> public static (Scalar <float> score, Scalar <bool> predictedLabel) Sdca( this BinaryClassificationContext.BinaryClassificationTrainers ctx, Scalar <bool> label, Vector <float> features, Scalar <float> weights, ISupportSdcaClassificationLoss loss, SdcaBinaryTrainer.Options options, Action <LinearBinaryModelParameters> onFit = null ) { Contracts.CheckValue(label, nameof(label)); Contracts.CheckValue(features, nameof(features)); Contracts.CheckValueOrNull(weights); Contracts.CheckValueOrNull(options); Contracts.CheckValueOrNull(onFit); bool hasProbs = loss is LogLoss; var rec = new TrainerEstimatorReconciler.BinaryClassifierNoCalibration( (env, labelName, featuresName, weightsName) => { options.FeatureColumn = featuresName; options.LabelColumn = labelName; var trainer = new SdcaBinaryTrainer(env, options); if (onFit != null) { return(trainer.WithOnFitDelegate(trans => { var model = trans.Model; if (model is ParameterMixingCalibratedPredictor cali) { onFit((LinearBinaryModelParameters)cali.SubPredictor); } else { onFit((LinearBinaryModelParameters)model); } })); } return(trainer); }, label, features, weights, hasProbs); return(rec.Output); }
public static PredictionEngine <TIn, TOut> SdcaNonCalibrated <TIn, TOut>( IEnumerable <TIn> trainDataset, string outputColumnName = "PredictedLabel", string exampleWeightColumnName = null, ISupportSdcaClassificationLoss lossFunction = null, float?l1Regularization = null, float?l2Regularization = null, int?maximumNumberOfIterations = null, Action <ITransformer> additionModelAction = null ) where TIn : class, new() where TOut : class, new() { var context = new MLContext(); var type = typeof(TIn); var labelColumnName = Preprocessing.LabelColumn(type.GetProperties()).Name; var properties = Preprocessing.ExcludeColumns(type.GetProperties()); var preprocessor = context.OneHotEncoding(properties); var trainDataframe = context.Data.LoadFromEnumerable(trainDataset); var pipeline = context.Transforms.Conversion.MapValueToKey(labelColumnName) .Append(preprocessor.OneHotEncodingEstimator) .Append(context.Transforms.Concatenate("Features", preprocessor.CombinedFeatures.ToArray())) .Append(context.Transforms.ProjectToPrincipalComponents(outputColumnName: "PCAFeatures", inputColumnName: "Features", rank: 2)) .AppendCacheCheckpoint(context) .Append(context.MulticlassClassification.Trainers.SdcaNonCalibrated( labelColumnName, featureColumnName: "Features", exampleWeightColumnName, lossFunction, l2Regularization, l1Regularization, maximumNumberOfIterations )) .Append(context.Transforms.Conversion.MapKeyToValue(outputColumnName)); var model = pipeline.Fit(trainDataframe); var predictEngine = context.Model.CreatePredictionEngine <TIn, TOut>(model); additionModelAction?.Invoke(model); return(predictEngine); }
public TrivialClassificationLossFactory(ISupportSdcaClassificationLoss loss) { _loss = loss; }