internal static void DisposeIfDisposable <TPrediction>(IPredictorModel <TPrediction> model) { if (model is IDisposable) { ((IDisposable)model).Dispose(); } }
/// <summary> /// Learns a regression ensemble on the provided indices /// </summary> /// <param name="observations"></param> /// <param name="targets"></param> /// <param name="indices"></param> /// <returns></returns> public RegressionEnsembleModel Learn(F64Matrix observations, double[] targets, int[] indices) { Checks.VerifyObservationsAndTargets(observations, targets); Checks.VerifyIndices(indices, observations, targets); var ensembleModels = new IPredictorModel <double> [m_learners.Length]; var sampleSize = (int)Math.Round(m_subSampleRatio * indices.Length); if (sampleSize < 1) { throw new ArgumentException("subSampleRatio two small"); } var inSample = new int[sampleSize]; for (int i = 0; i < m_learners.Length; i++) { Trace.WriteLine("Training model: " + (i + 1)); if (m_subSampleRatio != 1.0) { Sample(inSample, indices); ensembleModels[i] = m_learners[i](observations, targets, inSample); } else { ensembleModels[i] = m_learners[i](observations, targets, indices); } } return(new RegressionEnsembleModel(ensembleModels, m_ensembleStrategy())); }
public override List <IDictionary <string, string> > ReleaseSets(IPredictorModel model) { return(new List <IDictionary <string, string> >() { model.SelectedReleases }); }
public PredictorPresenter(IPredictorModel model, IPredictorView view) { this.model = model; this.view = view; model.OnTitleUpdated += x => view.Title = x; model.OnClearReport += () => view.ClearReport(); model.OnAddReport += x => view.AddReport(x); model.OnClearRocList += () => view.ClearROCs(); model.OnRocAdded += view.AddRoc; model.OnReadyStateChanged += x => view.Ready = x; model.OnProgressStateChanged += x => view.Status = x; model.OnError += x => view.ShowError(x); view.OnOpenConfigFile += OpenConfigFile; view.OnPredict += Predict; view.OnShowROC += model.ShowROC; view.ShowFiles = model.ShowFiles; view.Evaluate = model.Evaluate; view.EvaluateUsingROC = model.EvaluateUsingROC; view.EvaluateRanking = model.EvaluateRanking; view.ShowEstimationStats = model.ShowEstimationStats; view.ShowTotalResult = model.ShowTotalResult; if (model.ReleaseSetGetting == ReleaseSetGettingAlgo.All) { view.ReleaseSetGettingAll = true; } else if (model.ReleaseSetGetting == ReleaseSetGettingAlgo.IncrementalGrowth) { view.ReleaseSetGettingIncrementalGrowth = true; } else { view.ReleaseSetGettingFixed = true; } view.ReleaseSetSize = model.ReleaseSetSize; }
public PredictorPresenter(IPredictorModel model, IPredictorView view) { this.model = model; this.view = view; model.OnTitleUpdated += x => view.Title = x; model.OnClearReport += () => view.ClearReport(); model.OnAddReport += x => view.AddReport(x); model.OnClearRocList += () => view.ClearROCs(); model.OnRocAdded += view.AddRoc; model.OnReadyStateChanged += x => view.Ready = x; model.OnProgressStateChanged += x => view.Status = x; model.OnError += x => view.ShowError(x); view.OnOpenConfigFile += OpenConfigFile; view.OnPredict += Predict; view.OnShowROC += model.ShowROC; view.ShowFiles = model.ShowFiles; view.Evaluate = model.Evaluate; view.EvaluateUsingROC = model.EvaluateUsingROC; view.EvaluateRanking = model.EvaluateRanking; view.ShowEstimationStats = model.ShowEstimationStats; view.ShowTotalResult = model.ShowTotalResult; if (model.ReleaseSetGetting == ReleaseSetGettingAlgo.All) { view.ReleaseSetGettingAll = true; } else if (model.ReleaseSetGetting == ReleaseSetGettingAlgo.IncrementalGrowth) { view.ReleaseSetGettingIncrementalGrowth = true; } else { view.ReleaseSetGettingFixed = true; } view.ReleaseSetSize = model.ReleaseSetSize; }
/// <summary> /// Saves the PredictorModel to the given path /// </summary> private void SavePredictorModel(IPredictorModel pm, string path) { Contracts.CheckValue(pm, nameof(pm)); using (var handle = _host.CreateOutputFile(path)) using (var fs = handle.CreateWriteStream()) pm.Save(_host, fs); }
internal static void DisposeIfDisposable <TPrediction>(IPredictorModel <TPrediction> model) { var modelDisposable = typeof(IDisposable).IsAssignableFrom(model.GetType()); if (modelDisposable) { ((IDisposable)model).Dispose(); } }
/// <summary> /// /// </summary> /// <param name="ensembleModels">Models included in the ensemble</param> /// <param name="metaModel">Meta or top level model to combine the ensemble models</param> /// <param name="includeOriginalFeaturesForMetaLearner">True; the meta learner also receives the original features. /// False; the meta learner only receives the output of the ensemble models as features</param> /// <param name="numberOfClasses">Number of classes in the classification problem</param> public ClassificationStackingEnsembleModel(IPredictorModel <ProbabilityPrediction>[] ensembleModels, IPredictorModel <ProbabilityPrediction> metaModel, bool includeOriginalFeaturesForMetaLearner, int numberOfClasses) { m_ensembleModels = ensembleModels ?? throw new ArgumentException(nameof(ensembleModels)); m_metaModel = metaModel ?? throw new ArgumentException(nameof(metaModel)); m_includeOriginalFeaturesForMetaLearner = includeOriginalFeaturesForMetaLearner; m_numberOfClasses = numberOfClasses; }
public override List <IDictionary <string, string> > ReleaseSets(IPredictorModel model) { List <IDictionary <string, string> > list = new List <IDictionary <string, string> >(); for (int i = 1; i <= model.SelectedReleases.Count; i++) { list.Add( model.SelectedReleases.Take(i).ToDictionary(x => x.Key, x => x.Value) ); } return(list); }
/// <summary> /// /// </summary> /// <param name="ensembleModels">Models included in the ensemble</param> /// <param name="metaModel">Meta or top level model to combine the ensemble models</param> /// <param name="includeOriginalFeaturesForMetaLearner">True; the meta learner also recieves the original features. /// False; the meta learner only recieves the output of the ensemble models as features</param> public RegressionStackingEnsembleModel(IPredictorModel <double>[] ensembleModels, IPredictorModel <double> metaModel, bool includeOriginalFeaturesForMetaLearner) { if (ensembleModels == null) { throw new ArgumentException("ensembleModels"); } if (metaModel == null) { throw new ArgumentException("metaModel"); } m_ensembleModels = ensembleModels; m_metaModel = metaModel; m_includeOriginalFeaturesForMetaLearner = includeOriginalFeaturesForMetaLearner; }
public void LoadModel(string path) { model = null; try { var binarySerializer = new GenericBinarySerializer(); model = binarySerializer.Deserialize <IPredictorModel <double> >( () => new StreamReader(root + path)); } catch (Exception e) { Debug.Log(e.Message); model = null; } }
/// <summary> /// /// </summary> /// <param name="ensembleModels">Models included in the ensemble</param> /// <param name="metaModel">Meta or top level model to combine the ensemble models</param> /// <param name="includeOriginalFeaturesForMetaLearner">True; the meta learner also recieves the original features. /// False; the meta learner only recieves the output of the ensemble models as features</param> /// <param name="numberOfClasses">Number of classes in the classification problem</param> public ClassificationStackingEnsembleModel(IPredictorModel <ProbabilityPrediction>[] ensembleModels, IPredictorModel <ProbabilityPrediction> metaModel, bool includeOriginalFeaturesForMetaLearner, int numberOfClasses) { if (ensembleModels == null) { throw new ArgumentException("ensembleModels"); } if (metaModel == null) { throw new ArgumentException("metaModel"); } m_ensembleModels = ensembleModels; m_metaModel = metaModel; m_includeOriginalFeaturesForMetaLearner = includeOriginalFeaturesForMetaLearner; m_numberOfClasses = numberOfClasses; }
public void TrainModelWithRangeOfPossibleParametersTest() { const string baseFolder = nameof(TrainModelWithRangeOfPossibleParametersTest); Directory.CreateDirectory(baseFolder); var generator = new ModelTrainingFilesGenerator(); // create catalog items IList <SarCatalogItem> catalogItems; string[] featureNames; string catalogFilePath = Path.Combine(baseFolder, "catalog.csv"); generator.CreateCatalogFile(catalogFilePath); var itemIdsIndex = new ConcurrentDictionary <string, uint>(); var catalogParser = new CatalogFileParser(0, itemIdsIndex); var parsingReport = catalogParser.ParseCatalogFile(catalogFilePath, CancellationToken.None, out catalogItems, out featureNames); Assert.IsTrue(parsingReport.IsCompletedSuccessfuly); // create usage items IList <SarUsageEvent> usageEvents; string usageFileFolderPath = Path.Combine(baseFolder, "usage"); Directory.CreateDirectory(usageFileFolderPath); generator.CreateUsageFile(Path.Combine(usageFileFolderPath, "usage.csv"), 10000); var userIdsIndex = new ConcurrentDictionary <string, uint>(); var usageFilesParser = new UsageEventsFilesParser(itemIdsIndex, userIdsIndex); parsingReport = usageFilesParser.ParseUsageEventFiles(usageFileFolderPath, CancellationToken.None, out usageEvents); Assert.IsTrue(parsingReport.IsCompletedSuccessfuly); int count = 0; var sarTrainer = new SarTrainer(); IDictionary <string, double> catalogFeatureWeights; foreach (IModelTrainerSettings settings in GetAllModelTrainingParameters()) { IPredictorModel model = sarTrainer.Train(settings, usageEvents, catalogItems, featureNames, userIdsIndex.Count, itemIdsIndex.Count, out catalogFeatureWeights); Assert.IsNotNull(model, $"Expected training to complete successfully when using settings#{count}: {settings}"); count++; } }
private void CalibrateSpectra(IPredictorModel <double> ms1predictor, IPredictorModel <double> ms2predictor) { Parallel.ForEach(Partitioner.Create(1, MyMsDataFile.NumSpectra + 1), new ParallelOptions { MaxDegreeOfParallelism = commonParameters.MaxThreadsToUsePerFile }, (fff, loopState) => { for (int i = fff.Item1; i < fff.Item2; i++) { // Stop loop if canceled if (GlobalVariables.StopLoops) { loopState.Stop(); return; } var scan = MyMsDataFile.GetOneBasedScan(i); if (scan.MsnOrder == 2) { var precursorScan = MyMsDataFile.GetOneBasedScan(scan.OneBasedPrecursorScanNumber.Value); if (!scan.SelectedIonMonoisotopicGuessIntensity.HasValue && scan.SelectedIonMonoisotopicGuessMz.HasValue) { scan.ComputeMonoisotopicPeakIntensity(precursorScan.MassSpectrum); } double theFunc(MzPeak x) => x.Mz - ms2predictor.Predict(new double[] { x.Mz, scan.RetentionTime, Math.Log(scan.TotalIonCurrent), scan.InjectionTime.HasValue ? Math.Log(scan.InjectionTime.Value) : double.NaN, Math.Log(x.Intensity) }); double theFuncForPrecursor(MzPeak x) => x.Mz - ms1predictor.Predict(new double[] { x.Mz, precursorScan.RetentionTime, Math.Log(precursorScan.TotalIonCurrent), precursorScan.InjectionTime.HasValue ? Math.Log(precursorScan.InjectionTime.Value) : double.NaN, Math.Log(x.Intensity) }); scan.TransformMzs(theFunc, theFuncForPrecursor); } else { Func <MzPeak, double> theFunc = x => x.Mz - ms1predictor.Predict(new double[] { x.Mz, scan.RetentionTime, Math.Log(scan.TotalIonCurrent), scan.InjectionTime.HasValue ? Math.Log(scan.InjectionTime.Value) : double.NaN, Math.Log(x.Intensity) }); scan.MassSpectrum.ReplaceXbyApplyingFunction(theFunc); } } } ); }
public override List <IDictionary <string, string> > ReleaseSets(IPredictorModel model) { List <IDictionary <string, string> > list = new List <IDictionary <string, string> >(); if (model.SelectedReleases.Count <= model.ReleaseSetSize) { list.Add(model.SelectedReleases); } else { for (int i = 0; i <= model.SelectedReleases.Count - model.ReleaseSetSize; i++) { list.Add( model.SelectedReleases.Skip(i).Take(model.ReleaseSetSize) .ToDictionary(x => x.Key, x => x.Value) ); } } return(list); }
/// <summary> /// Creates a new instance of the <see cref="TrainedModel"/> class. /// </summary> /// <param name="predictorModel">The predictor model</param> /// <param name="properties">The model properties</param> /// <param name="itemIdIndex">The indexed item ids </param> internal TrainedModel(IPredictorModel predictorModel, ModelProperties properties, string[] itemIdIndex) { if (predictorModel == null) { throw new ArgumentNullException(nameof(predictorModel)); } if (properties == null) { throw new ArgumentNullException(nameof(properties)); } if (itemIdIndex == null) { throw new ArgumentNullException(nameof(itemIdIndex)); } _predictorModel = predictorModel; RecommenderData = new ModelRecommenderData(_predictorModel.Predictor as IUserHistoryToItemsRecommender); Properties = properties; ItemIdIndex = itemIdIndex; }
public abstract List <IDictionary <string, string> > ReleaseSets(IPredictorModel model);
/// <summary> /// Trains a model using the input files /// </summary> /// <param name="settings">The trainer settings</param> /// <param name="workFolderPath">A temp work folder for storing intermediate files</param> /// <param name="usageFolderPath">The path to the folder of usage files</param> /// <param name="catalogFilePath">The path to the catalog file</param> /// <param name="evaluationFolderPath">The path to the evaluation file (optional) </param> /// <param name="cancellationToken">A cancellation token used to abort the training</param> private ModelTrainResult TrainModelInternal(IModelTrainerSettings settings, string workFolderPath, string usageFolderPath, string catalogFilePath, string evaluationFolderPath, CancellationToken cancellationToken) { var duration = ModelTraininigDuration.Start(); var result = new ModelTrainResult { Duration = duration }; var userIdsIndexMap = new ConcurrentDictionary <string, uint>(); var itemIdsIndexMap = new ConcurrentDictionary <string, uint>(); // parse the catalog file IList <SarCatalogItem> catalogItems = null; string[] catalogFeatureNames = null; if (!string.IsNullOrWhiteSpace(catalogFilePath) && File.Exists(catalogFilePath)) { // report progress _progressMessageReportDelegate("Parsing Catalog File"); // create a catalog file parser var catalogParser = new CatalogFileParser(MaximumParsingErrorsCount, itemIdsIndexMap, _tracer); // parse the catalog file result.CatalogFilesParsingReport = catalogParser.ParseCatalogFile(catalogFilePath, cancellationToken, out catalogItems, out catalogFeatureNames); // record the catalog parsing duration duration.SetCatalogParsingDuration(); _tracer.TraceInformation($"Catalog parsing completed in {duration.CatalogParsingDuration.TotalMinutes} minutes"); // get the catalog items count result.CatalogItemsCount = catalogItems.Count; // fail the training if parsing had failed or yielded no items if (!result.CatalogFilesParsingReport.IsCompletedSuccessfuly || !catalogItems.Any()) { result.CompletionMessage = "Failed to parse catalog file or parsing found no valid items"; _tracer.TraceInformation(result.CompletionMessage); return(result); } // clear the catalog items list if it's not used anymore if (!settings.EnableColdItemPlacement) { catalogItems.Clear(); } } // report progress _progressMessageReportDelegate("Parsing Usage Events Files"); // create a usage events files parser that skips events of unknown item ids (if catalog was provided)) var usageEventsParser = new UsageEventsFilesParser(itemIdsIndexMap, userIdsIndexMap, MaximumParsingErrorsCount, catalogItems != null, _tracer); _tracer.TraceInformation("Parsing the usage event files"); IList <SarUsageEvent> usageEvents; result.UsageFilesParsingReport = usageEventsParser.ParseUsageEventFiles(usageFolderPath, cancellationToken, out usageEvents); // record the usage files parsing duration duration.SetUsageFilesParsingDuration(); _tracer.TraceInformation($"Usage file(s) parsing completed in {duration.UsageFilesParsingDuration.TotalMinutes} minutes"); // fail the training if parsing had failed or yielded no events if (!result.UsageFilesParsingReport.IsCompletedSuccessfuly || !usageEvents.Any()) { result.CompletionMessage = "Failed to parse usage file(s) or parsing found no valid items"; _tracer.TraceInformation(result.CompletionMessage); return(result); } _tracer.TraceInformation($"Found {userIdsIndexMap.Count} unique users"); result.UniqueUsersCount = userIdsIndexMap.Count; _tracer.TraceInformation($"Found {itemIdsIndexMap.Count} unique items"); result.UniqueItemsCount = usageEvents.Select(x => x.ItemId).Distinct().Count(); _tracer.TraceInformation("Extracting the indexed item ids from the item index map"); string[] itemIdsIndex = itemIdsIndexMap.OrderBy(kvp => kvp.Value).Select(kvp => kvp.Key).ToArray(); _tracer.TraceInformation($"Sorting the usage events based on the cooccurrenceUnit unit ({settings.CooccurrenceUnit})"); switch (settings.CooccurrenceUnit) { case CooccurrenceUnit.User: usageEvents = usageEvents.OrderBy(x => x.UserId).ToArray(); break; case CooccurrenceUnit.Timestamp: usageEvents = usageEvents.OrderBy(x => x.Timestamp).ThenBy(x => x.UserId).ToArray(); break; } _tracer.TraceInformation("Finished sorting usage events."); Stopwatch storeUserHistoryDuration = null; Task storeUserHistoryTask = null; if (settings.EnableUserToItemRecommendations && _userHistoryStore != null) { storeUserHistoryDuration = Stopwatch.StartNew(); _tracer.TraceInformation($"Extracting the indexed user ids from the user index map ({userIdsIndexMap.Count:N} users)"); string[] userIdsIndex = userIdsIndexMap.OrderBy(kvp => kvp.Value).Select(kvp => kvp.Key).ToArray(); _tracer.TraceInformation($"Asynchronously starting to store usage events per user (total of {usageEvents.Count:N} items)"); storeUserHistoryTask = Task.Run(() => _userHistoryStore.StoreUserHistoryEventsAsync(usageEvents, userIdsIndex, cancellationToken), cancellationToken); } // if provided, parse the evaluation usage event files int evaluationUsageEventsCount = 0; string parsedEvaluationUsageEventsFilePath = null; if (!string.IsNullOrWhiteSpace(evaluationFolderPath) && Directory.Exists(evaluationFolderPath)) { // report progress _progressMessageReportDelegate("Parsing Evaluation Usage Events Files"); _tracer.TraceInformation("Parsing the evaluation usage event files"); IList <SarUsageEvent> evaluationUsageEvents; result.EvaluationFilesParsingReport = usageEventsParser.ParseUsageEventFiles(evaluationFolderPath, cancellationToken, out evaluationUsageEvents); if (result.EvaluationFilesParsingReport.IsCompletedSuccessfuly) { // set the evaluation usage events count evaluationUsageEventsCount = evaluationUsageEvents.Count; _tracer.TraceInformation("Storing the parsed usage events for evaluation to reduce memory print"); parsedEvaluationUsageEventsFilePath = Path.Combine(workFolderPath, Path.GetTempFileName()); File.WriteAllLines(parsedEvaluationUsageEventsFilePath, evaluationUsageEvents.Select(JsonConvert.SerializeObject)); } else { _tracer.TraceWarning("Skipping model evaluation as it failed to parse evaluation usage files."); } // record the evaluation usage files parsing duration duration.SetEvaluationUsageFilesParsingDuration(); _tracer.TraceInformation($"Evaluation usage file(s) parsing completed in {duration.EvaluationUsageFilesParsingDuration.TotalMinutes} minutes"); } // clear the indices maps as they are no longer needed userIdsIndexMap.Clear(); itemIdsIndexMap.Clear(); cancellationToken.ThrowIfCancellationRequested(); // report progress _progressMessageReportDelegate("Core Training"); _tracer.TraceInformation("Training a new model using SAR trainer"); IDictionary <string, double> catalogFeatureWeights; var sarTrainer = new SarTrainer(_tracer); IPredictorModel sarModel = sarTrainer.Train(settings, usageEvents, catalogItems, catalogFeatureNames, result.UniqueUsersCount, result.CatalogItemsCount ?? result.UniqueItemsCount, out catalogFeatureWeights, cancellationToken); _tracer.TraceInformation("SAR training was completed."); // create the trained model properties var modelProperties = new ModelProperties { IncludeHistory = settings.AllowSeedItemsInRecommendations, EnableUserAffinity = settings.EnableUserAffinity, IsUserToItemRecommendationsSupported = settings.EnableUserToItemRecommendations, Decay = TimeSpan.FromDays(settings.DecayPeriodInDays), ReferenceDate = usageEventsParser.MostRecentEventTimestamp, UniqueUsersCount = result.UniqueUsersCount, }; // create the trained model result.Model = new TrainedModel(sarModel, modelProperties, itemIdsIndex); // set the catalog features weights result.CatalogFeatureWeights = catalogFeatureWeights; // record the core training duration duration.SetTrainingDuration(); // run model evaluation if evaluation usage event are available if (evaluationUsageEventsCount > 0 && parsedEvaluationUsageEventsFilePath != null) { // report progress _progressMessageReportDelegate("Evaluating Trained Model"); var evaluationUsageEvents = new List <SarUsageEvent>(evaluationUsageEventsCount); // load the evaluation usage events using (var reader = new StreamReader(parsedEvaluationUsageEventsFilePath)) { while (!reader.EndOfStream) { evaluationUsageEvents.Add(JsonConvert.DeserializeObject <SarUsageEvent>(reader.ReadLine())); } } _tracer.TraceInformation("Starting model evaluation"); var evaluator = new ModelEvaluator(_tracer); result.ModelMetrics = evaluator.Evaluate(result.Model, usageEvents, evaluationUsageEvents, cancellationToken); // record the evaluation duration duration.SetEvaluationDuration(); } if (storeUserHistoryTask != null) { _tracer.TraceInformation("Waiting for storing of usage events per user (user history) to complete"); if (!storeUserHistoryTask.IsCompleted) { _progressMessageReportDelegate("Storing User History"); // set the reporting flag to true so usage history upload progress will get reported to model status _reportUserHistoryProgress = true; } try { storeUserHistoryTask.Wait(cancellationToken); storeUserHistoryDuration?.Stop(); duration.StoringUserHistoryDuration = storeUserHistoryDuration?.Elapsed; _tracer.TraceInformation( $"Storing usage events per user (user history) to complete after {duration.StoringUserHistoryDuration.Value.TotalMinutes} minutes"); } catch (AggregateException ex) { var exception = new Exception("Exception while trying to store user history", ex); _tracer.TraceError(exception.ToString()); throw exception; } } // stop measuring the duration and record the total duration duration.Stop(); // return the train result result.CompletionMessage = "Model Training Completed Successfully"; return(result); }
private static void SavePredictorModelToFile(IPredictorModel model, string path, IHost host) { using (var fs = File.OpenWrite(path)) model.Save(host, fs); }
/// <summary> /// Trains a model using SAR. /// </summary> /// <param name="settings">The training settings</param> /// <param name="usageEvents">The usage events to use for training</param> /// <param name="catalogItems">The catalog items to use for training</param> /// <param name="featureNames">The names of the catalog items features, in the same order as the feature values in the catalog</param> /// <param name="uniqueUsersCount">The number of users in the user id index file.</param> /// <param name="uniqueUsageItemsCount">The number of usage items in the item id index file</param> /// <param name="catalogFeatureWeights">The computed catalog items features weights (if relevant)</param> /// <param name="cancellationToken">A cancellation token</param> public IPredictorModel Train(ITrainingSettings settings, IList <SarUsageEvent> usageEvents, IList <SarCatalogItem> catalogItems, string[] featureNames, int uniqueUsersCount, int uniqueUsageItemsCount, out IDictionary <string, double> catalogFeatureWeights, CancellationToken cancellationToken) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (usageEvents == null) { throw new ArgumentNullException(nameof(usageEvents)); } if (settings.EnableColdItemPlacement && catalogItems == null) { throw new ArgumentNullException(nameof(catalogItems)); } if (uniqueUsersCount < 0) { var exception = new ArgumentException($"{nameof(uniqueUsersCount)} must be a positive integer"); _tracer.TraceWarning(exception.ToString()); throw exception; } if (uniqueUsageItemsCount < 0) { var exception = new ArgumentException($"{nameof(uniqueUsageItemsCount)} must be a positive integer"); _tracer.TraceWarning(exception.ToString()); throw exception; } cancellationToken.ThrowIfCancellationRequested(); using (TlcEnvironment environment = new TlcEnvironment(verbose: true)) { _detectedFeatureWeights = null; try { environment.AddListener <ChannelMessage>(ChannelMessageListener); IHost environmentHost = environment.Register("SarHost"); // bind the cancellation token to SAR cancellation using (cancellationToken.Register(() => { environmentHost.StopExecution(); })) { _tracer.TraceInformation("Starting training model using SAR"); IPredictorModel model = TrainModel(environmentHost, settings, usageEvents, catalogItems, uniqueUsersCount, uniqueUsageItemsCount); catalogFeatureWeights = new Dictionary <string, double>(); if (_detectedFeatureWeights != null && featureNames != null) { if (_detectedFeatureWeights.Length == featureNames.Length) { for (int i = 0; i < featureNames.Length; i++) { catalogFeatureWeights[featureNames[i]] = _detectedFeatureWeights[i]; } } else { _tracer.TraceWarning( $"Found a mismatch between number of feature names ({featureNames.Length}) and the number of feature weights ({_detectedFeatureWeights.Length})"); } } return(model); } } finally { environment.RemoveListener <ChannelMessage>(ChannelMessageListener); } } }
/// <summary> /// /// </summary> /// <param name="ensembleModels">Models included in the ensemble</param> /// <param name="metaModel">Meta or top level model to combine the ensemble models</param> /// <param name="includeOriginalFeaturesForMetaLearner">True; the meta learner also receives the original features. /// False; the meta learner only receives the output of the ensemble models as features</param> public RegressionStackingEnsembleModel(IPredictorModel <double>[] ensembleModels, IPredictorModel <double> metaModel, bool includeOriginalFeaturesForMetaLearner) { m_ensembleModels = ensembleModels ?? throw new ArgumentException(nameof(ensembleModels)); m_metaModel = metaModel ?? throw new ArgumentException(nameof(metaModel)); m_includeOriginalFeaturesForMetaLearner = includeOriginalFeaturesForMetaLearner; }
public void Train(string dataName, string savePath) { var parser = new CsvParser(() => new StreamReader(root + dataName + ".csv"), ','); var targetName = "f5"; var enemyName = "f6"; try { // read data var observations = parser.EnumerateRows(c => c != targetName && c != enemyName) .ToF64Matrix(); var targets = parser.EnumerateRows(targetName) .ToF64Vector(); //Debug.Log("READ"); // split data //var splitter = new StratifiedTrainingTestIndexSplitter<double>(trainingPercentage: 0.9, seed: 1); //var trainingTestSplit = splitter.SplitSet(observations, targets); //var trainingSet = trainingTestSplit.TrainingSet; //var testSet = trainingTestSplit.TestSet; //Debug.Log("SPLIT"); //model = learner.Learn(trainingSet.Observations, trainingSet.Targets); model = learner.Learn(observations, targets); //Debug.Log("TRAINED"); //var met1 = new MeanSquaredErrorRegressionMetric(); //var met2 = new CoefficientOfDeterminationMetric(); //var met3 = new MeanAbsolutErrorRegressionMetric(); //var met4 = new NormalizedGiniCoefficientRegressionMetric(); //var met5 = new RootMeanSquarePercentageRegressionMetric(); //var pred = model.Predict(testSet.Observations); //Debug.Log(met1.Error(testSet.Targets, pred) + " <-> " + type); //Debug.Log(met2.Error(testSet.Targets, pred) + " <-> " + type); //Debug.Log(met3.Error(testSet.Targets, pred) + " <-> " + type); //Debug.Log(met4.Error(testSet.Targets, pred) + " <-> " + type); //Debug.Log(met5.Error(testSet.Targets, pred) + " <-> " + type); // NEURAL TEST Debug.Log("TRAINED"); // save SaveModel(savePath); // load //LoadModel("" + LevelData.instance.stage + "/" + LevelData.instance.playerID + ".model"); } catch (Exception e) { Debug.Log(e.Message); model = null; } finally { } }