Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/LogisticRegressionLoss.cs @ 12872

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

#2434 reverse merge of r12871 (changes should be applied directly to trunk)

File size: 3.5 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.Diagnostics;
26using System.Linq;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  // Greedy Function Approximation: A Gradient Boosting Machine (page 9)
31  public class LogisticRegressionLoss : ILossFunction {
32    public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred) {
33      var targetEnum = target.GetEnumerator();
34      var predEnum = pred.GetEnumerator();
35
36      double s = 0;
37      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
38        Debug.Assert(targetEnum.Current.IsAlmost(0.0) || targetEnum.Current.IsAlmost(1.0), "labels must be 0 or 1 for logistic regression loss");
39
40        var y = targetEnum.Current * 2 - 1; // y in {-1,1}
41        s += Math.Log(1 + Math.Exp(-2 * y * predEnum.Current));
42      }
43      if (targetEnum.MoveNext() | predEnum.MoveNext())
44        throw new ArgumentException("target and pred have different lengths");
45
46      return s;
47    }
48
49    public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred) {
50      var targetEnum = target.GetEnumerator();
51      var predEnum = pred.GetEnumerator();
52
53      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
54        Debug.Assert(targetEnum.Current.IsAlmost(0.0) || targetEnum.Current.IsAlmost(1.0), "labels must be 0 or 1 for logistic regression loss");
55        var y = targetEnum.Current * 2 - 1; // y in {-1,1}
56
57        yield return 2 * y / (1 + Math.Exp(2 * y * predEnum.Current));
58
59      }
60      if (targetEnum.MoveNext() | predEnum.MoveNext())
61        throw new ArgumentException("target and pred have different lengths");
62    }
63
64    // targetArr and predArr are not changed by LineSearch
65    public double LineSearch(double[] targetArr, double[] predArr, int[] idx, int startIdx, int endIdx) {
66      if (targetArr.Length != predArr.Length)
67        throw new ArgumentException("target and pred have different lengths");
68
69      // "Simple Newton-Raphson step" of eqn. 23
70      double sumY = 0.0;
71      double sumDiff = 0.0;
72      for (int i = startIdx; i <= endIdx; i++) {
73        var row = idx[i];
74        var y = targetArr[row] * 2 - 1; // y in {-1,1}
75        var pseudoResponse = 2 * y / (1 + Math.Exp(2 * y * predArr[row]));
76
77        sumY += pseudoResponse;
78        sumDiff += Math.Abs(pseudoResponse) * (2 - Math.Abs(pseudoResponse));
79      }
80      // prevent divByZero
81      sumDiff = Math.Max(1E-12, sumDiff);
82      return sumY / sumDiff;
83    }
84
85    public override string ToString() {
86      return "Logistic regression loss";
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.