Wrong input size - expected matrix

I’m trying to get my network to evaluate an INDArray. This is my network:
public class 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);

    private MultiLayerConfiguration setConfig(){
        return new NeuralNetConfiguration.Builder()
                .updater(new AdaGrad(0.5))
                .layer(0, new DenseLayer.Builder().nIn(this.inputSize).nOut(250).weightInit(WeightInit.XAVIER).activation(Activation.RELU) //First hidden layer
                .layer(1, new OutputLayer.Builder().nIn(250).nOut(this.outputSize).weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX) //Output layer

    public INDArray evaluate(INDArray vec){
        return this.network.output(vec);

Unfortunately, whenever I try to call evaluate with my INDArray, I get:
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)

My inputs are rank-1 vectors with 23 elements, but the net seems to be wanting a matrix. Does anyone know why? Thanks :slight_smile:

In DL4J the model always expects to be getting an input that has the batch size as its first dimension.

So if you want to by using just a single example, you can just reshape your inputs to [1, 23] or use expandDims to add the leading dimension.

Makes perfect sense - I managed to get it going by specifying 1x23 as the shape for the input vector but didn’t realise it works because the batch size is first. Thanks