-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Public API for remaining learners #1901
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
docs/samples/Microsoft.ML.Samples/Dynamic/FieldAwareFactorizationMachine.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Microsoft.ML.Runtime.Data; | ||
using Microsoft.ML.Runtime.FactorizationMachine; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Microsoft.ML.Samples.Dynamic | ||
{ | ||
public class FFM_BinaryClassificationExample | ||
{ | ||
public static void FFM_BinaryClassification() | ||
{ | ||
// 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.CreateTextReader( | ||
columns: new[] | ||
{ | ||
new TextLoader.Column("Sentiment", DataKind.BL, 0), | ||
new TextLoader.Column("SentimentText", DataKind.Text, 1) | ||
}, | ||
hasHeader: true | ||
); | ||
|
||
// Read the data | ||
var data = reader.Read(dataFile); | ||
|
||
// 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 | ||
// 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 | ||
// helpful when working with iterative algorithms which needs many data passes. Since SDCA is the case, we cache. Inserting a | ||
// cache step in a pipeline is also possible, please see the construction of pipeline below. | ||
data = mlContext.Data.Cache(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("SentimentText", "Features") | ||
.AppendCacheCheckpoint(mlContext) // Add a data-cache step within a pipeline. | ||
.Append(mlContext.BinaryClassification.Trainers.FieldAwareFactorizationMachine(labelColumn: "Sentiment", featureColumns: new[] { "Features" })); | ||
|
||
// Fit the model. | ||
var model = pipeline.Fit(data); | ||
|
||
// Let's get the model parameters from the model. | ||
var modelParams = model.LastTransformer.Model; | ||
|
||
// Let's inspect the model parameters. | ||
var featureCount = modelParams.GetFeatureCount(); | ||
var fieldCount = modelParams.GetFieldCount(); | ||
var latentDim = modelParams.GetLatentDim(); | ||
var linearWeights = modelParams.GetLinearWeights(); | ||
var latentWeights = modelParams.GetLatentWeights(); | ||
|
||
Console.WriteLine("The feature count is: " + featureCount); | ||
Console.WriteLine("The number of fields is: " + fieldCount); | ||
Console.WriteLine("The latent dimension is: " + latentDim); | ||
Console.WriteLine("The lineear weights of the features are: " + string.Join(", ", linearWeights)); | ||
Console.WriteLine("The weights of the latent features are: " + string.Join(", ", latentWeights)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
double-checking that this is intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must be. I did not touch this code, except Float -> float!
In reply to: 243714825 [](ancestors = 243714825)