33using System . IO ;
44using Microsoft . ML ;
55using Microsoft . ML . Trainers ;
6- using Microsoft . ML . Data ;
7- using Microsoft . Data . DataView ;
86// </SnippetUsingStatements>
97
108namespace MovieRecommendation
@@ -43,7 +41,7 @@ static void Main(string[] args)
4341
4442 // Save model
4543 // <SnippetSaveModelMain>
46- SaveModel ( mlContext , model ) ;
44+ SaveModel ( mlContext , trainingDataView . Schema , model ) ;
4745 // </SnippetSaveModelMain>
4846 }
4947
@@ -103,11 +101,11 @@ public static void EvaluateModel(MLContext mlContext, IDataView testDataView, IT
103101 // </SnippetTransform>
104102
105103 // <SnippetEvaluate>
106- var metrics = mlContext . Regression . Evaluate ( prediction , label : DefaultColumnNames . Label , score : DefaultColumnNames . Score ) ;
104+ var metrics = mlContext . Regression . Evaluate ( prediction , labelColumnName : " Label" , scoreColumnName : " Score" ) ;
107105 // </SnippetEvaluate>
108106
109107 // <SnippetPrintMetrics>
110- Console . WriteLine ( "Rms : " + metrics . Rms . ToString ( ) ) ;
108+ Console . WriteLine ( "Root Mean Squared Error : " + metrics . RootMeanSquaredError . ToString ( ) ) ;
111109 Console . WriteLine ( "RSquared: " + metrics . RSquared . ToString ( ) ) ;
112110 // </SnippetPrintMetrics>
113111 }
@@ -117,7 +115,7 @@ public static void UseModelForSinglePrediction(MLContext mlContext, ITransformer
117115 {
118116 // <SnippetPredictionEngine>
119117 Console . WriteLine ( "=============== Making a prediction ===============" ) ;
120- var predictionEngine = model . CreatePredictionEngine < MovieRating , MovieRatingPrediction > ( mlContext ) ;
118+ var predictionEngine = mlContext . Model . CreatePredictionEngine < MovieRating , MovieRatingPrediction > ( model ) ;
121119 // </SnippetPredictionEngine>
122120
123121 // Create test input & make single prediction
@@ -140,15 +138,14 @@ public static void UseModelForSinglePrediction(MLContext mlContext, ITransformer
140138 }
141139
142140 //Save model
143- public static void SaveModel ( MLContext mlContext , ITransformer model )
141+ public static void SaveModel ( MLContext mlContext , DataViewSchema trainingDataViewSchema , ITransformer model )
144142 {
145143 // Save the trained model to .zip file
146144 // <SnippetSaveModel>
147145 var modelPath = Path . Combine ( Environment . CurrentDirectory , "Data" , "MovieRecommenderModel.zip" ) ;
148146
149147 Console . WriteLine ( "=============== Saving the model to a file ===============" ) ;
150- using ( var fs = new FileStream ( modelPath , FileMode . Create , FileAccess . Write , FileShare . Write ) )
151- mlContext . Model . Save ( model , fs ) ;
148+ mlContext . Model . Save ( model , trainingDataViewSchema , modelPath ) ;
152149 // </SnippetSaveModel>
153150 }
154151
0 commit comments