简体   繁体   English

INDArray 的 DeepLearning4J 问题

[英]DeepLearning4J Problems with INDArray

    public void playFullGame(MultiLayerNetwork m1, MultiLayerNetwork m2) {
    boolean player = false;
    while (!this.isOver) {
        float[] f = Main.rowsToInput(this.rows);
        System.out.println(f.length);// prints 42
        INDArray input = Nd4j.create(f);
        this.addChip(Main.getHighestOutput(player ? m1.output(input) : m2.output(input)), player);
        player = !player;
    }
}

I use INDArray input = Nd4j.create(f);我使用INDArray input = Nd4j.create(f); to create the INDArray but this m1.output(input) throws following exception:创建 INDArray 但此m1.output(input)引发以下异常:

Exception in thread "AWT-EventQueue-0" org.deeplearning4j.exception.DL4JInvalidInputException: Input size (63 columns; shape = [1, 63]) is invalid: does not match layer input size (layer # inputs = 42) (layer name: layer2, layer index: 2, layer type: OutputLayer)

I do not understand why the created INDArray is two-dimensional and where the 63 is coming from..我不明白为什么创建的 INDArray 是二维的以及 63 的来源..

Edit: The MultiLayerNetwork Configuration:编辑:多层网络配置:

MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(randSeed).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
            .updater(new Nesterovs(0.1, 0.9)).list()
            .layer(new DenseLayer.Builder().nIn(numRows * numColums).nOut(63).activation(Activation.RELU)
                    .weightInit(WeightInit.XAVIER).build())
            .layer(new DenseLayer.Builder().nIn(63).nOut(63).activation(Activation.RELU)
                    .weightInit(WeightInit.XAVIER).build())
            .layer(new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD).nIn(numRows * numColums).nOut(7)
                    .activation(Activation.SOFTMAX).weightInit(WeightInit.XAVIER).build())
            .build();

Your 63 is coming from the neural network itself.你的 63 来自神经网络本身。 You have a shape mismatch on the number of inputs and outputs.您的输入和输出数量存在形状不匹配。

If you're new to neural networks, just understand that the basic 2d neural networks have a number of inputs and outputs specified.如果您不熟悉神经网络,只需了解基本的 2d 神经网络有许多指定的输入和输出。 The next layer should have the number of inputs match the number of outputs of the previous layer.下一层的输入数量应与前一层的输出数量相匹配。

For dense layers, the number of inputs in the first layer needs to match the number of input columns in your dataset.对于密集层,第一层中的输入数量需要与数据集中的输入列数相匹配。 I would make sure whatever your dataset is the number of columns matches.我会确保无论您的数据集是匹配的列数。

Of note here is setting the number of inputs and outputs for every layer of a neural network can be error prone.值得注意的是,为神经网络的每一层设置输入和输出的数量可能容易出错。 Instead, just set the number of outputs for each layer and use dl4j's setInputType api instead with the number of columns.相反,只需设置每层的输出数量,并使用 dl4j 的 setInputType api 代替列数。 In your case add InputType.feedforward( https://github.com/eclipse/deeplearning4j/blob/master/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/inputs/InputType.java#L107 )在你的情况下添加 InputType.feedforward( https://github.com/eclipse/deeplearning4j/blob/master/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/inputs/InputType.java# L107 )

MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(randSeed).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
            .updater(new Nesterovs(0.1, 0.9)).list()
            .layer(new DenseLayer.Builder().nIn(numRows * numColums).nOut(63).activation(Activation.RELU)
                    .weightInit(WeightInit.XAVIER).build())
            .layer(new DenseLayer.Builder().nIn(63).nOut(63).activation(Activation.RELU)
                    .weightInit(WeightInit.XAVIER).build())
            .layer(new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD).nIn(numRows * numColums).nOut(7)
                    .activation(Activation.SOFTMAX).weightInit(WeightInit.XAVIER).build())
            .setInputType(InputType.feedForward(numRows * numColums))
            .build();

More examples (mainly cnns) here: https://github.com/eclipse/deeplearning4j-examples/search?q=setInputType更多示例(主要是cnns)在这里: https://github.com/eclipse/deeplearning4j-examples/search?q=setInputType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM