Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.9k
Creation of components through MLContext, internalization, and renaming#2510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a54fa85949936340ba094a2ec77f49a311455ecb11205958cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Microsoft.ML.Data; | ||
| using Microsoft.ML.Trainers; | ||
| namespace Microsoft.ML.Samples.Dynamic | ||
| { | ||
| public class PriorTrainerSample | ||
| { | ||
| public static void Example() | ||
| { | ||
| // Downloading the dataset from github.com/dotnet/machinelearning. | ||
| // This will create a sentiment.tsv file in the filesystem. | ||
| // You can open this file, if you want to see the data. | ||
| string dataFile = SamplesUtils.DatasetUtils.DownloadSentimentDataset(); | ||
| // A preview of the data. | ||
| // Sentiment SentimentText | ||
| // 0 " :Erm, thank you. " | ||
| // 1 ==You're cool== | ||
| // 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. | ||
| var mlContext = new MLContext(); | ||
| // Step 1: Read the data as an IDataView. | ||
| // First, we define the reader: specify the data columns and where to find them in the text file. | ||
| var reader = mlContext.Data.CreateTextLoader( | ||
| columns: new[] | ||
| { | ||
| new TextLoader.Column("Sentiment", DataKind.R4, 0), | ||
| new TextLoader.Column("SentimentText", DataKind.Text, 1) | ||
| }, | ||
| hasHeader: true | ||
| ); | ||
| // Read the data | ||
| var data = reader.Read(dataFile); | ||
| // Split it between training and test data | ||
| var trainTestData = mlContext.BinaryClassification.TrainTestSplit(data); | ||
| // Step 2: Pipeline | ||
| // Featurize the text column through the FeaturizeText API. | ||
| // Then append a binary classifier, setting the "Label" column as the label of the dataset, and | ||
| // the "Features" column produced by FeaturizeText as the features column. | ||
| var pipeline = mlContext.Transforms.Text.FeaturizeText("Features", "SentimentText") | ||
| .AppendCacheCheckpoint(mlContext) // Add a data-cache step within a pipeline. | ||
| .Append(mlContext.BinaryClassification.Trainers.Prior(labelColumn: "Sentiment")); | ||
| // Step 3: Train the pipeline | ||
| var trainedPipeline = pipeline.Fit(trainTestData.TrainSet); | ||
| // Step 4: Evaluate on the test set | ||
| var transformedData = trainedPipeline.Transform(trainTestData.TestSet); | ||
| var evalMetrics = mlContext.BinaryClassification.Evaluate(transformedData, label: "Sentiment"); | ||
| // Step 5: Inspect the output | ||
| Console.WriteLine("Accuracy: " + evalMetrics.Accuracy); | ||
| // The Prior trainer outputs the proportion of a label in the dataset as the probability of that label. | ||
| // In this case it means that there is a split of around 64%-36% of positive and negative labels in the dataset. | ||
| // Expected output: | ||
| // Accuracy: 0.647058823529412 | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Microsoft.ML.Data; | ||
| using Microsoft.ML.Trainers; | ||
| namespace Microsoft.ML.Samples.Dynamic | ||
| { | ||
| public class RandomTrainerSample | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Zeeshan A, Shahab and me have PRs where we create BinaryClassification folder. | ||
| { | ||
| public static void Example() | ||
| { | ||
| // Downloading the dataset from github.com/dotnet/machinelearning. | ||
| // This will create a sentiment.tsv file in the filesystem. | ||
| // You can open this file, if you want to see the data. | ||
| string dataFile = SamplesUtils.DatasetUtils.DownloadSentimentDataset(); | ||
| // A preview of the data. | ||
| // Sentiment SentimentText | ||
| // 0 " :Erm, thank you. " | ||
| // 1 ==You're cool== | ||
| // 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. | ||
| var mlContext = new MLContext(seed: 1); | ||
| // Step 1: Read the data as an IDataView. | ||
| // First, we define the reader: specify the data columns and where to find them in the text file. | ||
| var reader = mlContext.Data.CreateTextLoader( | ||
| columns: new[] | ||
| { | ||
| new TextLoader.Column("Sentiment", DataKind.R4, 0), | ||
| new TextLoader.Column("SentimentText", DataKind.Text, 1) | ||
| }, | ||
| hasHeader: true | ||
| ); | ||
| // Read the data | ||
| var data = reader.Read(dataFile); | ||
| // Split it between training and test data | ||
| var trainTestData = mlContext.BinaryClassification.TrainTestSplit(data); | ||
| // Step 2: Pipeline | ||
| // Featurize the text column through the FeaturizeText API. | ||
| // Then append a binary classifier, setting the "Label" column as the label of the dataset, and | ||
| // the "Features" column produced by FeaturizeText as the features column. | ||
| var pipeline = mlContext.Transforms.Text.FeaturizeText("Features", "SentimentText") | ||
| .AppendCacheCheckpoint(mlContext) // Add a data-cache step within a pipeline. | ||
| .Append(mlContext.BinaryClassification.Trainers.Random()); | ||
| // Step 3: Train the pipeline | ||
| var trainedPipeline = pipeline.Fit(trainTestData.TrainSet); | ||
| // Step 4: Evaluate on the test set | ||
| var transformedData = trainedPipeline.Transform(trainTestData.TestSet); | ||
| var evalMetrics = mlContext.BinaryClassification.Evaluate(transformedData, label: "Sentiment"); | ||
| // Step 5: Inspect the output | ||
| Console.WriteLine("Accuracy: " + evalMetrics.Accuracy); | ||
| // We expect an output probability closet to 0.5 as the Random trainer outputs a random prediction. | ||
| // Regardless of the input features, the trainer will predict either positive or negative label with equal probability. | ||
| // Expected output (close to 0.5): | ||
artidoro marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // Accuracy: 0.588235294117647 | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using System; | ||
| using Microsoft.ML.Data; | ||
| namespace Microsoft.ML.Samples.Dynamic | ||
| { | ||
| public class CustomMappingSample | ||
| { | ||
| public static void Example() | ||
| { | ||
| // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging, | ||
| // as well as the source of randomness. | ||
| var mlContext = new MLContext(); | ||
| // Get a small dataset as an IEnumerable and convert it to an IDataView. | ||
| var data = SamplesUtils.DatasetUtils.GetInfertData(); | ||
| var trainData = mlContext.Data.ReadFromEnumerable(data); | ||
| // Preview of the data. | ||
| // | ||
| // Age Case Education Induced Parity PooledStratum RowNum ... | ||
| // 26 1 0-5yrs 1 6 3 1 ... | ||
| // 42 1 0-5yrs 1 1 1 2 ... | ||
| // 39 1 0-5yrs 2 6 4 3 ... | ||
| // 34 1 0-5yrs 2 4 2 4 ... | ||
| // 35 1 6-11yrs 1 3 32 5 ... | ||
| // We define the custom mapping between input and output rows that will be applied by the transformation. | ||
| Action<SamplesUtils.DatasetUtils.SampleInfertData, SampleInfertDataTransformed> mapping = | ||
| (input, output) => output.IsUnderThirty = input.Age < 30; | ||
| // Custom transformations can be used to transform data directly, or as part of a pipeline. Below we transform data directly. | ||
| var transformer = mlContext.Transforms.CustomMappingTransformer(mapping, null); | ||
| var transformedData = transformer.Transform(trainData); | ||
| // Preview of the data. | ||
| // | ||
| // IsUnderThirty Age Case Education Induced Parity PooledStratum RowNum ... | ||
| // true 26 1 0-5yrs 1 6 3 1 ... | ||
| // false 42 1 0-5yrs 1 1 1 2 ... | ||
| // false 39 1 0-5yrs 2 6 4 3 ... | ||
| // false 34 1 0-5yrs 2 4 2 4 ... | ||
| // false 35 1 6-11yrs 1 3 32 5 ... | ||
| // Here instead we use it as part of a pipeline of estimators. | ||
| var pipeline = mlContext.Transforms.CustomMapping(mapping, null) | ||
| .Append(mlContext.Transforms.Concatenate(outputColumnName: "Features", inputColumnNames: new[] { "Parity", "Induced" })) | ||
| // It is useful to add a caching checkpoint before a trainer that does several passes over the data. | ||
| .AppendCacheCheckpoint(mlContext) | ||
| // We use binary FastTree to predict the label column that was generated by the custom mapping at the first step of the pipeline. | ||
| .Append(mlContext.BinaryClassification.Trainers.FastTree(labelColumn: "IsUnderThirty")); | ||
| // We can train the pipeline and use it to transform data. | ||
| transformedData = pipeline.Fit(trainData).Transform(trainData); | ||
| } | ||
| // Represents the transformed infertility dataset. | ||
| public class SampleInfertDataTransformed | ||
| { | ||
| public int RowNum { get; set; } | ||
| public string Education { get; set; } | ||
| public bool IsUnderThirty { get; set; } | ||
| public float Parity { get; set; } | ||
| public float Induced { get; set; } | ||
| public float Case { get; set; } | ||
| public float Spontaneous { get; set; } | ||
| public float Stratum { get; set; } | ||
| public float PooledStratum { get; set; } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,6 +2,7 @@ | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
| using System; | ||
| using Microsoft.ML.Data; | ||
| using Microsoft.ML.Transforms; | ||
| @@ -61,5 +62,19 @@ public static OnnxScoringEstimator ApplyOnnxModel(this TransformsCatalog catalog | ||
| bool fallbackToCpu = false) | ||
| => new OnnxScoringEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnNames, inputColumnNames, modelFile, gpuDeviceId, fallbackToCpu); | ||
| /// <summary> | ||
| /// Creates a new instance of <see cref="DnnImageFeaturizerEstimator"/> which applies a pre-trained DNN model to featurize an image. | ||
| /// </summary> | ||
| /// <param name="catalog">The transform's catalog.</param> | ||
| /// <param name="outputColumnName">The name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param> | ||
| /// <param name="modelFactory">An extension method on the <see cref="DnnImageModelSelector"/> that creates a chain of two | ||
| /// <see cref="OnnxScoringEstimator"/> (one for preprocessing and one with a pretrained image DNN) with specific models | ||
| /// included in a package together with that extension method.</param> | ||
| /// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param> | ||
| public static DnnImageFeaturizerEstimator DnnFeaturizeImage(this TransformsCatalog catalog, | ||
artidoro marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| string outputColumnName, | ||
| Func<DnnImageFeaturizerInput, EstimatorChain<ColumnCopyingTransformer>> modelFactory, | ||
| string inputColumnName = null) | ||
| => new DnnImageFeaturizerEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnName, modelFactory, inputColumnName); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.