/// <summary> /// Predict a target using a field-aware factorization machine. /// </summary> /// <param name="catalog">The binary classifier catalog trainer object.</param> /// <param name="label">The label, or dependent variable.</param> /// <param name="features">The features, or independent variables.</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 model that was trained. The type of the model is <see cref="FieldAwareFactorizationMachineModelParameters"/>. /// 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 predicted output.</returns> public static (Scalar <float> score, Scalar <bool> predictedLabel) FieldAwareFactorizationMachine(this BinaryClassificationCatalog.BinaryClassificationTrainers catalog, Scalar <bool> label, Vector <float>[] features, FieldAwareFactorizationMachineTrainer.Options options, Action <FieldAwareFactorizationMachineModelParameters> onFit = null) { Contracts.CheckValue(label, nameof(label)); Contracts.CheckNonEmpty(features, nameof(features)); Contracts.CheckValueOrNull(options); Contracts.CheckValueOrNull(onFit); var rec = new CustomReconciler((env, labelCol, featureCols) => { var trainer = new FieldAwareFactorizationMachineTrainer(env, options); if (onFit != null) { return(trainer.WithOnFitDelegate(trans => onFit(trans.Model))); } else { return(trainer); } }, label, features); return(rec.Output); }
public void FieldAwareFactorizationMachine_Estimator() { var data = new TextLoader(Env, GetFafmBCLoaderArgs()) .Read(GetDataPath(TestDatasets.breastCancer.trainFilename)); var est = new FieldAwareFactorizationMachineTrainer(Env, new[] { "Feature1", "Feature2", "Feature3", "Feature4" }, "Label", advancedSettings: s => { s.Shuffle = false; s.Iters = 3; s.LatentDim = 7; }); TestEstimatorCore(est, data); Done(); }
/// <summary> /// Predict a target using a field-aware factorization machine. /// </summary> /// <param name="ctx">The binary classifier context trainer object.</param> /// <param name="label">The label, or dependent variable.</param> /// <param name="features">The features, or independent variables.</param> /// <param name="learningRate">Initial learning rate.</param> /// <param name="numIterations">Number of training iterations.</param> /// <param name="numLatentDimensions">Latent space dimensions.</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>/// <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 model that was trained. 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 predicted output.</returns> public static (Scalar <float> score, Scalar <bool> predictedLabel) FieldAwareFactorizationMachine(this BinaryClassificationContext.BinaryClassificationTrainers ctx, Scalar <bool> label, Vector <float>[] features, float learningRate = 0.1f, int numIterations = 5, int numLatentDimensions = 20, Action <FieldAwareFactorizationMachineTrainer.Arguments> advancedSettings = null, Action <FieldAwareFactorizationMachinePredictor> onFit = null) { Contracts.CheckValue(label, nameof(label)); Contracts.CheckNonEmpty(features, nameof(features)); Contracts.CheckParam(learningRate > 0, nameof(learningRate), "Must be positive"); Contracts.CheckParam(numIterations > 0, nameof(numIterations), "Must be positive"); Contracts.CheckParam(numLatentDimensions > 0, nameof(numLatentDimensions), "Must be positive"); Contracts.CheckValueOrNull(advancedSettings); Contracts.CheckValueOrNull(onFit); var rec = new CustomReconciler((env, labelCol, featureCols) => { var trainer = new FieldAwareFactorizationMachineTrainer(env, featureCols, labelCol, advancedSettings: args => { args.LearningRate = learningRate; args.Iters = numIterations; args.LatentDim = numLatentDimensions; advancedSettings?.Invoke(args); }); if (onFit != null) { return(trainer.WithOnFitDelegate(trans => onFit(trans.Model))); } else { return(trainer); } }, label, features); return(rec.Output); }
public void FfmBinaryClassificationWithAdvancedArguments() { var mlContext = new MLContext(seed: 0); var data = DatasetUtils.GenerateFfmSamples(500); var dataView = ComponentCreation.CreateDataView(mlContext, data.ToList()); var ffmArgs = new FieldAwareFactorizationMachineTrainer.Arguments(); // Customized the field names. ffmArgs.FeatureColumn = nameof(DatasetUtils.FfmExample.Field0); // First field. ffmArgs.ExtraFeatureColumns = new[] { nameof(DatasetUtils.FfmExample.Field1), nameof(DatasetUtils.FfmExample.Field2) }; var pipeline = new FieldAwareFactorizationMachineTrainer(mlContext, ffmArgs); var model = pipeline.Fit(dataView); var prediction = model.Transform(dataView); var metrics = mlContext.BinaryClassification.Evaluate(prediction); // Run a sanity check against a few of the metrics. Assert.InRange(metrics.Accuracy, 0.9, 1); Assert.InRange(metrics.Auc, 0.9, 1); Assert.InRange(metrics.Auprc, 0.9, 1); }