Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/26/19 14:50:34 (5 years ago)
Author:
mkommend
Message:

#3030: Fixed cloning of RF & GBT surrogate models with already created models.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs

    r17180 r17272  
    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;
     120      actualModel = CreateLazyInitFunc();
    117121    }
    118122
     
    135139
    136140    public IEnumerable<IRegressionModel> Models {
    137       get {
    138         return ActualModel.Models;
    139       }
     141      get { return ActualModel.Models; }
    140142    }
    141143
    142144    public IEnumerable<double> Weights {
    143       get {
    144         return ActualModel.Weights;
    145       }
     145      get { return ActualModel.Weights; }
    146146    }
    147147  }
Note: See TracChangeset for help on using the changeset viewer.