Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/SquaredErrorLoss.cs @ 16462

Last change on this file since 16462 was 16462, checked in by jkarder, 5 years ago

#2520: worked on reintegration of new persistence

  • added nuget references to HEAL.Fossil
  • added StorableType attributes to many classes
  • changed signature of StorableConstructors
  • removed some classes in old persistence
  • removed some unnecessary usings
File size: 3.3 KB
RevLine 
[12590]1#region License Information
2/* HeuristicLab
[16453]3 * Copyright (C) 2002-2019 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
23using System;
[12332]24using System.Collections.Generic;
[12873]25using HeuristicLab.Common;
26using HeuristicLab.Core;
[16462]27using HEAL.Fossil;
[12332]28
[12590]29namespace HeuristicLab.Algorithms.DataAnalysis {
[16462]30  [StorableType("5D02E552-B96E-4267-858B-22339D8CB6B2")]
[12873]31  [Item("Squared error loss", "")]
[12875]32  public sealed class SquaredErrorLoss : Item, ILossFunction {
[12873]33    public SquaredErrorLoss() { }
34
[12696]35    public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred) {
[12332]36      var targetEnum = target.GetEnumerator();
37      var predEnum = pred.GetEnumerator();
38
39      double s = 0;
[12696]40      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
[12332]41        double res = targetEnum.Current - predEnum.Current;
[12696]42        s += res * res; // (res)^2
[12332]43      }
[12696]44      if (targetEnum.MoveNext() | predEnum.MoveNext())
45        throw new ArgumentException("target and pred have different lengths");
[12332]46
47      return s;
48    }
49
[12696]50    public IEnumerable<double> GetLossGradient(IEnumerable<double> target, IEnumerable<double> pred) {
[12332]51      var targetEnum = target.GetEnumerator();
52      var predEnum = pred.GetEnumerator();
53
[12696]54      while (targetEnum.MoveNext() & predEnum.MoveNext()) {
55        yield return 2.0 * (targetEnum.Current - predEnum.Current); // dL(y, f(x)) / df(x)  = 2 * res
[12332]56      }
[12696]57      if (targetEnum.MoveNext() | predEnum.MoveNext())
58        throw new ArgumentException("target and pred have different lengths");
[12332]59    }
60
[12697]61    // targetArr and predArr are not changed by LineSearch
62    public double LineSearch(double[] targetArr, double[] predArr, int[] idx, int startIdx, int endIdx) {
[12696]63      if (targetArr.Length != predArr.Length)
64        throw new ArgumentException("target and pred have different lengths");
[12332]65
[12607]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
[12697]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;
[12332]76    }
77
[12873]78    #region item implementation
[12875]79    [StorableConstructor]
[16462]80    private SquaredErrorLoss(StorableConstructorFlag _) : base(_) { }
[12875]81
[12873]82    private SquaredErrorLoss(SquaredErrorLoss original, Cloner cloner) : base(original, cloner) { }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      return new SquaredErrorLoss(this, cloner);
[12332]86    }
[12873]87    #endregion
[12332]88  }
89}
Note: See TracBrowser for help on using the repository browser.