public void PredictionEngineModelDisposal() { var mlContext = new MLContext(seed: 1); var data = mlContext.Data.LoadFromEnumerable(TypeTestData.GenerateDataset()); var pipeline = mlContext.BinaryClassification.Trainers.LbfgsLogisticRegression( new Trainers.LbfgsLogisticRegressionBinaryTrainer.Options { NumberOfThreads = 1 }); var model = pipeline.Fit(data); var engine = mlContext.Model.CreatePredictionEngine <TypeTestData, Prediction>(model, new PredictionEngineOptions()); // Dispose of prediction engine, should dispose of model engine.Dispose(); // Get disposed flag using reflection var bfIsDisposed = BindingFlags.Instance | BindingFlags.NonPublic; var field = model.GetType().BaseType.BaseType.GetField("_disposed", bfIsDisposed); // Make sure the model is actually disposed Assert.True((bool)field.GetValue(model)); // Make a new model/prediction engine. Set the options so prediction engine doesn't dispose model = pipeline.Fit(data); var options = new PredictionEngineOptions() { OwnsTransformer = false }; engine = mlContext.Model.CreatePredictionEngine <TypeTestData, Prediction>(model, options); // Dispose of prediction engine, shouldn't dispose of model engine.Dispose(); // Make sure model is not disposed of. Assert.False((bool)field.GetValue(model)); // Dispose of the model for test cleanliness model.Dispose(); }
/// <summary> /// <see cref="TimeSeriesPredictionEngine{TSrc, TDst}"/> creates a prediction engine for a time series pipeline. /// It updates the state of time series model with observations seen at prediction phase and allows checkpointing the model. /// </summary> /// <typeparam name="TSrc">Class describing input schema to the model.</typeparam> /// <typeparam name="TDst">Class describing the output schema of the prediction.</typeparam> /// <param name="transformer">The time series pipeline in the form of a <see cref="ITransformer"/>.</param> /// <param name="env">Usually <see cref="MLContext"/></param> /// <param name="options">Advanced configuration options.</param> /// <p>Example code can be found by searching for <i>TimeSeriesPredictionEngine</i> in <a href='https://github.com/dotnet/machinelearning'>ML.NET.</a></p> /// <example> /// <format type="text/markdown"> /// <![CDATA[ /// This is an example for detecting change point using Singular Spectrum Analysis (SSA) model. /// [!code-csharp[MF](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectChangePointBySsa.cs)] /// ]]> /// </format> /// </example> public static TimeSeriesPredictionEngine <TSrc, TDst> CreateTimeSeriesEngine <TSrc, TDst>(this ITransformer transformer, IHostEnvironment env, PredictionEngineOptions options) where TSrc : class where TDst : class, new() { Contracts.CheckValue(env, nameof(env)); env.CheckValue(options, nameof(options)); return(new TimeSeriesPredictionEngine <TSrc, TDst>(env, transformer, options)); }
/// <summary> /// Contructor for creating time series specific prediction engine. It allows the time series model to be updated with the observations /// seen at prediction time via <see cref="CheckPoint(IHostEnvironment, string)"/> /// </summary> internal TimeSeriesPredictionEngine(IHostEnvironment env, ITransformer transformer, PredictionEngineOptions options) : base(env, CloneTransformers(transformer), options.IgnoreMissingColumns, options.InputSchemaDefinition, options.OutputSchemaDefinition, options.OwnsTransformer) { }