DL4J:错误的输入大小-期望矩阵
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DL4J:错误的输入大小-期望矩阵相关的知识,希望对你有一定的参考价值。
我正在尝试让我的网络评估INDArray。这是我的网络:公共类DQN
private final int inputSize;
private final int outputSize;
private final MultiLayerNetwork network;
public DQN(int inputSize, int outputSize)
this.inputSize = inputSize;
this.outputSize = outputSize;
MultiLayerConfiguration config = setConfig();
this.network = new MultiLayerNetwork(config);
this.network.init();
private MultiLayerConfiguration setConfig()
return new NeuralNetConfiguration.Builder()
.seed(12345)
.weightInit(WeightInit.XAVIER)
.updater(new AdaGrad(0.5))
.activation(Activation.RELU)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.l2(0.0001)
.list()
.layer(0, new DenseLayer.Builder().nIn(this.inputSize).nOut(250).weightInit(WeightInit.XAVIER).activation(Activation.RELU) //First hidden layer
.build())
.layer(1, new OutputLayer.Builder().nIn(250).nOut(this.outputSize).weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX) //Output layer
.lossFunction(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.build())
.build();
public INDArray evaluate(INDArray vec)
return this.network.output(vec);
不幸的是,每当我尝试使用INDArray调用评估时,都会得到:
Exception in thread "Thread-2" org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 1 array with shape [23]. Missing preprocessor or wrong input type? (layer name: layer0, layer index: 0, layer type: DenseLayer)
at org.deeplearning4j.nn.layers.BaseLayer.preOutputWithPreNorm(BaseLayer.java:308)
at org.deeplearning4j.nn.layers.BaseLayer.preOutput(BaseLayer.java:291)
at org.deeplearning4j.nn.layers.BaseLayer.activate(BaseLayer.java:339)
at org.deeplearning4j.nn.layers.AbstractLayer.activate(AbstractLayer.java:258)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.outputOfLayerDetached(MultiLayerNetwork.java:1303)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2415)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2378)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2369)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2356)
at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2452)
at wmaclean.rl.dqn.DQN.evaluate(DQN.java:51)
我的输入是具有23个元素的1级矢量,但是网络似乎想要一个矩阵。有人知道为什么吗?谢谢:)
答案
发现了我的问题。对于有类似问题的任何人,请确保在创建INDArray时指定形状:
Nd4j.create(data, **new int[]1, vectorSizeCount**)
以上是关于DL4J:错误的输入大小-期望矩阵的主要内容,如果未能解决你的问题,请参考以下文章