Search in sources :

Example 1 with Value

use of org.nd4j.linalg.indexing.functions.Value in project deeplearning4j by deeplearning4j.

the class BarnesHutTsne method update.

@Override
public void update(INDArray gradient, String paramType) {
    INDArray yGrads = gradient;
    gains = gains.add(.2).muli(sign(yGrads)).neqi(sign(yIncs)).addi(gains.mul(0.8).muli(sign(yGrads)).neqi(sign(yIncs)));
    BooleanIndexing.applyWhere(gains, Conditions.lessThan(minGain), new Value(minGain));
    INDArray gradChange = gains.mul(yGrads);
    if (useAdaGrad) {
        if (adaGrad == null)
            adaGrad = new AdaGrad();
        gradChange = adaGrad.getGradient(gradChange, 0);
    } else
        gradChange.muli(learningRate);
    yIncs.muli(momentum).subi(gradChange);
    Y.addi(yIncs);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Value(org.nd4j.linalg.indexing.functions.Value) AdaGrad(org.nd4j.linalg.learning.AdaGrad)

Example 2 with Value

use of org.nd4j.linalg.indexing.functions.Value in project deeplearning4j by deeplearning4j.

the class Tsne method x2p.

/**
     * This method build probabilities for given source data
     *
     * @param X
     * @param tolerance
     * @param perplexity
     * @return
     */
private INDArray x2p(final INDArray X, double tolerance, double perplexity) {
    int n = X.rows();
    final INDArray p = zeros(n, n);
    final INDArray beta = ones(n, 1);
    final double logU = Math.log(perplexity);
    INDArray sumX = pow(X, 2).sum(1);
    logger.debug("sumX shape: " + Arrays.toString(sumX.shape()));
    INDArray times = X.mmul(X.transpose()).muli(-2);
    logger.debug("times shape: " + Arrays.toString(times.shape()));
    INDArray prodSum = times.transpose().addiColumnVector(sumX);
    logger.debug("prodSum shape: " + Arrays.toString(prodSum.shape()));
    INDArray D = // thats times
    X.mmul(X.transpose()).mul(-2).transpose().addColumnVector(// thats prodSum
    sumX).addRowVector(// thats D
    sumX.transpose());
    logger.info("Calculating probabilities of data similarities...");
    logger.debug("Tolerance: " + tolerance);
    for (int i = 0; i < n; i++) {
        if (i % 500 == 0 && i > 0)
            logger.info("Handled [" + i + "] records out of [" + n + "]");
        double betaMin = Double.NEGATIVE_INFINITY;
        double betaMax = Double.POSITIVE_INFINITY;
        int[] vals = Ints.concat(ArrayUtil.range(0, i), ArrayUtil.range(i + 1, n));
        INDArrayIndex[] range = new INDArrayIndex[] { new SpecifiedIndex(vals) };
        INDArray row = D.slice(i).get(range);
        Pair<Double, INDArray> pair = hBeta(row, beta.getDouble(i));
        //INDArray hDiff = pair.getFirst().sub(logU);
        double hDiff = pair.getFirst() - logU;
        int tries = 0;
        //while hdiff > tolerance
        while (Math.abs(hDiff) > tolerance && tries < 50) {
            //if hdiff > 0
            if (hDiff > 0) {
                betaMin = beta.getDouble(i);
                if (Double.isInfinite(betaMax))
                    beta.putScalar(i, beta.getDouble(i) * 2.0);
                else
                    beta.putScalar(i, (beta.getDouble(i) + betaMax) / 2.0);
            } else {
                betaMax = beta.getDouble(i);
                if (Double.isInfinite(betaMin))
                    beta.putScalar(i, beta.getDouble(i) / 2.0);
                else
                    beta.putScalar(i, (beta.getDouble(i) + betaMin) / 2.0);
            }
            pair = hBeta(row, beta.getDouble(i));
            hDiff = pair.getFirst() - logU;
            tries++;
        }
        p.slice(i).put(range, pair.getSecond());
    }
    //dont need data in memory after
    logger.info("Mean value of sigma " + sqrt(beta.rdiv(1)).mean(Integer.MAX_VALUE));
    BooleanIndexing.applyWhere(p, Conditions.isNan(), new Value(1e-12));
    //set 0 along the diagonal
    INDArray permute = p.transpose();
    INDArray pOut = p.add(permute);
    pOut.divi(pOut.sumNumber().doubleValue() + 1e-6);
    pOut.muli(4);
    BooleanIndexing.applyWhere(pOut, Conditions.lessThan(1e-12), new Value(1e-12));
    return pOut;
}
Also used : SpecifiedIndex(org.nd4j.linalg.indexing.SpecifiedIndex) INDArray(org.nd4j.linalg.api.ndarray.INDArray) INDArrayIndex(org.nd4j.linalg.indexing.INDArrayIndex) Value(org.nd4j.linalg.indexing.functions.Value)

Example 3 with Value

use of org.nd4j.linalg.indexing.functions.Value in project deeplearning4j by deeplearning4j.

the class Tsne method calculate.

public INDArray calculate(INDArray X, int targetDimensions, double perplexity) {
    // pca hook
    if (usePca) {
        X = PCA.pca(X, Math.min(50, X.columns()), normalize);
    } else if (normalize) {
        X.subi(X.min(Integer.MAX_VALUE));
        X = X.divi(X.max(Integer.MAX_VALUE));
        X = X.subiRowVector(X.mean(0));
    }
    int n = X.rows();
    // FIXME: this is wrong, another distribution required here
    Y = randn(X.rows(), targetDimensions, Nd4j.getRandom());
    INDArray dY = Nd4j.zeros(n, targetDimensions);
    INDArray iY = Nd4j.zeros(n, targetDimensions);
    INDArray gains = Nd4j.ones(n, targetDimensions);
    boolean stopLying = false;
    logger.debug("Y:Shape is = " + Arrays.toString(Y.shape()));
    // compute P-values
    INDArray P = x2p(X, tolerance, perplexity);
    // do training
    for (int i = 0; i < maxIter; i++) {
        INDArray sumY = pow(Y, 2).sum(1).transpose();
        //Student-t distribution
        //also un normalized q
        // also known as num in original implementation
        INDArray qu = Y.mmul(Y.transpose()).muli(-2).addiRowVector(sumY).transpose().addiRowVector(sumY).addi(1).rdivi(1);
        //          doAlongDiagonal(qu,new Zero());
        INDArray Q = qu.div(qu.sumNumber().doubleValue());
        BooleanIndexing.applyWhere(Q, Conditions.lessThan(1e-12), new Value(1e-12));
        INDArray PQ = P.sub(Q).muli(qu);
        logger.debug("PQ shape is: " + Arrays.toString(PQ.shape()));
        logger.debug("PQ.sum(1) shape is: " + Arrays.toString(PQ.sum(1).shape()));
        dY = diag(PQ.sum(1)).subi(PQ).mmul(Y).muli(4);
        if (i < switchMomentumIteration) {
            momentum = initialMomentum;
        } else {
            momentum = finalMomentum;
        }
        gains = gains.add(.2).muli(dY.cond(Conditions.greaterThan(0)).neqi(iY.cond(Conditions.greaterThan(0)))).addi(gains.mul(0.8).muli(dY.cond(Conditions.greaterThan(0)).eqi(iY.cond(Conditions.greaterThan(0)))));
        BooleanIndexing.applyWhere(gains, Conditions.lessThan(minGain), new Value(minGain));
        INDArray gradChange = gains.mul(dY);
        gradChange.muli(learningRate);
        iY.muli(momentum).subi(gradChange);
        double cost = P.mul(log(P.div(Q), false)).sumNumber().doubleValue();
        logger.info("Iteration [" + i + "] error is: [" + cost + "]");
        Y.addi(iY);
        //  Y.addi(iY).subiRowVector(Y.mean(0));
        INDArray tiled = Nd4j.tile(Y.mean(0), new int[] { Y.rows(), 1 });
        Y.subi(tiled);
        if (!stopLying && (i > maxIter / 2 || i >= stopLyingIteration)) {
            P.divi(4);
            stopLying = true;
        }
    }
    return Y;
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Value(org.nd4j.linalg.indexing.functions.Value)

Example 4 with Value

use of org.nd4j.linalg.indexing.functions.Value in project deeplearning4j by deeplearning4j.

the class BackTrackLineSearch method setScoreFor.

public double setScoreFor(INDArray parameters) {
    if (Nd4j.ENFORCE_NUMERICAL_STABILITY) {
        BooleanIndexing.applyWhere(parameters, Conditions.isNan(), new Value(Nd4j.EPS_THRESHOLD));
    }
    layer.setParams(parameters);
    layer.computeGradientAndScore();
    return layer.score();
}
Also used : Value(org.nd4j.linalg.indexing.functions.Value) ScalarSetValue(org.nd4j.linalg.api.ops.impl.scalar.comparison.ScalarSetValue)

Aggregations

Value (org.nd4j.linalg.indexing.functions.Value)4 INDArray (org.nd4j.linalg.api.ndarray.INDArray)3 ScalarSetValue (org.nd4j.linalg.api.ops.impl.scalar.comparison.ScalarSetValue)1 INDArrayIndex (org.nd4j.linalg.indexing.INDArrayIndex)1 SpecifiedIndex (org.nd4j.linalg.indexing.SpecifiedIndex)1 AdaGrad (org.nd4j.linalg.learning.AdaGrad)1