Exception CNN neural network with java

I am trying to create a neural network in java using deeplearning4j, but I keep getting an error when trying to create the network.
[main] INFO org.deeplearning4j.nn.multilayer.MultiLayerNetwork - Starting MultiLayerNetwork with WorkspaceModes set to [training: ENABLED; inference: ENABLED], cacheMode set to [NONE]
Exception in thread "main" org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 4 array with shape [3, 16, 3, 3]. Missing preprocessor or wrong input type? (layer name: layer2, layer index: 2, layer type: DenseLayer)
at org.deeplearning4j.nn.layers.BaseLayer.preOutputWithPreNorm(BaseLayer.java:312)
at org.deeplearning4j.nn.layers.BaseLayer.preOutput(BaseLayer.java:295)
at org.deeplearning4j.nn.layers.BaseLayer.activate(BaseLayer.java:343)
at org.deeplearning4j.nn.layers.AbstractLayer.activate(AbstractLayer.java:262)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.ffToLayerActivationsInWs(MultiLayerNetwork.java:1138)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:2783)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:2741)
at org.deeplearning4j.optimize.solvers.BaseOptimizer.gradientAndScore(BaseOptimizer.java:174)
at org.deeplearning4j.optimize.solvers.StochasticGradientDescent.optimize(StochasticGradientDescent.java:61)
at org.deeplearning4j.optimize.Solver.optimize(Solver.java:52)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fitHelper(MultiLayerNetwork.java:1752)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fit(MultiLayerNetwork.java:1673)
at de.RolandHoeckenschnieder.TryTwo.ChessCNN.main(ChessCNN.java:130)
[main] INFO org.deeplearning4j.nn.multilayer.MultiLayerNetwork - Starting MultiLayerNetwork with WorkspaceModes set to [training: ENABLED; inference: ENABLED], cacheMode set to [NONE]
Exception in thread "main" org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 4 array with shape [3, 16, 3, 3]. Missing preprocessor or wrong input type? (layer name: layer2, layer index: 2, layer type: DenseLayer)
at org.deeplearning4j.nn.layers.BaseLayer.preOutputWithPreNorm(BaseLayer.java:312)
at org.deeplearning4j.nn.layers.BaseLayer.preOutput(BaseLayer.java:295)
at org.deeplearning4j.nn.layers.BaseLayer.activate(BaseLayer.java:343)
at org.deeplearning4j.nn.layers.AbstractLayer.activate(AbstractLayer.java:262)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.ffToLayerActivationsInWs(MultiLayerNetwork.java:1138)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:2783)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:2741)
at org.deeplearning4j.optimize.solvers.BaseOptimizer.gradientAndScore(BaseOptimizer.java:174)
at org.deeplearning4j.optimize.solvers.StochasticGradientDescent.optimize(StochasticGradientDescent.java:61)
at org.deeplearning4j.optimize.Solver.optimize(Solver.java:52)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fitHelper(MultiLayerNetwork.java:1752)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fit(MultiLayerNetwork.java:1673)
at de.RolandHoeckenschnieder.TryTwo.ChessCNN.main(ChessCNN.java:130)
25 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @Lloyd_159! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Lloyd_159
Lloyd_159OP2mo ago
This my the code to create the network:
MultiLayerNetwork model = new MultiLayerNetwork(new NeuralNetConfiguration.Builder()
.updater(new Adam(0.001))
.weightInit(WeightInit.XAVIER)
.list()
.layer(new ConvolutionLayer.Builder(3, 3)
.nIn(1) //input 8x8 matrix
.stride(1, 1)
.nOut(16)
.activation(Activation.RELU)
.build())
.layer(new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
.kernelSize(2, 2)
.stride(2, 2)
.build())
.layer(new DenseLayer.Builder()
.nIn(16 * 3 * 3)
.nOut(64)
.activation(Activation.RELU)
.build())
.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MSE)
.nIn(64)
.nOut(1)
.activation(Activation.IDENTITY)
.build())
.build());
MultiLayerNetwork model = new MultiLayerNetwork(new NeuralNetConfiguration.Builder()
.updater(new Adam(0.001))
.weightInit(WeightInit.XAVIER)
.list()
.layer(new ConvolutionLayer.Builder(3, 3)
.nIn(1) //input 8x8 matrix
.stride(1, 1)
.nOut(16)
.activation(Activation.RELU)
.build())
.layer(new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
.kernelSize(2, 2)
.stride(2, 2)
.build())
.layer(new DenseLayer.Builder()
.nIn(16 * 3 * 3)
.nOut(64)
.activation(Activation.RELU)
.build())
.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MSE)
.nIn(64)
.nOut(1)
.activation(Activation.IDENTITY)
.build())
.build());
It looks like the Dense layer is not accepting the output of the SubsamplingLayer, but I don't manage to fix it.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
dan1st
dan1st2mo ago
You might need to flatten the output of the previous layer I think you can add .setInputTypes(InputType.convolutional(3,3,16)) in the dense layer I think with this, you won't need nIn(16*3*3)
Lloyd_159
Lloyd_159OP2mo ago
doesn't exist i think so how can I do that? I tried to look it up and read that I should use a flattenLayer by adding .layer(new org.deeplearning4j.nn.conf.layers.FlattenLayer()) before the denseLayer, but Intellij is not accepting this, I also tried a few other way to use it but no idea how it works...
dan1st
dan1st2mo ago
what happens when doing that? what doesn't exist? Which DL4J version are you using?
Lloyd_159
Lloyd_159OP2mo ago
1.0.0-M1.1 I think that's the newest one it says Cannot resolve symbol 'FlattenLayer' the .setInputTypes() Method
dan1st
dan1st2mo ago
ah it seems like that might not be per layer Can you try .addVertex("flatten",new PreprocessorVertex(new CnnToFeedForwardPreProcessor(3,3,16)),"input") before the dense layer? if that exists I think it's available in 1.0.0-M2.1 Also I think you only need the .nIn() in the first layer oh actually I think setInputType should work
MultiLayerNetwork model = new MultiLayerNetwork(new NeuralNetConfiguration.Builder()
.updater(new Adam(0.001))
.weightInit(WeightInit.XAVIER)
.list()
.layer(new ConvolutionLayer.Builder(3, 3)
.nIn(1) //input 8x8 matrix
.stride(1, 1)
.nOut(16)
.activation(Activation.RELU)
.build())
.layer(new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
.kernelSize(2, 2)
.stride(2, 2)
.build())
.layer(new DenseLayer.Builder()
.nIn(16 * 3 * 3)
.nOut(64)
.activation(Activation.RELU)
.build())
.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MSE)
.nIn(64)
.nOut(1)
.activation(Activation.IDENTITY)
.build())
.setInputType(InputType.convolutional(8,8,1))//8x8 images with one channel
.build());
MultiLayerNetwork model = new MultiLayerNetwork(new NeuralNetConfiguration.Builder()
.updater(new Adam(0.001))
.weightInit(WeightInit.XAVIER)
.list()
.layer(new ConvolutionLayer.Builder(3, 3)
.nIn(1) //input 8x8 matrix
.stride(1, 1)
.nOut(16)
.activation(Activation.RELU)
.build())
.layer(new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
.kernelSize(2, 2)
.stride(2, 2)
.build())
.layer(new DenseLayer.Builder()
.nIn(16 * 3 * 3)
.nOut(64)
.activation(Activation.RELU)
.build())
.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MSE)
.nIn(64)
.nOut(1)
.activation(Activation.IDENTITY)
.build())
.setInputType(InputType.convolutional(8,8,1))//8x8 images with one channel
.build());
and this should tell DL4J to handle that Note: that is if you have the input as a batchSize x channels x 8 x 8 array I think Alternatively, you could use a CnnToFeedForwardPreProcessor
Lloyd_159
Lloyd_159OP2mo ago
I now finally managed to update to 1.0.0-M2.1 but the .addVertex() method still cannot be resolved
dan1st
dan1st2mo ago
.
Lloyd_159
Lloyd_159OP2mo ago
ye doing that rn
dan1st
dan1st2mo ago
You are using a grayscale image, right?
Lloyd_159
Lloyd_159OP2mo ago
tho how do I know if I have the input as batchSize x channels x 8 x 8 array? ._.
dan1st
dan1st2mo ago
How are you providing the input?
Lloyd_159
Lloyd_159OP2mo ago
well kinda ig? but formatted like that
dan1st
dan1st2mo ago
if that's not the case, you'd probably get an exception ? Does your input image have exactly one channel?
Lloyd_159
Lloyd_159OP2mo ago
yes ig it's just a 8x8 matrix as double[8][8]
dan1st
dan1st2mo ago
ok that doesn't have channel information ig just try and see what happens you might need to change it to a double[1][8][8] array but idk - if it doesn't work, you'll notice it
Lloyd_159
Lloyd_159OP2mo ago
double[][] testBoard = {
{1, 1, 0, 0, 0, -1, -1, -1},
{2, 0, 0, 0, 0, 0, 0, -2},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, -1, -1, -1},
{2, 0, 0, 0, 0, 0, 0, -2}
};

INDArray testInput = Nd4j.create(testBoard).reshape(1, 1, 8, 8);
double[][] testBoard = {
{1, 1, 0, 0, 0, -1, -1, -1},
{2, 0, 0, 0, 0, 0, 0, -2},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, -1, -1, -1},
{2, 0, 0, 0, 0, 0, 0, -2}
};

INDArray testInput = Nd4j.create(testBoard).reshape(1, 1, 8, 8);
okay, on it
dan1st
dan1st2mo ago
oh ok that's batch size x channels x 8x8
Lloyd_159
Lloyd_159OP2mo ago
I think it works now it outputs something like [main] INFO org.deeplearning4j.optimize.listeners.ScoreIterationListener - Score at iteration 96 is 4198.407877604167 but ig this is right?
dan1st
dan1st2mo ago
idk whether it's right it could be right but whether it learns anything useful is another thing
Lloyd_159
Lloyd_159OP2mo ago
ye ik thanks for your help
JavaBot
JavaBot2mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@765577368030937129>.
Want results from more Discord servers?
Add your server