Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么DeepLearning4J CNN在INDARY输出中返回的不是概率而是0和1_Java_Conv Neural Network_Deeplearning4j_Dl4j - Fatal编程技术网

Java 为什么DeepLearning4J CNN在INDARY输出中返回的不是概率而是0和1

Java 为什么DeepLearning4J CNN在INDARY输出中返回的不是概率而是0和1,java,conv-neural-network,deeplearning4j,dl4j,Java,Conv Neural Network,Deeplearning4j,Dl4j,我正在使用DL4J版本1.0.0-beta3,并尝试创建一个卷积神经网络,用于识别32x32个棋子图像。 以下是我用来创建和训练网络的代码: public class BuildNetwork1 { public static void main(String[] args) throws Exception { File rootDir = new File("./CNNinput/chesscom1");

我正在使用DL4J版本1.0.0-beta3,并尝试创建一个卷积神经网络,用于识别32x32个棋子图像。 以下是我用来创建和训练网络的代码:

public class BuildNetwork1 {

    public static void main(String[] args) throws Exception {
        
        File rootDir = new File("./CNNinput/chesscom1");
        
        File locationToSave = new File(rootDir, "trained.chesscom1.bin");
        
        int height = 32;
        int width = 32;
        int channels = 1;
        int rngseed = 777;
        int numEpochs = 100;
        
        File trainData = new File(rootDir, "training");
        
        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .seed(rngseed)
                .updater(new Adam.Builder().learningRate(0.01).build())
                .activation(Activation.IDENTITY)
                .weightInit(WeightInit.XAVIER)
                .list()
                //.layer(new ConvolutionLayer.Builder(new int[] {5, 5}, new int[] {1, 1}, new int[]{0, 0}).name("cnn1").nIn(1).nOut(64).biasInit(0).build())
                //.layer(new SubsamplingLayer.Builder(new int[] {2, 2}, new int[] {2, 2}).name("maxpool1").build())
                //.layer(new ConvolutionLayer.Builder(new int[] {5, 5}, new int[] {1, 1}, new int[]{0, 0}).name("cnn2").nIn(64).nOut(16).biasInit(0).build())
                .layer(new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                        .nOut(13)
                        .activation(Activation.SOFTMAX)
                        .build())
                .setInputType(InputType.convolutional(32, 32, 1))
                .build();
        
        
        MultiLayerNetwork network = new MultiLayerNetwork(conf);
        
        network.init();
        network.setListeners(new ScoreIterationListener(10));
        
        ImageLoader loader = new ImageLoader(height, width, channels);
        DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
        
        for (int e = 0; e < numEpochs; e++) {
            File[] labels = trainData.listFiles();
            for (int i = 0; i < labels.length; i++) {
                File label = labels[i];
                File[] images = label.listFiles();
                for (int j = 0; j < images.length; j++) {
                    File imageFile = images[j];
                    BufferedImage image = ImageIO.read(imageFile);
                    
                    INDArray input = loader.asMatrix(image).reshape(1, channels, height, width);
                    scaler.fit(new DataSet(input, null));
                    scaler.transform(input);
                    
                    double[][] outputArray = new double[1][13];
                    outputArray[0][Integer.parseInt(label.getName())] = 1d;
                    INDArray output = Nd4j.create(outputArray);
                    
                    network.fit(input, output);
                }
            }
        }
        
        
        boolean saveUpdater = true;
        ModelSerializer.writeModel(network, locationToSave, saveUpdater);
    }
}
你知道为什么会这样吗?
提前多谢

您的模型对其输出非常有信心。当您向it部门展示以前可能见过的数据时,以及当您已将模型训练为非常适合该数据(通常称为过度拟合)时,可能会发生这种情况。

感谢您的及时回答!当我测试棋子的任意图像时,同样的自信输出(0和1)也会出现,但当然输出是错误的。最后,无论我尝试什么,我都无法接收概率。因为您使用的是身份激活,我猜softmax会获得非常高的输入数字,这些数字会变为1/0或非常接近1或0,实际上就是这样。嗨,保罗,再次感谢您的回答!我在[0,1]中使用了scaler作为输入,我假设它可以处理这个问题,不是吗?这会将您的输入置于0..1范围内,但与权重相乘后,它可能位于任何范围内。嗨,Paul,非常感谢!我成功地产生了概率!使用Activation.TANH并取消对代码中隐藏层的注释。我还评论了updater,因为它可以很好地处理默认值。
public class CalcNetworkAll {
    
    
    public static void main(String[] args) throws Exception {
        
        int height = 32;
        int width = 32;
        int channels = 1;
        
        File rootDir            = new File("./CNNinput/chesscom1");
        File locationToLoad     = new File("./CNNinput/chesscom1/trained.chesscom1.bin");
        
        File testData = new File(rootDir, "testing");
        
        MultiLayerNetwork network = ModelSerializer.restoreMultiLayerNetwork(locationToLoad);
        ImageLoader loader = new ImageLoader(height, width, channels);
        DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
        
        File[] labels = testData.listFiles();
        for (int i = 0; i < labels.length; i++) {
            File label = labels[i];
            File[] images = label.listFiles();
            for (int j = 0; j < images.length; j++) {
                File imageFile = images[j];
                BufferedImage image = ImageIO.read(imageFile);
                
                INDArray input = loader.asMatrix(image).reshape(1, channels, height, width);
                scaler.fit(new DataSet(input, null));
                scaler.transform(input);
                
                INDArray output = network.output(input, false);
                System.out.println(label.getName() + " => " + output);
            }
        }
    }
}
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,8.1707e-37,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
0 => [[    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
1 => [[         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
10 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0]]
11 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0]]
11 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0]]
11 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0]]
11 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0]]
11 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0]]
11 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0]]
11 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0]]
12 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000]]
12 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000]]
12 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000]]
12 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000]]
12 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000]]
12 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000]]
12 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
2 => [[         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
3 => [[         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
4 => [[         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0,         0]]
5 => [[         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0]]
5 => [[         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0]]
5 => [[         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0]]
5 => [[         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0]]
5 => [[         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0]]
5 => [[         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0]]
5 => [[         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0,         0]]
6 => [[         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0]]
6 => [[         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0]]
6 => [[         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0]]
6 => [[         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0]]
6 => [[         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0]]
6 => [[         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0]]
6 => [[         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
7 => [[         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
8 => [[         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]
9 => [[         0,         0,         0,         0,         0,         0,         0,         0,         0,    1.0000,         0,         0,         0]]