Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/SquaredErrorLoss.cs @ 16453

Last change on this file since 16453 was 16453, checked in by jkarder, 5 years ago

#2520: updated year of copyrights

File size: 3.4 KB
RevLine 
[12590]1#region License Information
2/* HeuristicLab
[16453]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12590]4 * and the BEACON Center for the Study of Evolution in Action.
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
[12332]24using System.Collections.Generic;
25using System.Linq;
[12873]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[12332]29
[12590]30namespace HeuristicLab.Algorithms.DataAnalysis {
[12873]31  [StorableClass]
32  [Item("Squared error loss", "")]
[12875]33  public sealed class SquaredErrorLoss : Item, ILossFunction {
[12873]34    public SquaredErrorLoss() { }
35
[12696]36    public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred) {
[12332]37      var targetEnum = target.GetEnumerator();
38      var predEnum = pred.GetEnumerator();
39
40      double s = 0;
[12696]41      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
[12332]42        double res = targetEnum.Current - predEnum.Current;
[12696]43        s += res * res; // (res)^2
[12332]44      }
[12696]45      if (targetEnum.MoveNext() | predEnum.MoveNext())
46        throw new ArgumentException("target and pred have different lengths");
[12332]47
48      return s;
49    }
50
[12696]51    public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred) {
[12332]52      var targetEnum = target.GetEnumerator();
53      var predEnum = pred.GetEnumerator();
54
[12696]55      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
56        yield return 2.0 * (targetEnum.Current - predEnum.Current); // dL(y, f(x)) / df(x)  = 2 * res
[12332]57      }
[12696]58      if (targetEnum.MoveNext() | predEnum.MoveNext())
59        throw new ArgumentException("target and pred have different lengths");
[12332]60    }
61
[12697]62    // targetArr and predArr are not changed by LineSearch
63    public double LineSearch(double[] targetArr, double[] predArr, int[] idx, int startIdx, int endIdx) {
[12696]64      if (targetArr.Length != predArr.Length)
65        throw new ArgumentException("target and pred have different lengths");
[12332]66
[12607]67      // line search for squared error loss
68      // for a given partition of rows the optimal constant that should be added to the current prediction values is the average of the residuals
[12697]69      double s = 0.0;
70      int n = 0;
71      for (int i = startIdx; i <= endIdx; i++) {
72        int row = idx[i];
73        s += (targetArr[row] - predArr[row]);
74        n++;
75      }
76      return s / n;
[12332]77    }
78
[12873]79    #region item implementation
[12875]80    [StorableConstructor]
81    private SquaredErrorLoss(bool deserializing) : base(deserializing) { }
82
[12873]83    private SquaredErrorLoss(SquaredErrorLoss original, Cloner cloner) : base(original, cloner) { }
84
85    public override IDeepCloneable Clone(Cloner cloner) {
86      return new SquaredErrorLoss(this, cloner);
[12332]87    }
[12873]88    #endregion
[12332]89  }
90}
Note: See TracBrowser for help on using the repository browser.