- Hands-On Machine Learning with ML.NET
- Jarred Capellman
- 310字
- 2021-06-24 16:43:30
The Predictor class
The Predictor class, as noted earlier, is the class that provides prediction support in our project. The idea behind this method is to provide a simple interface to run the model, given the relatively simple input. In future chapters, we will be expanding this method structure to support more complex integrations, such as those hosted in a web application:
- Akin to what was done in the Trainer class, we verify that the model exists prior to reading it:
if (!File.Exists(ModelPath)) {
Console.WriteLine($"Failed to find model at {ModelPath}");
return;
}
- Then, we define the ITransformer object:
ITransformer mlModel;
using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
mlModel = MlContext.Model.Load(stream, out _);
}
if (mlModel == null) {
Console.WriteLine("Failed to load model");
return;
}
This object will contain our model once we load via the Model.Load method. This method can also take a direct file path. However, the stream approach lends itself to support non on-disk approaches that we will use in later chapters.
- Next, create a PredictionEngine object given the model we loaded earlier:
var predictionEngine = MlContext.Model.CreatePredictionEngine<RestaurantFeedback, RestaurantPrediction>(mlModel);
We are passing in both TSrc and TDst, in our case for this project, RestaurantFeedback and RestaurantPrediction, respectively.
- Then, call the Predict method on the PredictionEngine class:
var prediction = predictionEngine.Predict(new RestaurantFeedback { Text = inputData });
Because, when we created the object with TSrc, the type was set to RestaurantFeedback, we have a strongly typed interface to our model. We then create the RestaurantFeedback object with the inputData variable that contains the string with the sentence we are going to run our model on.
- Finally, display the prediction output along with the probability:
Console.WriteLine($"Based on \"{inputData}\", the feedback is predicted to be:{Environment.NewLine}" +
"{(prediction.Prediction ? "Negative" : "Positive")} at a {prediction.Probability:P0}" + " confidence");
- 體驗(yàn)設(shè)計(jì)原理:行為、情感和細(xì)節(jié)
- DevOps入門與實(shí)踐
- 數(shù)據(jù)結(jié)構(gòu)與算法JavaScript描述
- MATLAB應(yīng)用與實(shí)驗(yàn)教程
- C++ 從入門到項(xiàng)目實(shí)踐(超值版)
- 數(shù)據(jù)結(jié)構(gòu)案例教程(C/C++版)
- Visual Basic程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)(第二版)
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- 深度學(xué)習(xí):Java語言實(shí)現(xiàn)
- Arduino計(jì)算機(jī)視覺編程
- JavaScript動(dòng)態(tài)網(wǎng)頁編程
- 深入理解BootLoader
- Java 9:Building Robust Modular Applications
- Python全棧開發(fā):數(shù)據(jù)分析
- 網(wǎng)絡(luò)工程方案設(shè)計(jì)與實(shí)施(第二版)