use of org.nd4j.linalg.api.ndarray.INDArray in project deeplearning4j by deeplearning4j.
the class ConvolutionLayerTest method getInput.
private static INDArray getInput() {
/*
----- Input images -----
example 0:
depth 0 depth 1
[ 0 1 2 [ 9 10 11
3 4 5 12 13 14
6 7 8] 15 16 17]
example 1:
[18 19 20 [27 28 29
21 22 23 30 31 32
24 25 26] 33 34 35]
*/
INDArray input = Nd4j.create(new int[] { miniBatch, inDepth, height, width }, 'c');
input.put(new INDArrayIndex[] { NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all() }, Nd4j.create(new double[][] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } }));
input.put(new INDArrayIndex[] { NDArrayIndex.point(0), NDArrayIndex.point(1), NDArrayIndex.all(), NDArrayIndex.all() }, Nd4j.create(new double[][] { { 9, 10, 11 }, { 12, 13, 14 }, { 15, 16, 17 } }));
input.put(new INDArrayIndex[] { NDArrayIndex.point(1), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all() }, Nd4j.create(new double[][] { { 18, 19, 20 }, { 21, 22, 23 }, { 24, 25, 26 } }));
input.put(new INDArrayIndex[] { NDArrayIndex.point(1), NDArrayIndex.point(1), NDArrayIndex.all(), NDArrayIndex.all() }, Nd4j.create(new double[][] { { 27, 28, 29 }, { 30, 31, 32 }, { 33, 34, 35 } }));
return input;
}
use of org.nd4j.linalg.api.ndarray.INDArray in project deeplearning4j by deeplearning4j.
the class ConvolutionLayerTest method testWeightReshaping.
@Test
public void testWeightReshaping() {
//Test assumptions of weight reshaping
//Weights: originally c order, shape [outDepth, inDepth, kH, kw]
//permute (3,2,1,0)
int depthOut = 2;
int depthIn = 3;
int kH = 2;
int kW = 2;
/*
----- Weights -----
- dOut 0 -
dIn 0 dIn 1 dIn 2
[ 0 1 [ 4 5 [ 8 9
2 3] 6 7] 10 11]
- dOut 1 -
[12 13 [16 17 [20 21
14 15] 18 19] 22 23]
*/
INDArray weightOrig = Nd4j.create(new int[] { depthOut, depthIn, kH, kW }, 'c');
weightOrig.put(new INDArrayIndex[] { NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all() }, Nd4j.create(new double[][] { { 0, 1 }, { 2, 3 } }));
weightOrig.put(new INDArrayIndex[] { NDArrayIndex.point(0), NDArrayIndex.point(1), NDArrayIndex.all(), NDArrayIndex.all() }, Nd4j.create(new double[][] { { 4, 5 }, { 6, 7 } }));
weightOrig.put(new INDArrayIndex[] { NDArrayIndex.point(0), NDArrayIndex.point(2), NDArrayIndex.all(), NDArrayIndex.all() }, Nd4j.create(new double[][] { { 8, 9 }, { 10, 11 } }));
weightOrig.put(new INDArrayIndex[] { NDArrayIndex.point(1), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all() }, Nd4j.create(new double[][] { { 12, 13 }, { 14, 15 } }));
weightOrig.put(new INDArrayIndex[] { NDArrayIndex.point(1), NDArrayIndex.point(1), NDArrayIndex.all(), NDArrayIndex.all() }, Nd4j.create(new double[][] { { 16, 17 }, { 18, 19 } }));
weightOrig.put(new INDArrayIndex[] { NDArrayIndex.point(1), NDArrayIndex.point(2), NDArrayIndex.all(), NDArrayIndex.all() }, Nd4j.create(new double[][] { { 20, 21 }, { 22, 23 } }));
INDArray weightPermute = weightOrig.permute(3, 2, 1, 0);
INDArray w2d = Shape.newShapeNoCopy(weightPermute, new int[] { depthIn * kH * kW, depthOut }, true);
assertNotNull(w2d);
//Expected order of weight rows, after reshaping: (kw0,kh0,din0), (kw1,kh0,din0), (kw0,kh1,din0), (kw1,kh1,din0), (kw0,kh0,din1), ...
INDArray wExp = Nd4j.create(new double[][] { { 0, 12 }, { 1, 13 }, { 2, 14 }, { 3, 15 }, { 4, 16 }, { 5, 17 }, { 6, 18 }, { 7, 19 }, { 8, 20 }, { 9, 21 }, { 10, 22 }, { 11, 23 } });
assertEquals(wExp, w2d);
}
use of org.nd4j.linalg.api.ndarray.INDArray in project deeplearning4j by deeplearning4j.
the class ConvolutionLayerTest method testCnnIm2ColReshaping.
@Test
public void testCnnIm2ColReshaping() {
//This test: a bit unusual in that it tests the *assumptions* of the CNN implementation rather than the implementation itself
//Specifically, it tests the row and column orders after reshaping on im2col is reshaped (both forward and backward pass)
INDArray input = getInput();
//im2col in the required order: want [outW,outH,miniBatch,depthIn,kH,kW], but need to input [miniBatch,depth,kH,kW,outH,outW]
// given the current im2col implementation
//To get this: create an array of the order we want, permute it to the order required by im2col implementation, and then do im2col on that
//to get old order from required order: permute(2,3,4,5,1,2)
INDArray col = Nd4j.create(new int[] { miniBatch, outH, outW, inDepth, kH, kW }, 'c');
INDArray col2 = col.permute(0, 3, 4, 5, 1, 2);
Convolution.im2col(input, kH, kW, strides[0], strides[1], pad[0], pad[1], false, col2);
/*
Expected Output, im2col
- example 0 -
depth 0 depth 1
h0,w0 h0,w1 h0,w0 h0,w1
0 1 1 2 9 10 10 11
3 4 4 5 12 13 13 14
h1,w0 h1,w1 h1,w0 h1,w1
3 4 4 5 12 13 13 14
6 7 7 8 15 16 16 17
- example 1 -
depth 0 depth 1
h0,w0 h0,w1 h0,w0 h0,w1
18 19 19 20 27 28 28 29
21 22 22 23 30 31 31 32
h1,w0 h1,w1 h1,w0 h1,w1
21 22 22 23 30 31 31 32
24 25 25 26 33 34 34 35
*/
//Now, after reshaping im2col to 2d, we expect:
//Rows with order (wOut0,hOut0,mb0), (wOut1,hOut0,mb0), (wOut0,hOut1,mb0), (wOut1,hOut1,mb0), (wOut0,hOut0,mb1), ...
//Columns with order (d0,kh0,kw0), (d0,kh0,kw1), (d0,kh1,kw0), (d0,kh1,kw1), (d1,kh0,kw0), ...
INDArray reshapedCol = Shape.newShapeNoCopy(col, new int[] { miniBatch * outH * outW, inDepth * kH * kW }, false);
INDArray exp2d = Nd4j.create(outW * outH * miniBatch, inDepth * kH * kW);
//wOut0,hOut0,mb0 -> both depths, in order (d0,kh0,kw0), (d0,kh0,kw1), (d0,kh1,kw0), (d0,kh1,kw1), (d1,kh0,kw0), (d1,kh0,kw1), (d1,kh1,kw0), (d1,kh1,kw1)
exp2d.putRow(0, Nd4j.create(new double[] { 0, 1, 3, 4, 9, 10, 12, 13 }));
//wOut1,hOut0,mb0
exp2d.putRow(1, Nd4j.create(new double[] { 1, 2, 4, 5, 10, 11, 13, 14 }));
//wOut0,hOut1,mb0
exp2d.putRow(2, Nd4j.create(new double[] { 3, 4, 6, 7, 12, 13, 15, 16 }));
//wOut1,hOut1,mb0
exp2d.putRow(3, Nd4j.create(new double[] { 4, 5, 7, 8, 13, 14, 16, 17 }));
//wOut0,hOut0,mb1
exp2d.putRow(4, Nd4j.create(new double[] { 18, 19, 21, 22, 27, 28, 30, 31 }));
//wOut1,hOut0,mb1
exp2d.putRow(5, Nd4j.create(new double[] { 19, 20, 22, 23, 28, 29, 31, 32 }));
//wOut0,hOut1,mb1
exp2d.putRow(6, Nd4j.create(new double[] { 21, 22, 24, 25, 30, 31, 33, 34 }));
//wOut1,hOut1,mb1
exp2d.putRow(7, Nd4j.create(new double[] { 22, 23, 25, 26, 31, 32, 34, 35 }));
assertEquals(exp2d, reshapedCol);
//Check the same thing for the backprop im2col (different order)
INDArray colBackprop = Nd4j.create(new int[] { miniBatch, outH, outW, inDepth, kH, kW }, 'c');
INDArray colBackprop2 = colBackprop.permute(0, 3, 4, 5, 1, 2);
Convolution.im2col(input, kH, kW, strides[0], strides[1], pad[0], pad[1], false, colBackprop2);
INDArray reshapedColBackprop = Shape.newShapeNoCopy(colBackprop, new int[] { miniBatch * outH * outW, inDepth * kH * kW }, false);
//Rows with order (mb0,h0,w0), (mb0,h0,w1), (mb0,h1,w0), (mb0,h1,w1), (mb1,h0,w0), (mb1,h0,w1), (mb1,h1,w0), (mb1,h1,w1)
//Columns with order (d0,kh0,kw0), (d0,kh0,kw1), (d0,kh1,kw0), (d0,kh1,kw1), (d1,kh0,kw0), ...
INDArray exp2dv2 = Nd4j.create(outW * outH * miniBatch, inDepth * kH * kW);
//wOut0,hOut0,mb0 -> both depths, in order (d0,kh0,kw0), (d0,kh0,kw1), (d0,kh1,kw0), (d0,kh1,kw1), (d1,kh0,kw0), (d1,kh0,kw1), (d1,kh1,kw0), (d1,kh1,kw1)
exp2dv2.putRow(0, Nd4j.create(new double[] { 0, 1, 3, 4, 9, 10, 12, 13 }));
//wOut1,hOut0,mb0
exp2dv2.putRow(1, Nd4j.create(new double[] { 1, 2, 4, 5, 10, 11, 13, 14 }));
//wOut0,hOut1,mb0
exp2dv2.putRow(2, Nd4j.create(new double[] { 3, 4, 6, 7, 12, 13, 15, 16 }));
//wOut1,hOut1,mb0
exp2dv2.putRow(3, Nd4j.create(new double[] { 4, 5, 7, 8, 13, 14, 16, 17 }));
//wOut0,hOut0,mb1
exp2dv2.putRow(4, Nd4j.create(new double[] { 18, 19, 21, 22, 27, 28, 30, 31 }));
//wOut1,hOut0,mb1
exp2dv2.putRow(5, Nd4j.create(new double[] { 19, 20, 22, 23, 28, 29, 31, 32 }));
//wOut0,hOut1,mb1
exp2dv2.putRow(6, Nd4j.create(new double[] { 21, 22, 24, 25, 30, 31, 33, 34 }));
//wOut1,hOut1,mb1
exp2dv2.putRow(7, Nd4j.create(new double[] { 22, 23, 25, 26, 31, 32, 34, 35 }));
assertEquals(exp2dv2, reshapedColBackprop);
}
use of org.nd4j.linalg.api.ndarray.INDArray in project deeplearning4j by deeplearning4j.
the class ConvolutionLayerTest method testGetSetParams.
@Test
public void testGetSetParams() {
MultiLayerNetwork net = getCNNMLNConfig(true, false);
INDArray paramsOrig = net.params().dup();
net.setParams(paramsOrig);
INDArray params2 = net.params();
assertEquals(paramsOrig, params2);
}
use of org.nd4j.linalg.api.ndarray.INDArray in project deeplearning4j by deeplearning4j.
the class ConvolutionLayerTest method testCNNZeroStride.
@Test(expected = Exception.class)
public void testCNNZeroStride() {
int imageHeight = 20;
int imageWidth = 23;
int nChannels = 1;
int classes = 2;
int numSamples = 200;
int kernelHeight = imageHeight;
int kernelWidth = imageWidth;
DataSet trainInput;
MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().seed(123).iterations(1).list().layer(0, new ConvolutionLayer.Builder(kernelHeight, kernelWidth).stride(1, 0).nOut(2).activation(Activation.RELU).weightInit(WeightInit.XAVIER).build()).layer(1, new OutputLayer.Builder().nOut(classes).weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).build()).backprop(true).pretrain(false);
new ConvolutionLayerSetup(builder, imageHeight, imageWidth, nChannels);
MultiLayerConfiguration conf = builder.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
INDArray emptyFeatures = Nd4j.zeros(numSamples, imageWidth * imageHeight * nChannels);
INDArray emptyLables = Nd4j.zeros(numSamples, classes);
trainInput = new DataSet(emptyFeatures, emptyLables);
model.fit(trainInput);
}
Aggregations