Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/28/20 12:14:21 (5 years ago)
Author:
gkronber
Message:

#2925: merged r17255:17402 from trunk to branch

Location:
branches/2925_AutoDiffForDynamicalModels
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/2925_AutoDiffForDynamicalModels

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs

    r17246 r17409  
    2424using System.Collections.Generic;
    2525using System.Linq;
     26using HEAL.Attic;
    2627using HeuristicLab.Common;
    2728using HeuristicLab.Core;
    28 using HEAL.Attic;
    2929using HeuristicLab.Problems.DataAnalysis;
    3030
     
    3838    // don't store the actual model!
    3939    // the actual model is only recalculated when necessary
     40    private IGradientBoostedTreesModel fullModel;
    4041    private readonly Lazy<IGradientBoostedTreesModel> actualModel;
    4142    private IGradientBoostedTreesModel ActualModel {
     
    4849    private readonly uint seed;
    4950    [Storable]
    50     private ILossFunction lossFunction;
     51    private readonly ILossFunction lossFunction;
    5152    [Storable]
    52     private double r;
     53    private readonly double r;
    5354    [Storable]
    54     private double m;
     55    private readonly double m;
    5556    [Storable]
    56     private double nu;
     57    private readonly double nu;
    5758    [Storable]
    58     private int iterations;
     59    private readonly int iterations;
    5960    [Storable]
    60     private int maxSize;
     61    private readonly int maxSize;
    6162
    6263
     
    6970    [StorableConstructor]
    7071    private GradientBoostedTreesModelSurrogate(StorableConstructorFlag _) : base(_) {
    71       actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel());
     72      actualModel = CreateLazyInitFunc();
    7273    }
    7374
    7475    private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner)
    7576      : base(original, cloner) {
    76       IGradientBoostedTreesModel clonedModel = null;
    77       if (original.actualModel.IsValueCreated) clonedModel = cloner.Clone(original.ActualModel);
    78       actualModel = new Lazy<IGradientBoostedTreesModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure
    79 
     77      // clone data which is necessary to rebuild the model
    8078      this.trainingProblemData = cloner.Clone(original.trainingProblemData);
    8179      this.lossFunction = cloner.Clone(original.lossFunction);
     
    8684      this.m = original.m;
    8785      this.nu = original.nu;
     86
     87      // clone full model if it has already been created
     88      if (original.fullModel != null) this.fullModel = cloner.Clone(original.fullModel);
     89      actualModel = CreateLazyInitFunc();
    8890    }
    8991
    90     private Func<IGradientBoostedTreesModel> CreateLazyInitFunc(IGradientBoostedTreesModel clonedModel) {
    91       return () => {
    92         return clonedModel ?? RecalculateModel();
    93       };
     92    private Lazy<IGradientBoostedTreesModel> CreateLazyInitFunc() {
     93      return new Lazy<IGradientBoostedTreesModel>(() => {
     94        if (fullModel == null) fullModel = RecalculateModel();
     95        return fullModel;
     96      });
    9497    }
    9598
     
    107110      this.nu = nu;
    108111
    109       actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel());
     112      actualModel = CreateLazyInitFunc();
    110113    }
    111114
    112     // wrap an actual model in a surrograte
     115    // wrap an actual model in a surrogate
    113116    public GradientBoostedTreesModelSurrogate(IGradientBoostedTreesModel model, IRegressionProblemData trainingProblemData, uint seed,
    114117      ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu)
    115118      : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) {
    116       actualModel = new Lazy<IGradientBoostedTreesModel>(() => model);
     119      fullModel = model;
    117120    }
    118121
     
    135138
    136139    public IEnumerable<IRegressionModel> Models {
    137       get {
    138         return ActualModel.Models;
    139       }
     140      get { return ActualModel.Models; }
    140141    }
    141142
    142143    public IEnumerable<double> Weights {
    143       get {
    144         return ActualModel.Weights;
    145       }
     144      get { return ActualModel.Weights; }
    146145    }
    147146  }
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelSurrogate.cs

    r17246 r17409  
    3535    #region parameters for recalculation of the model
    3636    [Storable]
    37     private int seed;
     37    private readonly int seed;
    3838    [Storable]
    39     private IDataAnalysisProblemData originalTrainingData;
     39    private readonly IDataAnalysisProblemData originalTrainingData;
    4040    [Storable]
    41     private double[] classValues;
     41    private readonly double[] classValues;
    4242    [Storable]
    43     private int nTrees;
     43    private readonly int nTrees;
    4444    [Storable]
    45     private double r;
     45    private readonly double r;
    4646    [Storable]
    47     private double m;
     47    private readonly double m;
    4848    #endregion
     49
    4950
    5051    // don't store the actual model!
    5152    // the actual model is only recalculated when necessary
     53    private IRandomForestModel fullModel = null;
    5254    private readonly Lazy<IRandomForestModel> actualModel;
     55
    5356    private IRandomForestModel ActualModel {
    5457      get { return actualModel.Value; }
     
    7477      this.m = m;
    7578
    76       actualModel = new Lazy<IRandomForestModel>(() => RecalculateModel());
     79      actualModel = CreateLazyInitFunc();
    7780    }
    7881
    79     // wrap an actual model in a surrograte
     82    // wrap an actual model in a surrogate
    8083    public RandomForestModelSurrogate(IRandomForestModel model, string targetVariable, IDataAnalysisProblemData originalTrainingData,
    81       int seed, int nTrees, double r, double m, double[] classValues = null) : this(targetVariable, originalTrainingData, seed, nTrees, r, m, classValues) {
    82       actualModel = new Lazy<IRandomForestModel>(() => model);
     84      int seed, int nTrees, double r, double m, double[] classValues = null)
     85      : this(targetVariable, originalTrainingData, seed, nTrees, r, m, classValues) {
     86      fullModel = model;
    8387    }
    8488
    8589    [StorableConstructor]
    8690    private RandomForestModelSurrogate(StorableConstructorFlag _) : base(_) {
    87       actualModel = new Lazy<IRandomForestModel>(() => RecalculateModel());
     91      actualModel = CreateLazyInitFunc();
    8892    }
    8993
    9094    private RandomForestModelSurrogate(RandomForestModelSurrogate original, Cloner cloner) : base(original, cloner) {
    91       IRandomForestModel clonedModel = null;
    92       if (original.actualModel.IsValueCreated) clonedModel = cloner.Clone(original.ActualModel);
    93       actualModel = new Lazy<IRandomForestModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure
    94 
    9595      // clone data which is necessary to rebuild the model
    9696      this.originalTrainingData = cloner.Clone(original.originalTrainingData);
     
    100100      this.r = original.r;
    101101      this.m = original.m;
    102     }
    103102
    104     private Func<IRandomForestModel> CreateLazyInitFunc(IRandomForestModel clonedModel) {
    105       return () => {
    106         return clonedModel ?? RecalculateModel();
    107       };
     103      // clone full model if it has already been created
     104      if (original.fullModel != null) this.fullModel = cloner.Clone(original.fullModel);
     105      actualModel = CreateLazyInitFunc();
    108106    }
    109107
    110108    public override IDeepCloneable Clone(Cloner cloner) {
    111109      return new RandomForestModelSurrogate(this, cloner);
     110    }
     111
     112    private Lazy<IRandomForestModel> CreateLazyInitFunc() {
     113      return new Lazy<IRandomForestModel>(() => {
     114        if (fullModel == null) fullModel = RecalculateModel();
     115        return fullModel;
     116      });
    112117    }
    113118
Note: See TracChangeset for help on using the changeset viewer.