Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/AbsoluteErrorLoss.cs @ 16671

Last change on this file since 16671 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 3.6 KB
RevLine 
[12590]1#region License Information
2/* HeuristicLab
[16565]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;
[12374]24using System.Collections.Generic;
25using HeuristicLab.Common;
[12873]26using HeuristicLab.Core;
[16565]27using HEAL.Attic;
[12374]28
[12590]29namespace HeuristicLab.Algorithms.DataAnalysis {
30  // loss function for the weighted absolute error
[16565]31  [StorableType("C4429BDA-665F-48A2-B18F-EA1569083842")]
[12873]32  [Item("Absolute error loss", "")]
[12875]33  public sealed class AbsoluteErrorLoss : Item, ILossFunction {
[12873]34    public AbsoluteErrorLoss() { }
35
[12696]36    public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred) {
[12374]37      var targetEnum = target.GetEnumerator();
38      var predEnum = pred.GetEnumerator();
39
40      double s = 0;
[12696]41      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
[12374]42        double res = targetEnum.Current - predEnum.Current;
[12696]43        s += Math.Abs(res);  // |res|
[12374]44      }
[12696]45      if (targetEnum.MoveNext() | predEnum.MoveNext())
46        throw new ArgumentException("target and pred have differing lengths");
[12374]47
48      return s;
49    }
50
[12696]51    public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred) {
[12374]52      var targetEnum = target.GetEnumerator();
53      var predEnum = pred.GetEnumerator();
54
[12696]55      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
56        // dL(y, f(x)) / df(x) = sign(res)
[12374]57        var res = targetEnum.Current - predEnum.Current;
[12696]58        if (res > 0) yield return 1.0;
59        else if (res < 0) yield return -1.0;
[12374]60        else yield return 0.0;
61      }
[12696]62      if (targetEnum.MoveNext() | predEnum.MoveNext())
63        throw new ArgumentException("target and pred have differing lengths");
[12374]64    }
65
[12597]66    // return median of residuals
[12697]67    // targetArr and predArr are not changed by LineSearch
68    public double LineSearch(double[] targetArr, double[] predArr, int[] idx, int startIdx, int endIdx) {
[12696]69      if (targetArr.Length != predArr.Length)
70        throw new ArgumentException("target and pred have differing lengths");
[12374]71
[12697]72      // Median() is allocating an array anyway
73      // It would be possible to pre-allocated an array for the residuals if Median() would allow specification of a sub-range
74      int nRows = endIdx - startIdx + 1;
75      var res = new double[nRows];
76      for (int i = startIdx; i <= endIdx; i++) {
77        var row = idx[i];
78        res[i - startIdx] = targetArr[row] - predArr[row];
79      }
80      return res.Median(); // TODO: improve efficiency
[12374]81    }
82
[12873]83    #region item implementation
[12875]84    [StorableConstructor]
[16565]85    private AbsoluteErrorLoss(StorableConstructorFlag _) : base(_) { }
[12875]86
[12873]87    private AbsoluteErrorLoss(AbsoluteErrorLoss original, Cloner cloner) : base(original, cloner) { }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new AbsoluteErrorLoss(this, cloner);
[12374]91    }
[12873]92    #endregion
[12374]93  }
94}
Note: See TracBrowser for help on using the repository browser.