Search in sources :

Example 1 with INDArrayIndex

use of org.nd4j.linalg.indexing.INDArrayIndex in project deeplearning4j by deeplearning4j.

the class SequenceRecordReaderDataSetIterator method nextMultipleSequenceReaders.

private DataSet nextMultipleSequenceReaders(List<INDArray> featureList, List<INDArray> labelList, List<RecordMetaData> meta) {
    //Convert 2d sequences/time series to 3d minibatch data
    INDArray featuresOut;
    INDArray labelsOut;
    INDArray featuresMask = null;
    INDArray labelsMask = null;
    if (alignmentMode == AlignmentMode.EQUAL_LENGTH) {
        int[] featureShape = new int[3];
        //mini batch size
        featureShape[0] = featureList.size();
        //example vector size
        featureShape[1] = featureList.get(0).size(1);
        //time series/sequence length
        featureShape[2] = featureList.get(0).size(0);
        int[] labelShape = new int[3];
        labelShape[0] = labelList.size();
        //label vector size
        labelShape[1] = labelList.get(0).size(1);
        //time series/sequence length
        labelShape[2] = labelList.get(0).size(0);
        featuresOut = Nd4j.create(featureShape, 'f');
        labelsOut = Nd4j.create(labelShape, 'f');
        for (int i = 0; i < featureList.size(); i++) {
            featuresOut.tensorAlongDimension(i, 1, 2).permutei(1, 0).assign(featureList.get(i));
            labelsOut.tensorAlongDimension(i, 1, 2).permutei(1, 0).assign(labelList.get(i));
        }
    } else if (alignmentMode == AlignmentMode.ALIGN_START) {
        int longestTimeSeries = 0;
        for (INDArray features : featureList) {
            longestTimeSeries = Math.max(features.size(0), longestTimeSeries);
        }
        for (INDArray labels : labelList) {
            longestTimeSeries = Math.max(labels.size(0), longestTimeSeries);
        }
        int[] featuresShape = new int[] { //# examples
        featureList.size(), //example vector size
        featureList.get(0).size(1), longestTimeSeries };
        int[] labelsShape = new int[] { //# examples
        labelList.size(), //example vector size
        labelList.get(0).size(1), longestTimeSeries };
        featuresOut = Nd4j.create(featuresShape, 'f');
        labelsOut = Nd4j.create(labelsShape, 'f');
        featuresMask = Nd4j.ones(featureList.size(), longestTimeSeries);
        labelsMask = Nd4j.ones(labelList.size(), longestTimeSeries);
        for (int i = 0; i < featureList.size(); i++) {
            INDArray f = featureList.get(i);
            INDArray l = labelList.get(i);
            //Again, permute is to put [timeSeriesLength,vectorSize] into a [vectorSize,timeSeriesLength] matrix
            featuresOut.tensorAlongDimension(i, 1, 2).permutei(1, 0).put(new INDArrayIndex[] { NDArrayIndex.interval(0, f.size(0)), NDArrayIndex.all() }, f);
            labelsOut.tensorAlongDimension(i, 1, 2).permutei(1, 0).put(new INDArrayIndex[] { NDArrayIndex.interval(0, l.size(0)), NDArrayIndex.all() }, l);
            for (int j = f.size(0); j < longestTimeSeries; j++) {
                featuresMask.putScalar(i, j, 0.0);
            }
            for (int j = l.size(0); j < longestTimeSeries; j++) {
                labelsMask.putScalar(i, j, 0.0);
            }
        }
    } else if (alignmentMode == AlignmentMode.ALIGN_END) {
        //Align at end
        int longestTimeSeries = 0;
        for (INDArray features : featureList) {
            longestTimeSeries = Math.max(features.size(0), longestTimeSeries);
        }
        for (INDArray labels : labelList) {
            longestTimeSeries = Math.max(labels.size(0), longestTimeSeries);
        }
        int[] featuresShape = new int[] { //# examples
        featureList.size(), //example vector size
        featureList.get(0).size(1), longestTimeSeries };
        int[] labelsShape = new int[] { //# examples
        labelList.size(), //example vector size
        labelList.get(0).size(1), longestTimeSeries };
        featuresOut = Nd4j.create(featuresShape, 'f');
        labelsOut = Nd4j.create(labelsShape, 'f');
        featuresMask = Nd4j.ones(featureList.size(), longestTimeSeries);
        labelsMask = Nd4j.ones(labelList.size(), longestTimeSeries);
        for (int i = 0; i < featureList.size(); i++) {
            INDArray f = featureList.get(i);
            INDArray l = labelList.get(i);
            int fLen = f.size(0);
            int lLen = l.size(0);
            if (fLen >= lLen) {
                //Align labels with end of features (features are longer)
                featuresOut.tensorAlongDimension(i, 1, 2).permutei(1, 0).put(new INDArrayIndex[] { NDArrayIndex.interval(0, fLen), NDArrayIndex.all() }, f);
                labelsOut.tensorAlongDimension(i, 1, 2).permutei(1, 0).put(new INDArrayIndex[] { NDArrayIndex.interval(fLen - lLen, fLen), NDArrayIndex.all() }, l);
                for (int j = fLen; j < longestTimeSeries; j++) {
                    featuresMask.putScalar(i, j, 0.0);
                }
                //labels mask: component before labels
                for (int j = 0; j < fLen - lLen; j++) {
                    labelsMask.putScalar(i, j, 0.0);
                }
                //labels mask: component after labels
                for (int j = fLen; j < longestTimeSeries; j++) {
                    labelsMask.putScalar(i, j, 0.0);
                }
            } else {
                //Align features with end of labels (labels are longer)
                featuresOut.tensorAlongDimension(i, 1, 2).permutei(1, 0).put(new INDArrayIndex[] { NDArrayIndex.interval(lLen - fLen, lLen), NDArrayIndex.all() }, f);
                labelsOut.tensorAlongDimension(i, 1, 2).permutei(1, 0).put(new INDArrayIndex[] { NDArrayIndex.interval(0, lLen), NDArrayIndex.all() }, l);
                //features mask: component before features
                for (int j = 0; j < lLen - fLen; j++) {
                    featuresMask.putScalar(i, j, 0.0);
                }
                //features mask: component after features
                for (int j = lLen; j < longestTimeSeries; j++) {
                    featuresMask.putScalar(i, j, 0.0);
                }
                //labels mask
                for (int j = lLen; j < longestTimeSeries; j++) {
                    labelsMask.putScalar(i, j, 0.0);
                }
            }
        }
    } else {
        throw new UnsupportedOperationException("Unknown alignment mode: " + alignmentMode);
    }
    cursor += featureList.size();
    if (inputColumns == -1)
        inputColumns = featuresOut.size(1);
    if (totalOutcomes == -1)
        totalOutcomes = labelsOut.size(1);
    DataSet ds = new DataSet(featuresOut, labelsOut, featuresMask, labelsMask);
    if (collectMetaData) {
        ds.setExampleMetaData(meta);
    }
    if (preProcessor != null)
        preProcessor.preProcess(ds);
    return ds;
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) INDArrayIndex(org.nd4j.linalg.indexing.INDArrayIndex)

Example 2 with INDArrayIndex

use of org.nd4j.linalg.indexing.INDArrayIndex 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 INDArrayIndex

use of org.nd4j.linalg.indexing.INDArrayIndex in project deeplearning4j by deeplearning4j.

the class TimeSeriesUtils method movingAverage.

/**
     * Calculate a moving average given the length
     * @param toAvg the array to average
     * @param n the length of the moving window
     * @return the moving averages for each row
     */
public static INDArray movingAverage(INDArray toAvg, int n) {
    INDArray ret = Nd4j.cumsum(toAvg);
    INDArrayIndex[] ends = new INDArrayIndex[] { NDArrayIndex.interval(n, toAvg.columns()) };
    INDArrayIndex[] begins = new INDArrayIndex[] { NDArrayIndex.interval(0, toAvg.columns() - n, false) };
    INDArrayIndex[] nMinusOne = new INDArrayIndex[] { NDArrayIndex.interval(n - 1, toAvg.columns()) };
    ret.put(ends, ret.get(ends).sub(ret.get(begins)));
    return ret.get(nMinusOne).divi(n);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) INDArrayIndex(org.nd4j.linalg.indexing.INDArrayIndex)

Example 4 with INDArrayIndex

use of org.nd4j.linalg.indexing.INDArrayIndex in project deeplearning4j by deeplearning4j.

the class CnnSentenceDataSetIterator method loadSingleSentence.

/**
     * Generally used post training time to load a single sentence for predictions
     */
public INDArray loadSingleSentence(String sentence) {
    List<String> tokens = tokenizeSentence(sentence);
    int[] featuresShape = new int[] { 1, 1, 0, 0 };
    if (sentencesAlongHeight) {
        featuresShape[2] = Math.min(maxSentenceLength, tokens.size());
        featuresShape[3] = wordVectorSize;
    } else {
        featuresShape[2] = wordVectorSize;
        featuresShape[3] = Math.min(maxSentenceLength, tokens.size());
    }
    INDArray features = Nd4j.create(featuresShape);
    int length = (sentencesAlongHeight ? featuresShape[2] : featuresShape[3]);
    for (int i = 0; i < length; i++) {
        INDArray vector = getVector(tokens.get(i));
        INDArrayIndex[] indices = new INDArrayIndex[4];
        indices[0] = NDArrayIndex.point(0);
        indices[1] = NDArrayIndex.point(0);
        if (sentencesAlongHeight) {
            indices[2] = NDArrayIndex.point(i);
            indices[3] = NDArrayIndex.all();
        } else {
            indices[2] = NDArrayIndex.all();
            indices[3] = NDArrayIndex.point(i);
        }
        features.put(indices, vector);
    }
    return features;
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) INDArrayIndex(org.nd4j.linalg.indexing.INDArrayIndex)

Example 5 with INDArrayIndex

use of org.nd4j.linalg.indexing.INDArrayIndex in project deeplearning4j by deeplearning4j.

the class TestVariableLengthTS method testMaskingBidirectionalRnn.

@Test
public void testMaskingBidirectionalRnn() {
    //Idea: mask some of the time steps, like [1,1,1,0,0]. We expect the activations for the first 3 time steps
    // to be the same as if we'd just fed in [1,1,1] for that example
    Nd4j.getRandom().setSeed(12345);
    int nIn = 4;
    int layerSize = 3;
    int nOut = 3;
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).activation(Activation.TANH).list().layer(0, new GravesBidirectionalLSTM.Builder().nIn(nIn).nOut(layerSize).build()).layer(1, new GravesBidirectionalLSTM.Builder().nIn(layerSize).nOut(layerSize).build()).layer(2, new RnnOutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MSE).nIn(layerSize).nOut(nOut).build()).build();
    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();
    int tsLength = 5;
    int minibatch = 3;
    INDArray input = Nd4j.rand(new int[] { minibatch, nIn, tsLength });
    INDArray labels = Nd4j.rand(new int[] { minibatch, nOut, tsLength });
    INDArray featuresMask = Nd4j.create(new double[][] { { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 0 }, { 1, 1, 1, 0, 0 } });
    INDArray labelsMask = featuresMask.dup();
    net.setLayerMaskArrays(featuresMask, labelsMask);
    INDArray outMasked = net.output(input);
    net.clearLayerMaskArrays();
    //Check forward pass:
    for (int i = 0; i < minibatch; i++) {
        INDArrayIndex[] idx = new INDArrayIndex[] { NDArrayIndex.interval(i, i, true), NDArrayIndex.all(), NDArrayIndex.interval(0, tsLength - i) };
        INDArray expExampleOut = net.output(input.get(idx));
        INDArray actualExampleOut = outMasked.get(idx);
        //            System.out.println(i);
        assertEquals(expExampleOut, actualExampleOut);
    }
    //Also: check the score examples method...
    DataSet ds = new DataSet(input, labels, featuresMask, labelsMask);
    INDArray exampleScores = net.scoreExamples(ds, false);
    //One score per time series (added over each time step)
    assertArrayEquals(new int[] { minibatch, 1 }, exampleScores.shape());
    for (int i = 0; i < minibatch; i++) {
        INDArrayIndex[] idx = new INDArrayIndex[] { NDArrayIndex.interval(i, i, true), NDArrayIndex.all(), NDArrayIndex.interval(0, tsLength - i) };
        DataSet dsSingle = new DataSet(input.get(idx), labels.get(idx));
        INDArray exampleSingleScore = net.scoreExamples(dsSingle, false);
        double exp = exampleSingleScore.getDouble(0);
        double act = exampleScores.getDouble(i);
        //            System.out.println(i + "\t" + exp + "\t" + act);
        assertEquals(exp, act, 1e-6);
    }
}
Also used : DataSet(org.nd4j.linalg.dataset.DataSet) INDArrayIndex(org.nd4j.linalg.indexing.INDArrayIndex) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Aggregations

INDArray (org.nd4j.linalg.api.ndarray.INDArray)11 INDArrayIndex (org.nd4j.linalg.indexing.INDArrayIndex)11 DataSet (org.nd4j.linalg.dataset.DataSet)4 FourierLinearOperatorNDArray (org.broadinstitute.hellbender.tools.coveragemodel.linalg.FourierLinearOperatorNDArray)2 Pair (org.deeplearning4j.berkeley.Pair)2 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)2 Test (org.junit.Test)2 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)1 DefaultGradient (org.deeplearning4j.nn.gradient.DefaultGradient)1 Gradient (org.deeplearning4j.nn.gradient.Gradient)1 SpecifiedIndex (org.nd4j.linalg.indexing.SpecifiedIndex)1 Value (org.nd4j.linalg.indexing.functions.Value)1