use of org.nd4j.linalg.indexing.INDArrayIndex in project deeplearning4j by deeplearning4j.
the class LocalResponseNormalization method backpropGradient.
public Pair<Gradient, INDArray> backpropGradient(INDArray epsilon) {
if (helper != null) {
Pair<Gradient, INDArray> ret = helper.backpropGradient(input, epsilon, k, n, alpha, beta);
if (ret != null) {
return ret;
}
}
int channel = input.size(1);
INDArray tmp, addVal;
Gradient retGradient = new DefaultGradient();
INDArray reverse = activations.mul(epsilon);
INDArray sumPart = reverse.dup();
// sumPart = sum(a^j_{x,y} * gb^j_{x,y})
for (int i = 1; i < halfN + 1; i++) {
tmp = sumPart.get(new INDArrayIndex[] { NDArrayIndex.all(), interval(i, channel), NDArrayIndex.all(), NDArrayIndex.all() });
addVal = reverse.get(new INDArrayIndex[] { NDArrayIndex.all(), interval(0, channel - i), NDArrayIndex.all(), NDArrayIndex.all() });
sumPart.put(new INDArrayIndex[] { NDArrayIndex.all(), interval(i, channel), NDArrayIndex.all(), NDArrayIndex.all() }, tmp.addi(addVal));
tmp = sumPart.get(new INDArrayIndex[] { NDArrayIndex.all(), interval(0, channel - i), NDArrayIndex.all(), NDArrayIndex.all() });
addVal = reverse.get(new INDArrayIndex[] { NDArrayIndex.all(), interval(i, channel), NDArrayIndex.all(), NDArrayIndex.all() });
sumPart.put(new INDArrayIndex[] { NDArrayIndex.all(), interval(0, channel - i), NDArrayIndex.all(), NDArrayIndex.all() }, tmp.addi(addVal));
}
// gx = gy * unitScale**-beta - 2 * alpha * beta * sumPart/unitScale * a^i_{x,y} - rearranged for more in-place ops
INDArray nextEpsilon = epsilon.mul(scale).subi(sumPart.muli(input).divi(unitScale).muli(2 * alpha * beta));
return new Pair<>(retGradient, nextEpsilon);
}
use of org.nd4j.linalg.indexing.INDArrayIndex in project deeplearning4j by deeplearning4j.
the class StackVertex method doForward.
@Override
public INDArray doForward(boolean training) {
// stacking along dimension 0
// inputs[] is an array of INDArray (e.g.: shape of 3 x [nExamples, nSize])
// what we want to do is make a stacked output (e.g.: [3 x nExamples, nSize])
int nStack = inputs.length;
int[] inShape = inputs[0].shape();
int[] outShape = new int[inShape.length];
// create the new shape
for (int i = 0; i < inShape.length; i++) {
if (i == 0)
outShape[0] = nStack * inShape[0];
else
outShape[i] = inShape[i];
}
INDArray out = Nd4j.create(outShape);
//Simplest case: no masking arrays, all same length
// loop through indexes for 2D, 3D, 4D...
INDArrayIndex[] indexes = new INDArrayIndex[inShape.length];
for (int i = 0; i < inShape.length; i++) {
indexes[i] = NDArrayIndex.all();
}
int rowCount = 0;
for (INDArray input : inputs) {
int nEx = input.size(0);
indexes[0] = NDArrayIndex.interval(rowCount, rowCount + nEx);
out.put(indexes, input);
rowCount += nEx;
}
return out;
}
use of org.nd4j.linalg.indexing.INDArrayIndex in project deeplearning4j by deeplearning4j.
the class TestVariableLengthTS method testMaskingLstmAndBidirectionalLstmGlobalPooling.
@Test
public void testMaskingLstmAndBidirectionalLstmGlobalPooling() {
//Idea: mask some of the time steps, like [1,1,1,0,0]. We expect the activations out of the global pooling
// to be the same as if we'd just fed in the in the present (1s) time steps only
Nd4j.getRandom().setSeed(12345);
int nIn = 2;
int layerSize = 4;
int nOut = 3;
PoolingType[] poolingTypes = new PoolingType[] { PoolingType.SUM, PoolingType.AVG, PoolingType.MAX };
boolean[] isBidirectional = new boolean[] { false, true };
for (boolean bidirectional : isBidirectional) {
for (PoolingType pt : poolingTypes) {
System.out.println("Starting test: bidirectional = " + bidirectional + ", poolingType = " + pt);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).activation(Activation.TANH).list().layer(0, bidirectional ? new GravesBidirectionalLSTM.Builder().nIn(nIn).nOut(layerSize).build() : new GravesLSTM.Builder().nIn(nIn).nOut(layerSize).build()).layer(1, bidirectional ? new GravesBidirectionalLSTM.Builder().nIn(layerSize).nOut(layerSize).build() : new GravesLSTM.Builder().nIn(layerSize).nOut(layerSize).build()).layer(2, new GlobalPoolingLayer.Builder().poolingType(pt).build()).layer(3, new OutputLayer.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 });
INDArray featuresMask = Nd4j.create(new double[][] { { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 0 }, { 1, 1, 1, 0, 0 } });
net.setLayerMaskArrays(featuresMask, null);
INDArray outMasked = net.output(input);
net.clearLayerMaskArrays();
for (int i = 0; i < minibatch; i++) {
INDArrayIndex[] idx = new INDArrayIndex[] { NDArrayIndex.interval(i, i, true), NDArrayIndex.all(), NDArrayIndex.interval(0, tsLength - i) };
INDArray inputSubset = input.get(idx);
INDArray expExampleOut = net.output(inputSubset);
INDArray actualExampleOut = outMasked.getRow(i);
// System.out.println(i);
assertEquals(expExampleOut, actualExampleOut);
}
//Also: check the score examples method...
DataSet ds = new DataSet(input, labels, featuresMask, null);
INDArray exampleScores = net.scoreExamples(ds, false);
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.getRow(i));
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);
}
}
}
}
use of org.nd4j.linalg.indexing.INDArrayIndex in project deeplearning4j by deeplearning4j.
the class CnnSentenceDataSetIterator method next.
@Override
public DataSet next(int num) {
if (sentenceProvider == null) {
throw new UnsupportedOperationException("Cannot do next/hasNext without a sentence provider");
}
List<Pair<List<String>, String>> tokenizedSentences = new ArrayList<>(num);
int maxLength = -1;
//Track to we know if we can skip mask creation for "all same length" case
int minLength = Integer.MAX_VALUE;
for (int i = 0; i < num && sentenceProvider.hasNext(); i++) {
Pair<String, String> p = sentenceProvider.nextSentence();
List<String> tokens = tokenizeSentence(p.getFirst());
maxLength = Math.max(maxLength, tokens.size());
tokenizedSentences.add(new Pair<>(tokens, p.getSecond()));
}
if (maxSentenceLength > 0 && maxLength > maxSentenceLength) {
maxLength = maxSentenceLength;
}
int currMinibatchSize = tokenizedSentences.size();
INDArray labels = Nd4j.create(currMinibatchSize, numClasses);
for (int i = 0; i < tokenizedSentences.size(); i++) {
String labelStr = tokenizedSentences.get(i).getSecond();
if (!labelClassMap.containsKey(labelStr)) {
throw new IllegalStateException("Got label \"" + labelStr + "\" that is not present in list of LabeledSentenceProvider labels");
}
int labelIdx = labelClassMap.get(labelStr);
labels.putScalar(i, labelIdx, 1.0);
}
int[] featuresShape = new int[4];
featuresShape[0] = currMinibatchSize;
featuresShape[1] = 1;
if (sentencesAlongHeight) {
featuresShape[2] = maxLength;
featuresShape[3] = wordVectorSize;
} else {
featuresShape[2] = wordVectorSize;
featuresShape[3] = maxLength;
}
INDArray features = Nd4j.create(featuresShape);
for (int i = 0; i < currMinibatchSize; i++) {
List<String> currSentence = tokenizedSentences.get(i).getFirst();
for (int j = 0; j < currSentence.size() && j < maxSentenceLength; j++) {
INDArray vector = getVector(currSentence.get(j));
INDArrayIndex[] indices = new INDArrayIndex[4];
//TODO REUSE
indices[0] = NDArrayIndex.point(i);
indices[1] = NDArrayIndex.point(0);
if (sentencesAlongHeight) {
indices[2] = NDArrayIndex.point(j);
indices[3] = NDArrayIndex.all();
} else {
indices[2] = NDArrayIndex.all();
indices[3] = NDArrayIndex.point(j);
}
features.put(indices, vector);
}
}
INDArray featuresMask = null;
if (minLength != maxLength) {
featuresMask = Nd4j.create(currMinibatchSize, maxLength);
for (int i = 0; i < currMinibatchSize; i++) {
int sentenceLength = tokenizedSentences.get(i).getFirst().size();
if (sentenceLength >= maxLength) {
featuresMask.getRow(i).assign(1.0);
} else {
featuresMask.get(NDArrayIndex.point(i), NDArrayIndex.interval(0, sentenceLength)).assign(1.0);
}
}
}
DataSet ds = new DataSet(features, labels, featuresMask, null);
if (dataSetPreProcessor != null) {
dataSetPreProcessor.preProcess(ds);
}
cursor += ds.numExamples();
return ds;
}
use of org.nd4j.linalg.indexing.INDArrayIndex in project gatk-protected by broadinstitute.
the class CoverageModelEMWorkspace method updateFilteredBiasCovariates.
/**
* This method applies the Fourier filter on a given bias covariates matrix, applies the Fourier filter on it,
* partitions the result, and pushes it to compute block(s)
*
* @param biasCovariates any T x D bias covariates matrix
*/
@UpdatesRDD
private void updateFilteredBiasCovariates(@Nonnull final INDArray biasCovariates) {
final INDArray filteredBiasCovariates = Nd4j.create(biasCovariates.shape());
/* instantiate the Fourier filter */
final FourierLinearOperatorNDArray regularizerFourierLinearOperator = createRegularizerFourierLinearOperator();
/* FFT by resolving W_tl on l */
for (int li = 0; li < numLatents; li++) {
final INDArrayIndex[] slice = { NDArrayIndex.all(), NDArrayIndex.point(li) };
filteredBiasCovariates.get(slice).assign(regularizerFourierLinearOperator.operate(biasCovariates.get(slice)));
}
/* sent the new W to workers */
switch(config.getBiasCovariatesComputeNodeCommunicationPolicy()) {
case BROADCAST_HASH_JOIN:
pushToWorkers(mapINDArrayToBlocks(filteredBiasCovariates), (W, cb) -> cb.cloneWithUpdatedPrimitive(CoverageModelEMComputeBlock.CoverageModelICGCacheNode.F_W_tl, W.get(cb.getTargetSpaceBlock())));
break;
case RDD_JOIN:
joinWithWorkersAndMap(chopINDArrayToBlocks(filteredBiasCovariates), p -> p._1.cloneWithUpdatedPrimitive(CoverageModelEMComputeBlock.CoverageModelICGCacheNode.F_W_tl, p._2));
break;
}
}
Aggregations