Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/LogisticRegressionLoss.cs @ 13238

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

#2450: fixed constructors for loss functions

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