public PredictionEngineExample(string modelName)
            {
                _env = EnvHelper.NewTestEnvironment();
                var transformer = TransformerChain.LoadFromLegacy(_env, File.OpenRead(modelName));
                var model       = new ModelOperationsCatalog(_env);

                _predictor = model.CreatePredictionEngine <FloatVectorInput, FloatOutput>(transformer);
            }
Exemplo n.º 2
0
        /// <summary>
        /// Load a <see cref="ICanForecast{T}"/> model.
        /// </summary>
        /// <typeparam name="T">The type of <see cref="ICanForecast{T}"/>, usually float.</typeparam>
        /// <param name="catalog"><see cref="ModelOperationsCatalog"/></param>
        /// <param name="filePath">File path to load the model from.</param>
        /// <returns><see cref="ICanForecast{T}"/> model.</returns>
        public static ICanForecast <T> LoadForecastingModel <T>(this ModelOperationsCatalog catalog, string filePath)
        {
            var env = CatalogUtils.GetEnvironment(catalog);

            using (var file = File.OpenRead(filePath))
            {
                using (var rep = RepositoryReader.Open(file, env))
                {
                    ModelLoadContext.LoadModel <ICanForecast <T>, SignatureLoadModel>(env, out var model, rep, LoaderSignature);
                    return(model);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Save a <see cref="ICanForecast{T}"/> model to a file specified by <paramref name="filePath"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="catalog"><see cref="ModelOperationsCatalog"/></param>
        /// <param name="model"><see cref="ICanForecast{T}"/> model to save.</param>
        /// <param name="filePath">File path to save the model to.</param>
        public static void SaveForecastingModel <T>(this ModelOperationsCatalog catalog, ICanForecast <T> model, string filePath)
        {
            var env = CatalogUtils.GetEnvironment(catalog);

            using (var file = File.Create(filePath))
            {
                using (var ch = env.Start("Saving forecasting model."))
                {
                    using (var rep = RepositoryWriter.CreateNew(file, ch))
                    {
                        ModelSaveContext.SaveModel(rep, model, LoaderSignature);
                        rep.Commit();
                    }
                }
            }
        }
Exemplo n.º 4
0
        internal static CountTableEstimator LoadFromFile(IHostEnvironment env, string initialCountsModel, string labelColumn, InputOutputColumnPair[] columns)
        {
            var catalog     = new ModelOperationsCatalog(env);
            var transformer = catalog.Load(initialCountsModel, out _);
            CountTableTransformer countTableTransformer;

            if ((countTableTransformer = transformer as CountTableTransformer) == null)
            {
                if (transformer is CountTargetEncodingTransformer cte)
                {
                    countTableTransformer = cte.CountTable;
                }
                else if (transformer is TransformerChain <ITransformer> chain)
                {
                    var transformerChain = ((ITransformerChainAccessor)chain).Transformers;
                    for (int i = transformerChain.Length - 1; i >= 0; i++)
                    {
                        countTableTransformer = transformerChain[i] as CountTableTransformer;
                        if (countTableTransformer == null)
                        {
                            if ((cte = transformerChain[i] as CountTargetEncodingTransformer) != null)
                            {
                                countTableTransformer = cte.CountTable;
                            }
                        }
                        if (countTableTransformer != null)
                        {
                            break;
                        }
                    }
                }
            }
            if (countTableTransformer != null)
            {
                return(new CountTableEstimator(env, labelColumn, countTableTransformer, columns));
            }
            return(null);
        }
Exemplo n.º 5
0
 public static IHostEnvironment GetEnvironment(this ModelOperationsCatalog catalog) => Contracts.CheckRef(catalog, nameof(catalog)).Environment;
Exemplo n.º 6
0
        private static List <Tuple <int, TimeSpan, int> > _MeasureTime(int conc,
                                                                       string engine, IDataScorerTransform scorer, ITransformer transformer,
                                                                       int N, int ncall, bool cacheScikit)
        {
            var args = new TextLoader.Options()
            {
                Separators = new[] { '\t' },
                HasHeader  = true,
                Columns    = new[] {
                    new TextLoader.Column("Label", DataKind.Boolean, 0),
                    new TextLoader.Column("SentimentText", DataKind.String, 1)
                }
            };

            var testFilename = FileHelper.GetTestFile("wikipedia-detox-250-line-test.tsv");
            var times        = new List <Tuple <int, TimeSpan, int> >();

            /*using (*/
            var env = EnvHelper.NewTestEnvironment(seed: 1, conc: conc);

            {
                // Take a couple examples out of the test data and run predictions on top.
                var       testLoader = new TextLoader(env, args).Load(new MultiFileSource(testFilename));
                IDataView cache;
                if (cacheScikit)
                {
                    cache = new ExtendedCacheTransform(env, new ExtendedCacheTransform.Arguments(), testLoader);
                }
                else
                {
                    cache = new CacheDataView(env, testLoader, new[] { 0, 1 });
                }
                //var testData = cache.AsEnumerable<SentimentDataBoolFloat>(env, false);

                if (engine == "mlnet")
                {
                    Console.WriteLine("engine={0} N={1} ncall={2} cacheScikit={3}", engine, N, ncall, cacheScikit);
                    var model = new ModelOperationsCatalog(env);
                    var fct   = model.CreatePredictionEngine <SentimentDataBoolFloat, SentimentPrediction>(transformer, true);
                    var sw    = new Stopwatch();
                    for (int call = 1; call <= ncall; ++call)
                    {
                        sw.Reset();
                        sw.Start();
                        for (int i = 0; i < N; ++i)
                        {
                            /*
                             * foreach (var input in testData)
                             *  fct.Predict(input);
                             */
                            ;
                        }
                        sw.Stop();
                        times.Add(new Tuple <int, TimeSpan, int>(N, sw.Elapsed, call));
                    }
                }
                else if (engine == "scikit")
                {
                    Console.WriteLine("engine={0} N={1} ncall={2} cacheScikit={3}", engine, N, ncall, cacheScikit);
                    var model  = new ValueMapperPredictionEngine <SentimentDataBoolFloat>(env, scorer);
                    var output = new ValueMapperPredictionEngine <SentimentDataBoolFloat> .PredictionTypeForBinaryClassification();

                    var sw = new Stopwatch();
                    for (int call = 1; call <= ncall; ++call)
                    {
                        sw.Reset();
                        sw.Start();
                        for (int i = 0; i < N; ++i)
                        {
                            /*
                             * foreach (var input in testData)
                             *  model.Predict(input, ref output);
                             */
                            ;
                        }
                        sw.Stop();
                        times.Add(new Tuple <int, TimeSpan, int>(N, sw.Elapsed, call));
                    }
                }
                else
                {
                    throw new NotImplementedException($"Unknown engine '{engine}'.");
                }
            }
            return(times);
        }