Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/SquaredErrorLoss.cs @ 17387

Last change on this file since 17387 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
24using System.Collections.Generic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HEAL.Attic;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [StorableType("5D02E552-B96E-4267-858B-22339D8CB6B2")]
31  [Item("Squared error loss", "")]
32  public sealed class SquaredErrorLoss : Item, ILossFunction {
33    public SquaredErrorLoss() { }
34
35    public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred) {
36      var targetEnum = target.GetEnumerator();
37      var predEnum = pred.GetEnumerator();
38
39      double s = 0;
40      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
41        double res = targetEnum.Current - predEnum.Current;
42        s += res * res; // (res)^2
43      }
44      if (targetEnum.MoveNext() | predEnum.MoveNext())
45        throw new ArgumentException("target and pred have different lengths");
46
47      return s;
48    }
49
50    public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred) {
51      var targetEnum = target.GetEnumerator();
52      var predEnum = pred.GetEnumerator();
53
54      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
55        yield return 2.0 * (targetEnum.Current - predEnum.Current); // dL(y, f(x)) / df(x)  = 2 * res
56      }
57      if (targetEnum.MoveNext() | predEnum.MoveNext())
58        throw new ArgumentException("target and pred have different lengths");
59    }
60
61    // targetArr and predArr are not changed by LineSearch
62    public double LineSearch(double[] targetArr, double[] predArr, int[] idx, int startIdx, int endIdx) {
63      if (targetArr.Length != predArr.Length)
64        throw new ArgumentException("target and pred have different lengths");
65
66      // line search for squared error loss
67      // for a given partition of rows the optimal constant that should be added to the current prediction values is the average of the residuals
68      double s = 0.0;
69      int n = 0;
70      for (int i = startIdx; i <= endIdx; i++) {
71        int row = idx[i];
72        s += (targetArr[row] - predArr[row]);
73        n++;
74      }
75      return s / n;
76    }
77
78    #region item implementation
79    [StorableConstructor]
80    private SquaredErrorLoss(StorableConstructorFlag _) : base(_) { }
81
82    private SquaredErrorLoss(SquaredErrorLoss original, Cloner cloner) : base(original, cloner) { }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      return new SquaredErrorLoss(this, cloner);
86    }
87    #endregion
88  }
89}
Note: See TracBrowser for help on using the repository browser.