All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.expleague.ml.loss.LLX2 Maven / Gradle / Ivy

There is a newer version: 1.4.9
Show newest version
package com.expleague.ml.loss;

import com.expleague.commons.math.FuncC1;
import com.expleague.commons.math.vectors.Vec;
import com.expleague.commons.math.vectors.impl.vectors.ArrayVec;
import com.expleague.ml.data.set.DataSet;
import com.expleague.ml.TargetFunc;

import static java.lang.Math.exp;
import static java.lang.Math.log;

/**
 * We use value representation = e^{-(x + 1)^2}.
 * User: solar
 * Date: 21.12.2010
 * Time: 22:37:55
 */
public class LLX2 extends FuncC1.Stub implements TargetFunc {
  private final Vec target;
  private final DataSet owner;

  public LLX2(final Vec target, final DataSet owner) {
    this.target = target;
    this.owner = owner;
  }

  @Override
  public Vec gradient(final Vec point) {
    final Vec result = new ArrayVec(point.dim());
    for (int i = 0; i < point.dim(); i++) {
      final double x = point.get(i) + 1;
      if (target.get(i) > 0) // positive example
        result.set(i, -2 * x);
      else // negative
        result.set(i, 2 * x/(exp(x*x) - 1));
    }
    return result;
  }

  @Override
  public int dim() {
    return target.dim();
  }

  @Override
  public double value(final Vec point) {
    double result = 0;
    for (int i = 0; i < point.dim(); i++) {
      final double x = point.get(i) + 1;
      final double pX = exp(-x * x);
      if (target.get(i) > 0) // positive example
        result -= log(pX);
      else // negative
        result -= log(1 - pX);
    }

    return result;
  }

  @Override
  public DataSet owner() {
    return owner;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy