PacktPublishing / Hands-On-Machine-Learning-for-.NET-Developers-V

MIT License
18 stars 13 forks source link

News classifer project is not complete #1

Open SimonBurfield opened 3 years ago

SimonBurfield commented 3 years ago

Please finish this off, as not covered in videos or anything

public class ConsumeModel { //TODO public static string Predict(string newsTitle) => "UNKNOWN";

}
SimonBurfield commented 3 years ago

I managed to work it out

public class ConsumeModel
    {
        private static Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictionEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(CreatePredictionEngine);

        public static string MLNetModelPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Model", "NewsClassificationModel.zip");

        // For more info on consuming ML.NET models, visit https://aka.ms/mlnet-consume
        // Method for consuming model in your app
        public static ModelOutput Predict(string newsTitle)
        {
            ModelOutput result = PredictionEngine.Value.Predict(new ModelInput() { Title = newsTitle });
            return result;
        }

        public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
        {
            // Create new MLContext
            MLContext mlContext = new MLContext();

            // Load model & create prediction engine
            ITransformer mlModel = mlContext.Model.Load(MLNetModelPath, out var modelInputSchema);
            var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);

            return predEngine;
        }

    }
skarlman commented 3 years ago

Hi,

The TODO is actually left as an exercise for the student, since the principle is covered in earlier sections. Glad you managed to work it out!

Regards Karl