org.nd4j.linalg.lossfunctions.impl.LossL2 Maven / Gradle / Ivy
package org.nd4j.linalg.lossfunctions.impl;
import lombok.EqualsAndHashCode;
import org.apache.commons.math3.util.Pair;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.lossfunctions.ILossFunction;
import org.nd4j.linalg.lossfunctions.LossUtil;
/**
* Created by susaneraly on 9/14/16.
*/
@EqualsAndHashCode
public class LossL2 implements ILossFunction {
public INDArray scoreArray(INDArray labels, INDArray preOutput, String activationFn, INDArray mask) {
INDArray scoreArr;
INDArray output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup()));
scoreArr = output.rsubi(labels);
scoreArr = scoreArr.muli(scoreArr);
if (mask != null) {
scoreArr.muliColumnVector(mask);
}
return scoreArr;
}
@Override
public double computeScore(INDArray labels, INDArray preOutput, String activationFn, INDArray mask, boolean average) {
INDArray scoreArr = scoreArray(labels, preOutput, activationFn, mask);
double score = scoreArr.sumNumber().doubleValue();
if (average) score /= scoreArr.size(0);
return score;
}
@Override
public INDArray computeScoreArray(INDArray labels, INDArray preOutput, String activationFn, INDArray mask) {
INDArray scoreArr = scoreArray(labels, preOutput, activationFn, mask);
return scoreArr.sum(1);
}
@Override
public INDArray computeGradient(INDArray labels, INDArray preOutput, String activationFn, INDArray mask) {
INDArray output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup()));
INDArray gradients;
if ("softmax".equals(activationFn)) {
gradients = LossUtil.dLdZsoftmaxi(output.sub(labels).muli(2), output);
} else {
INDArray sigmaPrimeZ = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup()).derivative());
gradients = output.subi(labels).muli(2).muli(sigmaPrimeZ);
}
if (mask != null) {
gradients.muliColumnVector(mask);
}
return gradients;
}
@Override
public org.apache.commons.math3.util.Pair computeGradientAndScore(INDArray labels, INDArray preOutput, String activationFn, INDArray mask, boolean average) {
//TODO: probably a more efficient way to do this...
return new Pair<>(
computeScore(labels, preOutput, activationFn, mask, average),
computeGradient(labels, preOutput, activationFn, mask));
}
@Override
public String toString() {
return "LossL2()";
}
}