use of org.deeplearning4j.nn.layers.feedforward.dense.DenseLayer in project deeplearning4j by deeplearning4j.
the class TestPreProcessors method testRnnToFeedForwardPreProcessor.
@Test
public void testRnnToFeedForwardPreProcessor() {
int[] miniBatchSizes = { 5, 1, 5, 1 };
int[] timeSeriesLengths = { 9, 9, 1, 1 };
for (int x = 0; x < miniBatchSizes.length; x++) {
int miniBatchSize = miniBatchSizes[x];
int layerSize = 7;
int timeSeriesLength = timeSeriesLengths[x];
RnnToFeedForwardPreProcessor proc = new RnnToFeedForwardPreProcessor();
NeuralNetConfiguration nnc = new NeuralNetConfiguration.Builder().layer(new org.deeplearning4j.nn.conf.layers.DenseLayer.Builder().nIn(layerSize).nOut(layerSize).build()).build();
int numParams = nnc.getLayer().initializer().numParams(nnc);
INDArray params = Nd4j.create(1, numParams);
DenseLayer layer = (DenseLayer) nnc.getLayer().instantiate(nnc, null, 0, params, true);
layer.setInputMiniBatchSize(miniBatchSize);
INDArray activations3dc = Nd4j.create(new int[] { miniBatchSize, layerSize, timeSeriesLength }, 'c');
INDArray activations3df = Nd4j.create(new int[] { miniBatchSize, layerSize, timeSeriesLength }, 'f');
for (int i = 0; i < miniBatchSize; i++) {
for (int j = 0; j < layerSize; j++) {
for (int k = 0; k < timeSeriesLength; k++) {
//value abc -> example=a, neuronNumber=b, time=c
double value = 100 * i + 10 * j + k;
activations3dc.putScalar(new int[] { i, j, k }, value);
activations3df.putScalar(new int[] { i, j, k }, value);
}
}
}
assertEquals(activations3dc, activations3df);
INDArray activations2dc = proc.preProcess(activations3dc, miniBatchSize);
INDArray activations2df = proc.preProcess(activations3df, miniBatchSize);
assertArrayEquals(activations2dc.shape(), new int[] { miniBatchSize * timeSeriesLength, layerSize });
assertArrayEquals(activations2df.shape(), new int[] { miniBatchSize * timeSeriesLength, layerSize });
assertEquals(activations2dc, activations2df);
//Expect each row in activations2d to have order:
//(example=0,t=0), (example=0,t=1), (example=0,t=2), ..., (example=1,t=0), (example=1,t=1), ...
int nRows = activations2dc.rows();
for (int i = 0; i < nRows; i++) {
INDArray rowc = activations2dc.getRow(i);
INDArray rowf = activations2df.getRow(i);
assertArrayEquals(rowc.shape(), new int[] { 1, layerSize });
assertEquals(rowc, rowf);
//c order reshaping
// int origExampleNum = i / timeSeriesLength;
// int time = i % timeSeriesLength;
//f order reshaping
int time = i / miniBatchSize;
int origExampleNum = i % miniBatchSize;
INDArray expectedRow = activations3dc.tensorAlongDimension(time, 1, 0).getRow(origExampleNum);
assertEquals(expectedRow, rowc);
assertEquals(expectedRow, rowf);
}
//Given that epsilons and activations have same shape, we can do this (even though it's not the intended use)
//Basically backprop should be exact opposite of preProcess
INDArray outc = proc.backprop(activations2dc, miniBatchSize);
INDArray outf = proc.backprop(activations2df, miniBatchSize);
assertEquals(activations3dc, outc);
assertEquals(activations3df, outf);
//Also check case when epsilons are different orders:
INDArray eps2d_c = Nd4j.create(activations2dc.shape(), 'c');
INDArray eps2d_f = Nd4j.create(activations2dc.shape(), 'f');
eps2d_c.assign(activations2dc);
eps2d_f.assign(activations2df);
INDArray eps3d_c = proc.backprop(eps2d_c, miniBatchSize);
INDArray eps3d_f = proc.backprop(eps2d_f, miniBatchSize);
assertEquals(activations3dc, eps3d_c);
assertEquals(activations3df, eps3d_f);
}
}
use of org.deeplearning4j.nn.layers.feedforward.dense.DenseLayer in project deeplearning4j by deeplearning4j.
the class TestPreProcessors method testFeedForwardToRnnPreProcessor.
@Test
public void testFeedForwardToRnnPreProcessor() {
Nd4j.getRandom().setSeed(12345L);
int[] miniBatchSizes = { 5, 1, 5, 1 };
int[] timeSeriesLengths = { 9, 9, 1, 1 };
for (int x = 0; x < miniBatchSizes.length; x++) {
int miniBatchSize = miniBatchSizes[x];
int layerSize = 7;
int timeSeriesLength = timeSeriesLengths[x];
String msg = "minibatch=" + miniBatchSize;
FeedForwardToRnnPreProcessor proc = new FeedForwardToRnnPreProcessor();
NeuralNetConfiguration nnc = new NeuralNetConfiguration.Builder().layer(new org.deeplearning4j.nn.conf.layers.DenseLayer.Builder().nIn(layerSize).nOut(layerSize).build()).build();
int numParams = nnc.getLayer().initializer().numParams(nnc);
INDArray params = Nd4j.create(1, numParams);
DenseLayer layer = (DenseLayer) nnc.getLayer().instantiate(nnc, null, 0, params, true);
layer.setInputMiniBatchSize(miniBatchSize);
INDArray rand = Nd4j.rand(miniBatchSize * timeSeriesLength, layerSize);
INDArray activations2dc = Nd4j.create(new int[] { miniBatchSize * timeSeriesLength, layerSize }, 'c');
INDArray activations2df = Nd4j.create(new int[] { miniBatchSize * timeSeriesLength, layerSize }, 'f');
activations2dc.assign(rand);
activations2df.assign(rand);
assertEquals(activations2dc, activations2df);
INDArray activations3dc = proc.preProcess(activations2dc, miniBatchSize);
INDArray activations3df = proc.preProcess(activations2df, miniBatchSize);
assertArrayEquals(new int[] { miniBatchSize, layerSize, timeSeriesLength }, activations3dc.shape());
assertArrayEquals(new int[] { miniBatchSize, layerSize, timeSeriesLength }, activations3df.shape());
assertEquals(activations3dc, activations3df);
int nRows2D = miniBatchSize * timeSeriesLength;
for (int i = 0; i < nRows2D; i++) {
//c order reshaping:
// int time = i % timeSeriesLength;
// int example = i / timeSeriesLength;
//f order reshaping
int time = i / miniBatchSize;
int example = i % miniBatchSize;
INDArray row2d = activations2dc.getRow(i);
INDArray row3dc = activations3dc.tensorAlongDimension(time, 0, 1).getRow(example);
INDArray row3df = activations3df.tensorAlongDimension(time, 0, 1).getRow(example);
assertEquals(row2d, row3dc);
assertEquals(row2d, row3df);
}
//Again epsilons and activations have same shape, we can do this (even though it's not the intended use)
INDArray epsilon2d1 = proc.backprop(activations3dc, miniBatchSize);
INDArray epsilon2d2 = proc.backprop(activations3df, miniBatchSize);
assertEquals(msg, activations2dc, epsilon2d1);
assertEquals(msg, activations2dc, epsilon2d2);
//Also check backprop with 3d activations in f order vs. c order:
INDArray act3d_c = Nd4j.create(activations3dc.shape(), 'c');
act3d_c.assign(activations3dc);
INDArray act3d_f = Nd4j.create(activations3dc.shape(), 'f');
act3d_f.assign(activations3dc);
assertEquals(msg, activations2dc, proc.backprop(act3d_c, miniBatchSize));
assertEquals(msg, activations2dc, proc.backprop(act3d_f, miniBatchSize));
}
}
Aggregations