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