@@ -13,27 +13,36 @@ public static class TextClassification
1313 /// </summary>
1414 public static void Example ( )
1515 {
16- string modelLocation = Microsoft . ML . SamplesUtils . DatasetUtils . DownloadTensorFlowSentimentModel ( ) ;
16+ string modelLocation = Microsoft . ML . SamplesUtils . DatasetUtils
17+ . DownloadTensorFlowSentimentModel ( ) ;
1718
1819 var mlContext = new MLContext ( ) ;
1920 var data = new [ ] { new IMDBSentiment ( ) {
20- Sentiment_Text = "this film was just brilliant casting location scenery story direction " +
21- "everyone's really suited the part they played and you could just imagine being there robert " +
22- "is an amazing actor and now the same being director father came from the same scottish " +
23- "island as myself so i loved the fact there was a real connection with this film the witty " +
24- "remarks throughout the film were great it was just brilliant so much that i bought the " +
25- "film as soon as it was released for and would recommend it to everyone to watch and the " +
26- "fly fishing was amazing really cried at the end it was so sad and you know what they say " +
27- "if you cry at a film it must have been good and this definitely was also to the two " +
28- "little boy's that played the of norman and paul they were just brilliant children are " +
29- "often left out of the list i think because the stars that play them all grown up are " +
30- "such a big profile for the whole film but these children are amazing and should be praised " +
31- "for what they have done don't you think the whole story was so lovely because it was true " +
32- "and was someone's life after all that was shared with us all" } } ;
21+ Sentiment_Text = "this film was just brilliant casting location " +
22+ "scenery story direction everyone's really suited the part they " +
23+ "played and you could just imagine being there robert is an " +
24+ "amazing actor and now the same being director father came from " +
25+ "the same scottish island as myself so i loved the fact there " +
26+ "was a real connection with this film the witty remarks " +
27+ "throughout the film were great it was just brilliant so much " +
28+ "that i bought the film as soon as it was released for and " +
29+ "would recommend it to everyone to watch and the fly fishing was " +
30+ "amazing really cried at the end it was so sad and you know what " +
31+ "they say if you cry at a film it must have been good and this " +
32+ "definitely was also to the two little boy's that played the of " +
33+ "norman and paul they were just brilliant children are often " +
34+ "left out of the list i think because the stars that play them " +
35+ "all grown up are such a big profile for the whole film but " +
36+ "these children are amazing and should be praised for what " +
37+ "they have done don't you think the whole story was so lovely" +
38+ "because it was true and was someone's life after all that was" +
39+ "shared with us all" } } ;
3340 var dataView = mlContext . Data . LoadFromEnumerable ( data ) ;
3441
3542 // This is the dictionary to convert words into the integer indexes.
36- var lookupMap = mlContext . Data . LoadFromTextFile ( Path . Combine ( modelLocation , "imdb_word_index.csv" ) ,
43+ var lookupMap = mlContext . Data . LoadFromTextFile ( Path . Combine (
44+ modelLocation , "imdb_word_index.csv" ) ,
45+
3746 columns : new [ ]
3847 {
3948 new TextLoader . Column ( "Words" , DataKind . String , 0 ) ,
@@ -43,49 +52,79 @@ public static void Example()
4352 ) ;
4453
4554 // Load the TensorFlow model once.
46- // - Use it for quering the schema for input and output in the model
55+ // - Use it for quering the schema for input and output in the
56+ // model
4757 // - Use it for prediction in the pipeline.
48- var tensorFlowModel = mlContext . Model . LoadTensorFlowModel ( modelLocation ) ;
58+ var tensorFlowModel = mlContext . Model . LoadTensorFlowModel (
59+ modelLocation ) ;
4960 var schema = tensorFlowModel . GetModelSchema ( ) ;
5061 var featuresType = ( VectorDataViewType ) schema [ "Features" ] . Type ;
51- Console . WriteLine ( "Name: {0}, Type: {1}, Shape: (-1, {2})" , "Features" , featuresType . ItemType . RawType , featuresType . Dimensions [ 0 ] ) ;
52- var predictionType = ( VectorDataViewType ) schema [ "Prediction/Softmax" ] . Type ;
53- Console . WriteLine ( "Name: {0}, Type: {1}, Shape: (-1, {2})" , "Prediction/Softmax" , predictionType . ItemType . RawType , predictionType . Dimensions [ 0 ] ) ;
54-
55- // The model expects the input feature vector to be a fixed length vector.
56- // In this sample, CustomMappingEstimator is used to resize variable length vector to fixed length vector.
62+ Console . WriteLine ( "Name: {0}, Type: {1}, Shape: (-1, {2})" , "Features" ,
63+ featuresType . ItemType . RawType , featuresType . Dimensions [ 0 ] ) ;
64+
65+ var predictionType = ( VectorDataViewType ) schema [ "Prediction/Softmax" ]
66+ . Type ;
67+ Console . WriteLine ( "Name: {0}, Type: {1}, Shape: (-1, {2})" ,
68+ "Prediction/Softmax" , predictionType . ItemType . RawType ,
69+ predictionType . Dimensions [ 0 ] ) ;
70+
71+ // The model expects the input feature vector to be a fixed length
72+ // vector.
73+ // In this sample, CustomMappingEstimator is used to resize variable
74+ // length vector to fixed length vector.
5775 // The following ML.NET pipeline
5876 // 1. tokenzies the string into words,
59- // 2. maps each word to an integer which is an index in the dictionary ('lookupMap'),
60- // 3. Resizes the integer vector to a fixed length vector using CustomMappingEstimator ('ResizeFeaturesAction')
77+ // 2. maps each word to an integer which is an index in the
78+ // dictionary ('lookupMap'),
79+ // 3. Resizes the integer vector to a fixed length vector using
80+ // CustomMappingEstimator ('ResizeFeaturesAction')
6181 // 4. Passes the data to TensorFlow for scoring.
62- // 5. Retreives the 'Prediction' from TensorFlow and put it into ML.NET Pipeline
82+ // 5. Retreives the 'Prediction' from TensorFlow and put it into
83+ // ML.NET Pipeline
6384
64- Action < IMDBSentiment , IntermediateFeatures > ResizeFeaturesAction = ( i , j ) =>
85+ Action < IMDBSentiment , IntermediateFeatures > ResizeFeaturesAction =
86+ ( i , j ) =>
6587 {
6688 j . Sentiment_Text = i . Sentiment_Text ;
6789 var features = i . VariableLengthFeatures ;
6890 Array . Resize ( ref features , MaxSentenceLength ) ;
6991 j . Features = features ;
7092 } ;
7193
72- var model = mlContext . Transforms . Text . TokenizeIntoWords ( "TokenizedWords" , "Sentiment_Text" )
73- . Append ( mlContext . Transforms . Conversion . MapValue ( "VariableLengthFeatures" , lookupMap ,
74- lookupMap . Schema [ "Words" ] , lookupMap . Schema [ "Ids" ] , "TokenizedWords" ) )
75- . Append ( mlContext . Transforms . CustomMapping ( ResizeFeaturesAction , "Resize" ) )
76- . Append ( tensorFlowModel . ScoreTensorFlowModel ( "Prediction/Softmax" , "Features" ) )
77- . Append ( mlContext . Transforms . CopyColumns ( "Prediction" , "Prediction/Softmax" ) )
94+ var model =
95+ mlContext . Transforms . Text . TokenizeIntoWords (
96+ "TokenizedWords" ,
97+ "Sentiment_Text" )
98+ . Append ( mlContext . Transforms . Conversion . MapValue (
99+ "VariableLengthFeatures" ,
100+ lookupMap ,
101+ lookupMap . Schema [ "Words" ] ,
102+ lookupMap . Schema [ "Ids" ] ,
103+ "TokenizedWords" ) )
104+ . Append ( mlContext . Transforms . CustomMapping (
105+ ResizeFeaturesAction ,
106+ "Resize" ) )
107+ . Append ( tensorFlowModel . ScoreTensorFlowModel (
108+ "Prediction/Softmax" ,
109+ "Features" ) )
110+ . Append ( mlContext . Transforms . CopyColumns (
111+ "Prediction" ,
112+ "Prediction/Softmax" ) )
78113 . Fit ( dataView ) ;
79- var engine = mlContext . Model . CreatePredictionEngine < IMDBSentiment , OutputScores > ( model ) ;
114+ var engine = mlContext . Model . CreatePredictionEngine < IMDBSentiment ,
115+ OutputScores > ( model ) ;
80116
81117 // Predict with TensorFlow pipeline.
82118 var prediction = engine . Predict ( data [ 0 ] ) ;
83119
84- Console . WriteLine ( "Number of classes: {0}" , prediction . Prediction . Length ) ;
85- Console . WriteLine ( "Is sentiment/review positive? {0}" , prediction . Prediction [ 1 ] > 0.5 ? "Yes." : "No." ) ;
86- Console . WriteLine ( "Prediction Confidence: {0}" , prediction . Prediction [ 1 ] . ToString ( "0.00" ) ) ;
120+ Console . WriteLine ( "Number of classes: {0}" , prediction . Prediction
121+ . Length ) ;
122+ Console . WriteLine ( "Is sentiment/review positive? {0}" , prediction
123+ . Prediction [ 1 ] > 0.5 ? "Yes." : "No." ) ;
124+ Console . WriteLine ( "Prediction Confidence: {0}" , prediction . Prediction [ 1 ]
125+ . ToString ( "0.00" ) ) ;
87126
88- /////////////////////////////////// Expected output ///////// //////////////////////////
127+ ///////////////////////////// Expected output //////////////////////////
89128 //
90129 // Name: Features, Type: System.Int32, Shape: (-1, 600)
91130 // Name: Prediction/Softmax, Type: System.Single, Shape: (-1, 2)
@@ -105,8 +144,9 @@ public class IMDBSentiment
105144
106145 /// <summary>
107146 /// This is a variable length vector designated by VectorType attribute.
108- /// Variable length vectors are produced by applying operations such as 'TokenizeWords' on strings
109- /// resulting in vectors of tokens of variable lengths.
147+ /// Variable length vectors are produced by applying operations such as
148+ /// 'TokenizeWords' on strings resulting in vectors of tokens of
149+ /// variable lengths.
110150 /// </summary>
111151 [ VectorType ]
112152 public int [ ] VariableLengthFeatures { get ; set ; }
0 commit comments