use of org.deeplearning4j.exception.DL4JException in project deeplearning4j by deeplearning4j.
the class TestInvalidInput method testInputNinMismatchOutputLayer.
@Test
public void testInputNinMismatchOutputLayer() {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().list().layer(0, new DenseLayer.Builder().nIn(10).nOut(20).build()).layer(1, new OutputLayer.Builder().nIn(10).nOut(10).build()).build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
try {
net.feedForward(Nd4j.create(1, 10));
fail("Expected DL4JException");
} catch (DL4JException e) {
System.out.println("testInputNinMismatchOutputLayer(): " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
fail("Expected DL4JException");
}
}
use of org.deeplearning4j.exception.DL4JException in project deeplearning4j by deeplearning4j.
the class TestInvalidInput method testInputNinRank2Subsampling.
@Test
public void testInputNinRank2Subsampling() {
//Rank 2 input, instead of rank 4 input. For example, using the wrong input type
int h = 16;
int w = 16;
int d = 3;
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().list().layer(0, new SubsamplingLayer.Builder().kernelSize(2, 2).build()).layer(1, new OutputLayer.Builder().nOut(10).build()).setInputType(InputType.convolutional(h, w, d)).build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
try {
net.feedForward(Nd4j.create(1, 5 * h * w));
fail("Expected DL4JException");
} catch (DL4JException e) {
System.out.println("testInputNinRank2Subsampling(): " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
fail("Expected DL4JException");
}
}
use of org.deeplearning4j.exception.DL4JException in project deeplearning4j by deeplearning4j.
the class TestConvolutionModes method testConvolutionModeInputTypes.
@Test
public void testConvolutionModeInputTypes() {
//Test 1: input 3x3, stride 1, kernel 2
int inH = 3;
int inW = 3;
int kH = 2;
int kW = 2;
int sH = 1;
int sW = 1;
int pH = 0;
int pW = 0;
int minibatch = 3;
int dIn = 5;
int dOut = 7;
int[] kernel = { kH, kW };
int[] stride = { sH, sW };
int[] padding = { pH, pW };
INDArray inData = Nd4j.create(minibatch, dIn, inH, inW);
InputType inputType = InputType.convolutional(inH, inW, dIn);
//Strict mode: expect 2x2 out -> (inH - kernel + 2*padding)/stride + 1 = (3-2+0)/1+1 = 2
InputType.InputTypeConvolutional it = (InputType.InputTypeConvolutional) InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, padding, ConvolutionMode.Strict, dOut, -1, "layerName", ConvolutionLayer.class);
assertEquals(2, it.getHeight());
assertEquals(2, it.getWidth());
assertEquals(dOut, it.getDepth());
int[] outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, padding, ConvolutionMode.Strict);
assertEquals(2, outSize[0]);
assertEquals(2, outSize[1]);
//Truncate: same as strict here
it = (InputType.InputTypeConvolutional) InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, padding, ConvolutionMode.Truncate, dOut, -1, "layerName", ConvolutionLayer.class);
assertEquals(2, it.getHeight());
assertEquals(2, it.getWidth());
assertEquals(dOut, it.getDepth());
outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, padding, ConvolutionMode.Truncate);
assertEquals(2, outSize[0]);
assertEquals(2, outSize[1]);
//Same mode: ceil(in / stride) = 3
it = (InputType.InputTypeConvolutional) InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, null, ConvolutionMode.Same, dOut, -1, "layerName", ConvolutionLayer.class);
assertEquals(3, it.getHeight());
assertEquals(3, it.getWidth());
assertEquals(dOut, it.getDepth());
outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, null, ConvolutionMode.Same);
assertEquals(3, outSize[0]);
assertEquals(3, outSize[1]);
//Test 2: input 3x4, stride 2, kernel 3
inH = 3;
inW = 4;
kH = 3;
kW = 3;
sH = 2;
sW = 2;
kernel = new int[] { kH, kW };
stride = new int[] { sH, sW };
padding = new int[] { pH, pW };
inData = Nd4j.create(minibatch, dIn, inH, inW);
inputType = InputType.convolutional(inH, inW, dIn);
//Strict mode: (4-3+0)/2+1 is not an integer -> exception
try {
InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, padding, ConvolutionMode.Strict, dOut, -1, "layerName", ConvolutionLayer.class);
fail("Expected exception");
} catch (DL4JException e) {
System.out.println(e.getMessage());
}
try {
outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, padding, ConvolutionMode.Strict);
fail("Exception expected");
} catch (DL4JException e) {
System.out.println(e.getMessage());
}
//Truncate: (3-3+0)/2+1 = 1 in height dim; (4-3+0)/2+1 = 1 in width dim
it = (InputType.InputTypeConvolutional) InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, padding, ConvolutionMode.Truncate, dOut, -1, "layerName", ConvolutionLayer.class);
assertEquals(1, it.getHeight());
assertEquals(1, it.getWidth());
assertEquals(dOut, it.getDepth());
outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, padding, ConvolutionMode.Truncate);
assertEquals(1, outSize[0]);
assertEquals(1, outSize[1]);
//Same mode: ceil(3/2) = 2 in height dim; ceil(4/2) = 2 in width dimension
it = (InputType.InputTypeConvolutional) InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, null, ConvolutionMode.Same, dOut, -1, "layerName", ConvolutionLayer.class);
assertEquals(2, it.getHeight());
assertEquals(2, it.getWidth());
assertEquals(dOut, it.getDepth());
outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, null, ConvolutionMode.Same);
assertEquals(2, outSize[0]);
assertEquals(2, outSize[1]);
}
use of org.deeplearning4j.exception.DL4JException in project deeplearning4j by deeplearning4j.
the class TestConvolutionModes method testStrictTruncateConvolutionModeOutput.
@Test
public void testStrictTruncateConvolutionModeOutput() {
//Idea: with convolution mode == Truncate, input size shouldn't matter (within the bounds of truncated edge),
// and edge data shouldn't affect the output
//Use: 9x9, kernel 3, stride 3, padding 0
// Should get same output for 10x10 and 11x11...
Nd4j.getRandom().setSeed(12345);
int[] minibatches = { 1, 3 };
int[] inDepths = { 1, 3 };
int[] inSizes = { 9, 10, 11 };
for (boolean isSubsampling : new boolean[] { false, true }) {
for (int minibatch : minibatches) {
for (int inDepth : inDepths) {
INDArray origData = Nd4j.rand(new int[] { minibatch, inDepth, 9, 9 });
for (int inSize : inSizes) {
for (ConvolutionMode cm : new ConvolutionMode[] { ConvolutionMode.Strict, ConvolutionMode.Truncate }) {
INDArray inputData = Nd4j.rand(new int[] { minibatch, inDepth, inSize, inSize });
inputData.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(0, 9), NDArrayIndex.interval(0, 9)).assign(origData);
Layer layer;
if (isSubsampling) {
layer = new SubsamplingLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).build();
} else {
layer = new ConvolutionLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).nOut(3).build();
}
MultiLayerNetwork net = null;
try {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).convolutionMode(cm).list().layer(0, layer).layer(1, new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).nOut(3).build()).setInputType(InputType.convolutional(inSize, inSize, inDepth)).build();
net = new MultiLayerNetwork(conf);
net.init();
if (inSize > 9 && cm == ConvolutionMode.Strict) {
fail("Expected exception");
}
} catch (DL4JException e) {
if (inSize == 9 || cm != ConvolutionMode.Strict) {
e.printStackTrace();
fail("Unexpected exception");
}
//Expected exception
continue;
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception");
}
INDArray out = net.output(origData);
INDArray out2 = net.output(inputData);
assertEquals(out, out2);
}
}
}
}
}
}
use of org.deeplearning4j.exception.DL4JException in project deeplearning4j by deeplearning4j.
the class TestRecordReaders method testClassIndexOutsideOfRangeRRMDSI.
@Test
public void testClassIndexOutsideOfRangeRRMDSI() {
Collection<Collection<Collection<Writable>>> c = new ArrayList<>();
Collection<Collection<Writable>> seq1 = new ArrayList<>();
seq1.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(0)));
seq1.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(1)));
c.add(seq1);
Collection<Collection<Writable>> seq2 = new ArrayList<>();
seq2.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(0)));
seq2.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(2)));
c.add(seq2);
CollectionSequenceRecordReader csrr = new CollectionSequenceRecordReader(c);
DataSetIterator dsi = new SequenceRecordReaderDataSetIterator(csrr, 2, 2, 1);
try {
DataSet ds = dsi.next();
fail("Expected exception");
} catch (DL4JException e) {
System.out.println("testClassIndexOutsideOfRangeRRMDSI(): " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
Aggregations