use of org.nd4j.linalg.api.ops.impl.transforms.SoftMax in project deeplearning4j by deeplearning4j.
the class LossFunctionGradientCheck method getFeaturesAndLabels.
public static INDArray[] getFeaturesAndLabels(ILossFunction l, int[] featuresShape, int[] labelsShape, long seed) {
Nd4j.getRandom().setSeed(seed);
Random r = new Random(seed);
INDArray[] ret = new INDArray[2];
ret[0] = Nd4j.rand(featuresShape);
switch(l.getClass().getSimpleName()) {
case "LossBinaryXENT":
//Want binary vector labels
ret[1] = Nd4j.rand(labelsShape);
BooleanIndexing.replaceWhere(ret[1], 0, Conditions.lessThanOrEqual(0.5));
BooleanIndexing.replaceWhere(ret[1], 1, Conditions.greaterThanOrEqual(0.5));
break;
case "LossCosineProximity":
//Should be real-valued??
ret[1] = Nd4j.rand(labelsShape).subi(0.5);
break;
case "LossKLD":
//KL divergence: should be a probability distribution for labels??
ret[1] = Nd4j.rand(labelsShape);
Nd4j.getExecutioner().exec(new SoftMax(ret[1]), 1);
break;
case "LossMCXENT":
case "LossNegativeLogLikelihood":
ret[1] = Nd4j.zeros(labelsShape);
if (labelsShape.length == 2) {
for (int i = 0; i < labelsShape[0]; i++) {
ret[1].putScalar(i, r.nextInt(labelsShape[1]), 1.0);
}
} else if (labelsShape.length == 3) {
for (int i = 0; i < labelsShape[0]; i++) {
for (int j = 0; j < labelsShape[2]; j++) {
ret[1].putScalar(i, r.nextInt(labelsShape[1]), j, 1.0);
}
}
} else {
throw new UnsupportedOperationException();
}
break;
case "LossHinge":
case "LossSquaredHinge":
ret[1] = Nd4j.ones(labelsShape);
if (labelsShape.length == 2) {
for (int i = 0; i < labelsShape[0]; i++) {
ret[1].putScalar(i, r.nextInt(labelsShape[1]), -1.0);
}
} else if (labelsShape.length == 3) {
for (int i = 0; i < labelsShape[0]; i++) {
for (int j = 0; j < labelsShape[2]; j++) {
ret[1].putScalar(i, r.nextInt(labelsShape[1]), j, -1.0);
}
}
} else {
throw new UnsupportedOperationException();
}
break;
case "LossMAPE":
//requires non-zero values for actual...
//1 to 2
ret[1] = Nd4j.rand(labelsShape).addi(1.0);
break;
case "LossMAE":
case "LossMSE":
case "LossL1":
case "LossL2":
ret[1] = Nd4j.rand(labelsShape).muli(2).subi(1);
break;
case "LossMSLE":
//Requires positive labels/activations due to log
ret[1] = Nd4j.rand(labelsShape);
break;
case "LossPoisson":
//Binary vector labels should be OK here??
ret[1] = Nd4j.rand(labelsShape);
BooleanIndexing.replaceWhere(ret[1], 0, Conditions.lessThanOrEqual(0.5));
BooleanIndexing.replaceWhere(ret[1], 1, Conditions.greaterThanOrEqual(0.5));
break;
default:
throw new IllegalArgumentException("Unknown class: " + l.getClass().getSimpleName());
}
return ret;
}
use of org.nd4j.linalg.api.ops.impl.transforms.SoftMax in project deeplearning4j by deeplearning4j.
the class RnnOutputLayer method output.
@Override
public INDArray output(boolean training) {
//Assume that input is 3d
if (input.rank() != 3)
throw new IllegalArgumentException("input must be rank 3");
INDArray preOutput2d = preOutput2d(training);
//if(conf.getLayer().getActivationFunction().equals("softmax")) {
if (conf.getLayer().getActivationFn() instanceof ActivationSoftmax) {
INDArray out2d = Nd4j.getExecutioner().execAndReturn(new SoftMax(preOutput2d));
if (maskArray != null) {
out2d.muliColumnVector(maskArray);
}
return TimeSeriesUtils.reshape2dTo3d(out2d, input.size(0));
}
if (training)
applyDropOutIfNecessary(training);
INDArray origInput = input;
this.input = TimeSeriesUtils.reshape3dTo2d(input);
INDArray out = super.activate(true);
this.input = origInput;
if (maskArray != null) {
out.muliColumnVector(maskArray);
}
return TimeSeriesUtils.reshape2dTo3d(out, input.size(0));
}
Aggregations