This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.examples.group.bgd.loss;
020
021import javax.inject.Inject;
022
023/**
024 * Weighted logistic {@link LossFunction}.
025 */
026public final class WeightedLogisticLossFunction implements LossFunction {
027
028  private static final double POS = 0.0025;
029  private static final double NEG = 0.9975;
030
031  private final double posWeight;
032  private final double negWeight;
033
034  /**
035   * Trivial constructor.
036   */
037  @Inject
038  public WeightedLogisticLossFunction() {
039    this.posWeight = (POS + NEG) / (2 * POS);
040    this.negWeight = (POS + NEG) / (2 * NEG);
041  }
042
043  @Override
044  public double computeLoss(final double y, final double f) {
045
046    final double predictedTimesLabel = y * f;
047    final double weight = y == -1 ? this.negWeight : this.posWeight;
048
049    if (predictedTimesLabel >= 0) {
050      return weight * Math.log(1 + Math.exp(-predictedTimesLabel));
051    } else {
052      return weight * (-predictedTimesLabel + Math.log(1 + Math.exp(predictedTimesLabel)));
053    }
054  }
055
056  @Override
057  public double computeGradient(final double y, final double f) {
058
059    final double predictedTimesLabel = y * f;
060    final double weight = y == -1 ? this.negWeight : this.posWeight;
061
062    final double probability;
063    if (predictedTimesLabel >= 0) {
064      probability = 1 / (1 + Math.exp(-predictedTimesLabel));
065    } else {
066      final double expVal = Math.exp(predictedTimesLabel);
067      probability = expVal / (1 + expVal);
068    }
069
070    return (probability - 1) * y * weight;
071  }
072
073  @Override
074  public String toString() {
075    return "WeightedLogisticLossFunction{}";
076  }
077}