Search in sources :

Example 1 with OldSoftMax

use of org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax in project nd4j by deeplearning4j.

the class SoftMaxDerivative method exec.

@Override
public void exec() {
    INDArray softmaxed = Nd4j.getExecutioner().execAndReturn(new OldSoftMax(x));
    INDArray mulled = softmaxed.muli(y);
    INDArray summed = mulled.sum(-1);
    softmaxed.muliColumnVector(summed);
    mulled.subi(softmaxed);
}
Also used : OldSoftMax(org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax) INDArray(org.nd4j.linalg.api.ndarray.INDArray)

Example 2 with OldSoftMax

use of org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax in project nd4j by deeplearning4j.

the class ActivationSoftmax method backprop.

@Override
public Pair<INDArray, INDArray> backprop(INDArray in, INDArray epsilon) {
    /*
        //libnd4j only returns diagonal elements, fix in libnd4j?
        //derivative of softmax(in) shape = minibatchxclasses should give minibatch x classes x classes
        int miniBatchSize = in.shape()[0];
        int classSize = in.shape()[1];
        //if (in.rank() != 2) throw exception?
        INDArray z = Nd4j.zeros(miniBatchSize,classSize,classSize);
        INDArray i = Nd4j.eye(classSize);
        INDArray out = z.dup();
        
        //identity matrix extended to 3d
        Nd4j.getExecutioner().execAndReturn(new BroadcastAddOp(z,i,out,new int[] {1,2}));
        
        //D_jS_j = S_i * (delta_ij - S_j)
        Nd4j.getExecutioner().execAndReturn(new BroadcastSubOp(out,in,z,new int[] {0,1}));//1-p or -p
        Nd4j.getExecutioner().execAndReturn(new BroadcastMulOp(z,in,out,new int[] {0,1}));//p*(1-p) or -pi*pj
        
        gradient = out;
        */
    // use loss fn utils and push this for next release
    // Nd4j.getExecutioner().execAndReturn(new SoftMax(in).derivative());
    // return in;
    INDArray out = Nd4j.getExecutioner().execAndReturn(new OldSoftMax(in));
    INDArray x = out.mul(epsilon).sum(1);
    INDArray dLdz = out.mul(epsilon.subColumnVector(x));
    return new Pair<>(dLdz, null);
}
Also used : OldSoftMax(org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Pair(org.nd4j.linalg.primitives.Pair)

Example 3 with OldSoftMax

use of org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax in project nd4j by deeplearning4j.

the class LoneTest method testSoftmaxStability.

@Test
public void testSoftmaxStability() {
    INDArray input = Nd4j.create(new double[] { -0.75, 0.58, 0.42, 1.03, -0.61, 0.19, -0.37, -0.40, -1.42, -0.04 }).transpose();
    System.out.println("Input transpose " + Shape.shapeToString(input.shapeInfo()));
    INDArray output = Nd4j.create(10, 1);
    System.out.println("Element wise stride of output " + output.elementWiseStride());
    Nd4j.getExecutioner().exec(new OldSoftMax(input, output));
}
Also used : OldSoftMax(org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Example 4 with OldSoftMax

use of org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax in project nd4j by deeplearning4j.

the class CrashTest method op.

protected void op(INDArray x, INDArray y, int i) {
    // broadcast along row & column
    INDArray row = Nd4j.ones(64);
    INDArray column = Nd4j.ones(1024, 1);
    x.addiRowVector(row);
    x.addiColumnVector(column);
    // casual scalar
    x.addi(i * 2);
    // reduction along all dimensions
    float sum = x.sumNumber().floatValue();
    // index reduction
    Nd4j.getExecutioner().exec(new IMax(x), Integer.MAX_VALUE);
    // casual transform
    Nd4j.getExecutioner().exec(new Sqrt(x, x));
    // dup
    INDArray x1 = x.dup(x.ordering());
    INDArray x2 = x.dup(x.ordering());
    INDArray x3 = x.dup('c');
    INDArray x4 = x.dup('f');
    // vstack && hstack
    INDArray vstack = Nd4j.vstack(x, x1, x2, x3, x4);
    INDArray hstack = Nd4j.hstack(x, x1, x2, x3, x4);
    // reduce3 call
    Nd4j.getExecutioner().exec(new ManhattanDistance(x, x2));
    // flatten call
    INDArray flat = Nd4j.toFlattened(x, x1, x2, x3, x4);
    // reduction along dimension: row & column
    INDArray max_0 = x.max(0);
    INDArray max_1 = x.max(1);
    // index reduction along dimension: row & column
    INDArray imax_0 = Nd4j.argMax(x, 0);
    INDArray imax_1 = Nd4j.argMax(x, 1);
    // logisoftmax, softmax & softmax derivative
    Nd4j.getExecutioner().exec(new OldSoftMax(x));
    Nd4j.getExecutioner().exec(new SoftMaxDerivative(x));
    Nd4j.getExecutioner().exec(new LogSoftMax(x));
    // BooleanIndexing
    BooleanIndexing.replaceWhere(x, 5f, Conditions.lessThan(8f));
    // assing on view
    BooleanIndexing.assignIf(x, x1, Conditions.greaterThan(-1000000000f));
    // std var along all dimensions
    float std = x.stdNumber().floatValue();
    // std var along row & col
    INDArray xStd_0 = x.std(0);
    INDArray xStd_1 = x.std(1);
    // blas call
    float dot = (float) Nd4j.getBlasWrapper().dot(x, x1);
    // mmul
    for (boolean tA : paramsA) {
        for (boolean tB : paramsB) {
            INDArray xT = tA ? x.dup() : x.dup().transpose();
            INDArray yT = tB ? y.dup() : y.dup().transpose();
            Nd4j.gemm(xT, yT, tA, tB);
        }
    }
    // specially for views, checking here without dup and rollover
    Nd4j.gemm(x, y, false, false);
    System.out.println("Iteration passed: " + i);
}
Also used : OldSoftMax(org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax) LogSoftMax(org.nd4j.linalg.api.ops.impl.transforms.LogSoftMax) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Sqrt(org.nd4j.linalg.api.ops.impl.transforms.Sqrt) SoftMaxDerivative(org.nd4j.linalg.api.ops.impl.transforms.SoftMaxDerivative) IMax(org.nd4j.linalg.api.ops.impl.indexaccum.IMax) ManhattanDistance(org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance)

Example 5 with OldSoftMax

use of org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax in project nd4j by deeplearning4j.

the class HalfOpsTests method testSoftmax1.

@Test
public void testSoftmax1() throws Exception {
    INDArray array1 = Nd4j.zeros(15);
    array1.putScalar(0, 0.9f);
    Nd4j.getExecutioner().exec(new OldSoftMax(array1));
    System.out.println("Array1: " + array1);
    assertEquals(1.0f, array1.sumNumber().doubleValue(), 0.01f);
    assertEquals(0.14f, array1.getFloat(0), 0.01f);
}
Also used : OldSoftMax(org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Aggregations

INDArray (org.nd4j.linalg.api.ndarray.INDArray)6 OldSoftMax (org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax)6 Test (org.junit.Test)3 SoftMaxDerivative (org.nd4j.linalg.api.ops.impl.transforms.SoftMaxDerivative)2 ManhattanDistance (org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance)1 IMax (org.nd4j.linalg.api.ops.impl.indexaccum.IMax)1 LogSoftMax (org.nd4j.linalg.api.ops.impl.transforms.LogSoftMax)1 Sqrt (org.nd4j.linalg.api.ops.impl.transforms.Sqrt)1 Pair (org.nd4j.linalg.primitives.Pair)1