-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Moved TensorFlow samples to its own directory in Samples project. #2429
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7ec7fb0
Moved TensorFlow samples to its own directory in Samples project.
zeahmed 9d8dfa9
Addressed reviewers' comments.
zeahmed 0032e16
Addressed reviewers' comments.
zeahmed c6a5144
Addressed reviewers' comments.
zeahmed 00a5264
Addressed reviewers' comments.
zeahmed 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
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
136 changes: 136 additions & 0 deletions
136
docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/TextClassification.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,136 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.ML.Data; | ||
using Microsoft.ML.Transforms.TensorFlow; | ||
|
||
namespace Microsoft.ML.Samples.Dynamic.TensorFlow | ||
{ | ||
class TextClassification | ||
{ | ||
public const int MaxSentenceLenth = 600; | ||
/// <summary> | ||
/// Example use of the TensorFlow sentiment classification model. | ||
/// </summary> | ||
public static void ScoringWithTextClassificationModelSample() | ||
zeahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
string modelLocation = SamplesUtils.DatasetUtils.DownloadTensorFlowSentimentModel(); | ||
|
||
var mlContext = new MLContext(); | ||
var data = new[] { new IMDBSentiment() { | ||
Sentiment_Text = "this film was just brilliant casting location scenery story direction " + | ||
"everyone's really suited the part they played and you could just imagine being there robert " + | ||
"is an amazing actor and now the same being director father came from the same scottish " + | ||
"island as myself so i loved the fact there was a real connection with this film the witty " + | ||
"remarks throughout the film were great it was just brilliant so much that i bought the " + | ||
"film as soon as it was released for and would recommend it to everyone to watch and the " + | ||
"fly fishing was amazing really cried at the end it was so sad and you know what they say " + | ||
"if you cry at a film it must have been good and this definitely was also to the two " + | ||
"little boy's that played the of norman and paul they were just brilliant children are " + | ||
"often left out of the list i think because the stars that play them all grown up are " + | ||
"such a big profile for the whole film but these children are amazing and should be praised " + | ||
"for what they have done don't you think the whole story was so lovely because it was true " + | ||
"and was someone's life after all that was shared with us all" } }; | ||
var dataView = mlContext.Data.ReadFromEnumerable(data); | ||
|
||
// This is the dictionary to convert words into the integer indexes. | ||
var lookupMap = mlContext.Data.ReadFromTextFile(Path.Combine(modelLocation, "imdb_word_index.csv"), | ||
columns: new[] | ||
{ | ||
new TextLoader.Column("Words", DataKind.TX, 0), | ||
new TextLoader.Column("Ids", DataKind.I4, 1), | ||
}, | ||
separatorChar: ',' | ||
); | ||
|
||
// Load the TensorFlow model once. | ||
// - Use it for quering the schema for input and output in the model | ||
// - Use it for prediction in the pipeline. | ||
var modelInfo = TensorFlowUtils.LoadTensorFlowModel(mlContext, modelLocation); | ||
var schema = modelInfo.GetModelSchema(); | ||
var featuresType = (VectorType)schema["Features"].Type; | ||
Console.WriteLine("Name: {0}, Type: {1}, Shape: (-1, {2})", "Features", featuresType.ItemType.RawType, featuresType.Dimensions[0]); | ||
var predictionType = (VectorType)schema["Prediction/Softmax"].Type; | ||
Console.WriteLine("Name: {0}, Type: {1}, Shape: (-1, {2})", "Prediction/Softmax", predictionType.ItemType.RawType, predictionType.Dimensions[0]); | ||
|
||
// The model expects the input feature vector to be a fixed length vector. | ||
// In this sample, CustomMappingEstimator is used to resize variable length vector to fixed length vector. | ||
// The following ML.NET pipeline | ||
// 1. tokenzies the string into words, | ||
// 2. maps each word to an integer which is an index in the dictionary ('lookupMap'), | ||
// 3. Resizes the integer vector to a fixed length vector using CustomMappingEstimator ('ResizeFeaturesAction') | ||
// 4. Passes the data to TensorFlow for scoring. | ||
// 5. Retreives the 'Prediction' from TensorFlow and put it into ML.NET Pipeline | ||
|
||
Action<IMDBSentiment, IntermediateFeatures> ResizeFeaturesAction = (i, j) => | ||
{ | ||
j.Sentiment_Text = i.Sentiment_Text; | ||
var features = i.VariableLenghtFeatures; | ||
Array.Resize(ref features, MaxSentenceLenth); | ||
zeahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
j.Features = features; | ||
}; | ||
|
||
var engine = mlContext.Transforms.Text.TokenizeWords("TokenizedWords", "Sentiment_Text") | ||
.Append(mlContext.Transforms.Conversion.ValueMap(lookupMap, "Words", "Ids", new[] { ("VariableLenghtFeatures", "TokenizedWords") })) | ||
.Append(mlContext.Transforms.CustomMapping(ResizeFeaturesAction, "Resize")) | ||
.Append(mlContext.Transforms.ScoreTensorFlowModel(modelInfo, new[] { "Prediction/Softmax" }, new[] { "Features" })) | ||
.Append(mlContext.Transforms.CopyColumns(("Prediction", "Prediction/Softmax"))) | ||
.Fit(dataView) | ||
.CreatePredictionEngine<IMDBSentiment, OutputScores>(mlContext); | ||
|
||
// Predict with TensorFlow pipeline. | ||
var prediction = engine.Predict(data[0]); | ||
|
||
Console.WriteLine("Number of classes: {0}", prediction.Prediction.Length); | ||
Console.WriteLine("Is sentiment/review positive? {0}", prediction.Prediction[1] > 0.5 ? "Yes." : "No."); | ||
Console.WriteLine("Prediction Confidence: {0}", prediction.Prediction[1].ToString("0.00")); | ||
|
||
/////////////////////////////////// Expected output /////////////////////////////////// | ||
// | ||
// Name: Features, Type: System.Int32, Shape: (-1, 600) | ||
// Name: Prediction/Softmax, Type: System.Single, Shape: (-1, 2) | ||
// | ||
// Number of classes: 2 | ||
// Is sentiment/review positive ? Yes | ||
// Prediction Confidence: 0.65 | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Class to hold original sentiment data. | ||
/// </summary> | ||
public class IMDBSentiment | ||
{ | ||
public string Sentiment_Text { get; set; } | ||
|
||
/// <summary> | ||
/// This is a variable length vector designated by VectorType(0) attribute. | ||
/// Variable length vectors are produced by applying operations such as 'TokenizeWords' on strings | ||
/// resulting in vectors of tokens of variable lengths. | ||
/// </summary> | ||
[VectorType(0)] | ||
zeahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public int[] VariableLenghtFeatures { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Class to hold intermediate data. Mostly used by CustomMapping Estimator | ||
/// </summary> | ||
public class IntermediateFeatures | ||
{ | ||
public string Sentiment_Text { get; set; } | ||
|
||
[VectorType(MaxSentenceLenth)] | ||
public int[] Features { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Class to contain the output values from the transformation. | ||
/// </summary> | ||
class OutputScores | ||
{ | ||
[VectorType(2)] | ||
public float[] Prediction { get; set; } | ||
} | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ internal TensorFlowModelInfo(IHostEnvironment env, TFSession session, string mod | |
/// <summary> | ||
/// Get <see cref="Schema"/> for complete model. Every node in the TensorFlow model will be included in the <see cref="Schema"/> object. | ||
/// </summary> | ||
internal Schema GetModelSchema() | ||
public Schema GetModelSchema() | ||
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.
This need to be public. Its helpful to get the schema without loading model again. #Resolved |
||
{ | ||
return TensorFlowUtils.GetModelSchema(_env, Session.Graph); | ||
} | ||
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.