# DL4JInvalidInputException in simple example

**URL:** <https://community.konduit.ai/t/dl4jinvalidinputexception-in-simple-example/1198>\
**Category:** DL4J\
**Created:** [February 24, 2021, 3:03pm UTC](https://community.konduit.ai/t/dl4jinvalidinputexception-in-simple-example/1198 "2021-02-24T15:03:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![braindiver](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@braindiver](https://community.konduit.ai/u/braindiver)\
**Post date:** [February 24, 2021, 3:03pm UTC](https://community.konduit.ai/t/dl4jinvalidinputexception-in-simple-example/1198/1 "2021-02-24T15:03:02Z")

</div>

Hello, I complied the HandWrittenDigitRecognizer from the PacktPub Book “Java Machine Learning for Computer Vision”. I can also train the network with the MNIST data but when I draw I digit and click on “recognize”, I get a DL4JInvalidInputException (Input that is not a matrix) Any Idea what could be wrong?

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 24, 2021, 3:08pm UTC](https://community.konduit.ai/t/dl4jinvalidinputexception-in-simple-example/1198/2 "2021-02-24T15:08:19Z")

</div>

Lots of things could be wrong, so you need to provide us with some more information.

Most people that frequent this forum don’t have the book you are using but even if we had, you still would need to share your code with us, as we need to know what version of dl4j you are using, and what you are actually feeding to your network.

---

<div class="post-metadata">

**Author:** ![braindiver](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@braindiver](https://community.konduit.ai/u/braindiver)\
**Post date:** [February 24, 2021, 6:29pm UTC](https://community.konduit.ai/t/dl4jinvalidinputexception-in-simple-example/1198/3 "2021-02-24T18:29:13Z")

</div>

Hello, I don’t have the book either, I just read the [chapter on recognizing handwritten digits](https://subscription.packtpub.com/book/big-data-and-business-intelligence/9781789613964/1/ch01lvl1sec17/building-a-handwritten-digit-recognizer) online and built the example application from the [github repository](https://github.com/PacktPublishing/Java-Machine-Learning-for-Computer-Vision)

The program has a GUI with button for training a NN with data from MNIST (which seems to work) and a canvas for drawing digits:

 ![eight](https://canada1.discourse-cdn.com/flex035/uploads/konduit/original/1X/1c152b0d9dbbd597203abcadcda18a8679dfd736.png)

When I click on recognize (no matter whether I use the simple NN or the CNN), I get the org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 1 array with shape [784]. Missing preprocessor or wrong input type? (layer name: layer0, layer index: 0, layer type: DenseLayer)

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 24, 2021, 6:43pm UTC](https://community.konduit.ai/t/dl4jinvalidinputexception-in-simple-example/1198/4 "2021-02-24T18:43:15Z")

</div>

Looks like the code there is about 3 years old. Just be aware that it is probably also using a very old version of dl4j, and if you try to update it you might run into incompatibilities.

The error you are getting tells you that the input data is formatted incorrectly.

In particular, DL4J expects that all prediction requests are batched. The code you are using is passing it a vector of 784 elements. It expects a matrix, in your case it would be a 1x784 matrix, because you’ve got just a single example.

To fix this, you need to turn your vector into a matrix.

You can do this with `matrix = Nd4j.expandDims(vector, 0)`.

Or in your particular case:

```java
int[] predict = preTrainedModel.predict(Nd4j.expandDims(Nd4j.create(labeledImage.getNormalizedPixels()), 0));

```

at [Java-Machine-Learning-for-Computer-Vision/DigitRecognizerNeuralNetwork.java at master · PacktPublishing/Java-Machine-Learning-for-Computer-Vision · GitHub](https://github.com/PacktPublishing/Java-Machine-Learning-for-Computer-Vision/blob/master/HandWrittenDigitRecognizer/src/main/java/ramo/klevis/ml/network/neural/DigitRecognizerNeuralNetwork.java#L151)

The same kind of mistake has also been made for the convolutional neural network, where it also looks like it passes the raw pixel vector instead of the appropriately reshaped tensor. The solution there is to reshape it into the proper shape.

---

<div class="post-metadata">

**Author:** ![braindiver](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@braindiver](https://community.konduit.ai/u/braindiver)\
**Post date:** [February 24, 2021, 7:02pm UTC](https://community.konduit.ai/t/dl4jinvalidinputexception-in-simple-example/1198/5 "2021-02-24T19:02:56Z")

</div>

problem solved - thanks a lot!
