|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using Microsoft.ML.Data; |
| 4 | +using Microsoft.ML.FactorizationMachine; |
| 5 | + |
| 6 | +namespace Microsoft.ML.Samples.Dynamic |
| 7 | +{ |
| 8 | + public static class FFMBinaryClassificationWithOptions |
| 9 | + { |
| 10 | + public static void Example() |
| 11 | + { |
| 12 | + // Creating the ML.Net IHostEnvironment object, needed for the pipeline. |
| 13 | + var mlContext = new MLContext(); |
| 14 | + |
| 15 | + // Download and featurize the dataset. |
| 16 | + (var trainData, var testData) = SamplesUtils.DatasetUtils.LoadFeaturizedSentimentDataset(mlContext); |
| 17 | + |
| 18 | + // ML.NET doesn't cache data set by default. Therefore, if one reads a data set from a file and accesses it many times, it can be slow due to |
| 19 | + // expensive featurization and disk operations. When the considered data can fit into memory, a solution is to cache the data in memory. Caching is especially |
| 20 | + // helpful when working with iterative algorithms which needs many data passes. Since SDCA is the case, we cache. Inserting a |
| 21 | + // cache step in a pipeline is also possible, please see the construction of pipeline below. |
| 22 | + trainData = mlContext.Data.Cache(trainData); |
| 23 | + |
| 24 | + // Step 2: Pipeline |
| 25 | + // Create the 'FieldAwareFactorizationMachine' binary classifier, setting the "Sentiment" column as the label of the dataset, and |
| 26 | + // the "Features" column as the features column. |
| 27 | + var pipeline = new EstimatorChain<ITransformer>().AppendCacheCheckpoint(mlContext) |
| 28 | + .Append(mlContext.BinaryClassification.Trainers. |
| 29 | + FieldAwareFactorizationMachine( |
| 30 | + new FieldAwareFactorizationMachineTrainer.Options |
| 31 | + { |
| 32 | + FeatureColumn = "Features", |
| 33 | + LabelColumn = "Sentiment", |
| 34 | + LearningRate = 0.1f, |
| 35 | + Iters = 10 |
| 36 | + })); |
| 37 | + |
| 38 | + // Fit the model. |
| 39 | + var model = pipeline.Fit(trainData); |
| 40 | + |
| 41 | + // Let's get the model parameters from the model. |
| 42 | + var modelParams = model.LastTransformer.Model; |
| 43 | + |
| 44 | + // Let's inspect the model parameters. |
| 45 | + var featureCount = modelParams.GetFeatureCount(); |
| 46 | + var fieldCount = modelParams.GetFieldCount(); |
| 47 | + var latentDim = modelParams.GetLatentDim(); |
| 48 | + var linearWeights = modelParams.GetLinearWeights(); |
| 49 | + var latentWeights = modelParams.GetLatentWeights(); |
| 50 | + |
| 51 | + Console.WriteLine("The feature count is: " + featureCount); |
| 52 | + Console.WriteLine("The number of fields is: " + fieldCount); |
| 53 | + Console.WriteLine("The latent dimension is: " + latentDim); |
| 54 | + Console.WriteLine("The linear weights of some of the features are: " + |
| 55 | + string.Concat(Enumerable.Range(1, 10).Select(i => $"{linearWeights[i]:F4} "))); |
| 56 | + Console.WriteLine("The weights of some of the latent features are: " + |
| 57 | + string.Concat(Enumerable.Range(1, 10).Select(i => $"{latentWeights[i]:F4} "))); |
| 58 | + |
| 59 | + // The feature count is: 9374 |
| 60 | + // The number of fields is: 1 |
| 61 | + // The latent dimension is: 20 |
| 62 | + // The linear weights of some of the features are: 0.0410 0.0000 -0.0078 -0.0285 0.0000 0.0114 0.1313 0.0183 -0.0224 0.0166 |
| 63 | + // The weights of some of the latent features are: -0.0326 0.1127 0.0621 0.1446 0.2038 0.1608 0.2084 0.0141 0.2458 -0.0625 |
| 64 | + |
| 65 | + // Evaluate how the model is doing on the test data. |
| 66 | + var dataWithPredictions = model.Transform(testData); |
| 67 | + |
| 68 | + var metrics = mlContext.BinaryClassification.Evaluate(dataWithPredictions, "Sentiment"); |
| 69 | + SamplesUtils.ConsoleUtils.PrintMetrics(metrics); |
| 70 | + |
| 71 | + // Accuracy: 0.78 |
| 72 | + // AUC: 0.81 |
| 73 | + // F1 Score: 0.78 |
| 74 | + // Negative Precision: 0.78 |
| 75 | + // Negative Recall: 0.78 |
| 76 | + // Positive Precision: 0.78 |
| 77 | + // Positive Recall: 0.78 |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments