Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/SquaredErrorLoss.cs @ 12590

Last change on this file since 12590 was 12590, checked in by gkronber, 9 years ago

#2261: preparations for trunk integration (adapt to current trunk version, add license headers, add comments, improve code quality)

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 System.Linq;
26
27namespace HeuristicLab.Algorithms.DataAnalysis {
28  public class SquaredErrorLoss : ILossFunction {
29    public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred, IEnumerable<double> weight) {
30      var targetEnum = target.GetEnumerator();
31      var predEnum = pred.GetEnumerator();
32      var weightEnum = weight.GetEnumerator();
33
34      double s = 0;
35      while (targetEnum.MoveNext() & predEnum.MoveNext() & weightEnum.MoveNext()) {
36        double res = targetEnum.Current - predEnum.Current;
37        s += weightEnum.Current * res * res;
38      }
39      if (targetEnum.MoveNext() | predEnum.MoveNext() | weightEnum.MoveNext())
40        throw new ArgumentException("target, pred and weight have differing lengths");
41
42      return s;
43    }
44
45    public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred, IEnumerable<double> weight) {
46      var targetEnum = target.GetEnumerator();
47      var predEnum = pred.GetEnumerator();
48      var weightEnum = weight.GetEnumerator();
49
50      while (targetEnum.MoveNext() & predEnum.MoveNext() & weightEnum.MoveNext()) {
51        yield return weightEnum.Current * 2.0 * (targetEnum.Current - predEnum.Current);
52      }
53      if (targetEnum.MoveNext() | predEnum.MoveNext() | weightEnum.MoveNext())
54        throw new ArgumentException("target, pred and weight have differing lengths");
55    }
56
57    public LineSearchFunc GetLineSearchFunc(IEnumerable<double> target, IEnumerable<double> pred, IEnumerable<double> weight) {
58      var targetArr = target.ToArray();
59      var predArr = pred.ToArray();
60      var weightArr = weight.ToArray();
61      if (targetArr.Length != predArr.Length || predArr.Length != weightArr.Length)
62        throw new ArgumentException("target, pred and weight have differing lengths");
63
64      // line search for squared error loss => return the average value
65      LineSearchFunc lineSearch = (idx, startIdx, endIdx) => {
66        double s = 0.0;
67        int n = 0;
68        for (int i = startIdx; i <= endIdx; i++) {
69          int row = idx[i];
70          s += (targetArr[row] - predArr[row]);
71          n++;
72        }
73        return s / n;
74      };
75      return lineSearch;
76
77    }
78
79    public override string ToString() {
80      return "Squared error loss";
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.