本文整理汇总了Java中org.nd4j.linalg.ops.transforms.Transforms.log方法的典型用法代码示例。如果您正苦于以下问题:Java Transforms.log方法的具体用法?Java Transforms.log怎么用?Java Transforms.log使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.nd4j.linalg.ops.transforms.Transforms
的用法示例。
在下文中一共展示了Transforms.log方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: scoreArray
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) {
if (labels.size(1) != preOutput.size(1)) {
throw new IllegalArgumentException(
"Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer"
+ " number of outputs (nOut = " + preOutput.size(1) + ") ");
}
/*
mean of (yhat - y * log(yhat))
*/
//INDArray postOutput = Nd4j.utioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup()));
INDArray postOutput = activationFn.getActivation(preOutput.dup(), true);
INDArray scoreArr = Transforms.log(postOutput);
scoreArr.muli(labels);
scoreArr = postOutput.sub(scoreArr);
if (mask != null) {
LossUtil.applyMask(scoreArr, mask);
}
return scoreArr;
}
示例2: scoreArray
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
private INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) {
if (labels.size(1) != preOutput.size(1)) {
throw new IllegalArgumentException(
"Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer"
+ " number of outputs (nOut = " + preOutput.size(1) + ") ");
}
INDArray output = activationFn.getActivation(preOutput.dup(), true);
// Clip output and labels to be between Nd4j.EPS_THREsHOLD and 1, i.e. a valid non-zero probability
output = Transforms.min(Transforms.max(output, Nd4j.EPS_THRESHOLD, false), 1, false);
labels = Transforms.min(Transforms.max(labels, Nd4j.EPS_THRESHOLD, true), 1, false);
INDArray logRatio = Transforms.log(output.rdivi(labels), false);
INDArray scoreArr = logRatio.muli(labels);
if (mask != null) {
LossUtil.applyMask(scoreArr, mask);
}
return scoreArr;
}
示例3: testLog
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testLog() {
INDArray log = Nd4j.linspace(1, 6, 6);
INDArray transformed = Transforms.log(log);
INDArray assertion = Nd4j.create(new double[] {0., 0.69314718, 1.09861229, 1.38629436, 1.60943791, 1.79175947});
assertEquals(assertion, transformed);
}
示例4: testLog
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testLog() {
INDArray arr = Nd4j.linspace(1, 4, 4).reshape(2, 2);
INDArray assertion = Nd4j.create(new double[][] {{0., 1.09861229}, {0.69314718, 1.38629436}});
INDArray logTest = Transforms.log(arr);
assertEquals(assertion, logTest);
arr = Nd4j.linspace(1, 6, 6).reshape(2, 3);
assertion = Nd4j.create(new double[][] {{0., 1.09861229, 1.60943791}, {0.69314718, 1.38629436, 1.79175947}});
logTest = Transforms.log(arr);
assertEquals(assertion, logTest);
}
示例5: testLogDouble
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testLogDouble() {
INDArray linspace = Nd4j.linspace(1, 6, 6);
INDArray log = Transforms.log(linspace);
INDArray assertion = Nd4j.create(new double[] {0, 0.6931471805599453, 1.0986122886681098, 1.3862943611198906,
1.6094379124341005, 1.791759469228055});
assertEquals(assertion, log);
}
示例6: testStridedTransforms1
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testStridedTransforms1() throws Exception {
//output: Rank: 2,Offset: 0
//Order: c Shape: [5,2], stride: [2,1]
//output: [0.5086864, 0.49131358, 0.50720876, 0.4927912, 0.46074104, 0.53925896, 0.49314, 0.50686, 0.5217741, 0.4782259]
double[] d = {0.5086864, 0.49131358, 0.50720876, 0.4927912, 0.46074104, 0.53925896, 0.49314, 0.50686, 0.5217741,
0.4782259};
INDArray in = Nd4j.create(d, new int[] {5, 2}, 'c');
INDArray col0 = in.getColumn(0);
INDArray col1 = in.getColumn(1);
float[] exp0 = new float[d.length / 2];
float[] exp1 = new float[d.length / 2];
for (int i = 0; i < col0.length(); i++) {
exp0[i] = (float) Math.log(col0.getDouble(i));
exp1[i] = (float) Math.log(col1.getDouble(i));
}
INDArray out0 = Transforms.log(col0, true);
INDArray out1 = Transforms.log(col1, true);
assertArrayEquals(exp0, out0.data().asFloat(), 1e-4f);
assertArrayEquals(exp1, out1.data().asFloat(), 1e-4f);
}
示例7: testLogDouble
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testLogDouble() {
INDArray linspace = Nd4j.linspace(1, 6, 6);
INDArray log = Transforms.log(linspace);
INDArray assertion = Nd4j.create(new double[] {0, 0.6931471805599453, 1.0986122886681098, 1.3862943611198906,
1.6094379124341005, 1.791759469228055});
assertEquals(assertion, log);
}
示例8: exec
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public void exec(int... dimensions) {
if (dimensions[0] != 1)
throw new IllegalArgumentException("Only supports row wise calculations");
Nd4j.getExecutioner().exec(new OldSoftMax(x,z));
Transforms.log(z, false);
}
示例9: scoreArray
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) {
if (labels.size(1) != preOutput.size(1)) {
throw new IllegalArgumentException(
"Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer"
+ " number of outputs (nOut = " + preOutput.size(1) + ") ");
}
INDArray scoreArr;
//INDArray output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup()));
INDArray output = activationFn.getActivation(preOutput.dup(), true);
scoreArr = Transforms.log(output.addi(1.0).divi(labels.add(1.0)), false);
scoreArr = scoreArr.muli(scoreArr).divi(labels.size(1));
//Weighted loss function
if (weights != null) {
if (weights.length() != output.size(1)) {
throw new IllegalStateException("Weights vector (length " + weights.length()
+ ") does not match output.size(1)=" + output.size(1));
}
scoreArr.muliRowVector(weights);
}
if (mask != null) {
LossUtil.applyMask(scoreArr, mask);
}
return scoreArr;
}
示例10: computeGradient
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) {
if (labels.size(1) != preOutput.size(1)) {
throw new IllegalArgumentException(
"Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer"
+ " number of outputs (nOut = " + preOutput.size(1) + ") ");
}
//INDArray output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup()));
INDArray output = activationFn.getActivation(preOutput.dup(), true);
INDArray p1 = output.add(1.0);
INDArray dlda = p1.rdiv(2.0 / labels.size(1));
INDArray logRatio = Transforms.log(p1.divi(labels.add(1.0)), false);
dlda.muli(logRatio);
if (weights != null) {
dlda.muliRowVector(weights);
}
if (mask != null && LossUtil.isPerOutputMasking(dlda, mask)) {
//For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later
//but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j)
//We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be
// error prone - though buy us a tiny bit of performance
LossUtil.applyMask(dlda, mask);
}
//dL/dz
INDArray gradients = activationFn.backprop(preOutput, dlda).getFirst(); //TODO activation functions with weights
if (mask != null) {
LossUtil.applyMask(gradients, mask);
}
return gradients;
}
示例11: map
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public NDArrayWritable map(Writable w) {
NDArrayWritable n = (NDArrayWritable) w;
INDArray i = n.get();
if (i == null) {
return n;
}
NDArrayWritable o;
switch (mathFunction) {
case ABS:
o = new NDArrayWritable(Transforms.abs(i, DUP));
break;
case ACOS:
o = new NDArrayWritable(Transforms.acos(i, DUP));
break;
case ASIN:
o = new NDArrayWritable(Transforms.asin(i, DUP));
break;
case ATAN:
o = new NDArrayWritable(Transforms.atan(i, DUP));
break;
case CEIL:
o = new NDArrayWritable(Transforms.ceil(i, DUP));
break;
case COS:
o = new NDArrayWritable(Transforms.cos(i, DUP));
break;
case COSH:
//No cosh operation in ND4J
throw new UnsupportedOperationException("sinh operation not yet supported for NDArray columns");
case EXP:
o = new NDArrayWritable(Transforms.exp(i, DUP));
break;
case FLOOR:
o = new NDArrayWritable(Transforms.floor(i, DUP));
break;
case LOG:
o = new NDArrayWritable(Transforms.log(i, DUP));
break;
case LOG10:
o = new NDArrayWritable(Transforms.log(i, 10.0, DUP));
break;
case SIGNUM:
o = new NDArrayWritable(Transforms.sign(i, DUP));
break;
case SIN:
o = new NDArrayWritable(Transforms.sin(i, DUP));
break;
case SINH:
//No sinh op in ND4J
throw new UnsupportedOperationException("sinh operation not yet supported for NDArray columns");
case SQRT:
o = new NDArrayWritable(Transforms.sqrt(i, DUP));
break;
case TAN:
//No tan op in ND4J yet - but tan(x) = sin(x)/cos(x)
INDArray sinx = Transforms.sin(i, true);
INDArray cosx = Transforms.cos(i, true);
o = new NDArrayWritable(sinx.divi(cosx));
break;
case TANH:
o = new NDArrayWritable(Transforms.tanh(i, DUP));
break;
default:
throw new RuntimeException("Unknown function: " + mathFunction);
}
//To avoid threading issues...
Nd4j.getExecutioner().commit();
return o;
}
示例12: apply
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public INDArray apply(INDArray input) {
return Transforms.log(input.dup());
}
示例13: scoreArray
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
private INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) {
if (labels.size(1) != preOutput.size(1)) {
throw new IllegalArgumentException(
"Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer"
+ " number of outputs (nOut = " + preOutput.size(1) + ") ");
}
INDArray scoreArr;
if (activationFn instanceof ActivationSoftmax) {
//Use LogSoftMax op to avoid numerical issues when calculating score
INDArray logsoftmax = Nd4j.getExecutioner().execAndReturn(new LogSoftMax(preOutput.dup()));
scoreArr = logsoftmax.muli(labels);
} else {
//INDArray output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup()));
INDArray output = activationFn.getActivation(preOutput.dup(), true);
if (clipEps > 0.0) {
CustomOp op = DynamicCustomOp.builder("clipbyvalue")
.addInputs(output)
.callInplace(true)
.addFloatingPointArguments(clipEps, 1.0-clipEps)
.build();
Nd4j.getExecutioner().exec(op);
}
scoreArr = Transforms.log(output, true).muli(labels);
INDArray secondTerm = output.rsubi(1);
Transforms.log(secondTerm, false);
secondTerm.muli(labels.rsub(1));
scoreArr.addi(secondTerm);
}
//Weighted loss function
if (weights != null) {
if (weights.length() != preOutput.size(1)) {
throw new IllegalStateException("Weights vector (length " + weights.length()
+ ") does not match output.size(1)=" + preOutput.size(1));
}
scoreArr.muliRowVector(weights);
}
if (mask != null) {
LossUtil.applyMask(scoreArr, mask);
}
return scoreArr;
}
示例14: testLogX1
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testLogX1() {
INDArray x = Nd4j.create(10).assign(7);
INDArray logX5 = Transforms.log(x, 5, true);
INDArray exp = Transforms.log(x, true).div(Transforms.log(Nd4j.create(10).assign(5)));
assertEquals(exp, logX5);
}