APISonar


org.nd4j.autodiff.samediff.SameDiff.math

> org > nd4j > autodiff > samediff > SameDiff > math
org APIs nd4j APIs autodiff APIs samediff APIs SameDiff APIs math APIs

Example 1
public void testLogGrad() {
        SameDiff sameDiff = SameDiff.create();
        SDVariable input = sameDiff.var("x", Nd4j.linspace(1, 4, 4, DataType.DOUBLE));
        SDVariable log = sameDiff.math().log(input);
        SDVariable sum = sameDiff.sum(log, Integer.MAX_VALUE);
        INDArray result = null;
        sameDiff.calculateGradients(Collections.emptyMap(), sameDiff.getVariables().keySet());
    }
Example 2
public void testDiagPart() {
        INDArray i = Nd4j.create(5,5);

        SameDiff sd = SameDiff.create();
        SDVariable var = sd.var("in", i);
        SDVariable diag = sd.math().diagPart(var);

        INDArray out = diag.eval();
        assertEquals(1, out.rank());
    }
Example 3
public SDVariable defineLayer(SameDiff sd, SDVariable layerInput, Map<String, SDVariable> paramTable, SDVariable mask) {
        SDVariable weights = paramTable.get(DefaultParamInitializer.WEIGHT_KEY);

        SDVariable logits = sd.tensorMmul(layerInput, weights, new int[][] { {2}, {0}});
        SDVariable reshapedLogits = sd.reshape(logits, layerInput.getShape()[0], layerInput.getShape()[1]);
        SDVariable ai = sd.math().exp(reshapedLogits);
        SDVariable aiSum = sd.sum(ai, 1);
        SDVariable aiSumEps = sd.expandDims(aiSum.add(EPS), 1);
        SDVariable attentionWeights = ai.div(aiSumEps);
        SDVariable weightedInput = layerInput.mul(sd.expandDims(attentionWeights, 2));

        return sd.sum(weightedInput, 2);
    }
Example 4
public void testLogSumExp(){
        Nd4j.getRandom().setSeed(12345);
        INDArray inputArr = Nd4j.rand(DataType.FLOAT, 1, 4);
        SameDiff sd = SameDiff.create();
        SDVariable in = sd.var(inputArr);
        SDVariable lse = sd.math().logSumExp(in);
        INDArray out = lse.eval();

        INDArray exp = Transforms.exp(inputArr, true);
        INDArray sum = exp.sum();
        INDArray log = Transforms.log(sum);
        assertEquals(log, out);
    }
Example 5
public SDVariable defineVertex(SameDiff sameDiff, VertexInputs inputs) {
        //2 inputs to the vertex. The VertexInputs class will dynamically add as many variables as we request from it!
        SDVariable input1 = inputs.getInput(0);
        SDVariable input2 = inputs.getInput(1);
        SDVariable average = sameDiff.math().mergeAvg(input1, input2);
        return average;
    }