0

请帮帮我!我正在使用deeplearning4j. MNIST 示例运行良好,但我的数据集出现错误。我的数据集有两个输出。

int height = 45;
int width = 800;
int channels = 1;
int rngseed = 123;
Random randNumGen = new Random(rngseed);
int batchSize = 128;
int outputNum = 2;
int numEpochs = 15;
File trainData = new File("C:/Users/JHP/Desktop/learningData/training");
File testData = new File("C:/Users/JHP/Desktop/learningData/testing");
FileSplit train = new FileSplit(trainData, NativeImageLoader.ALLOWED_FORMATS, randNumGen);
FileSplit test = new FileSplit(testData, NativeImageLoader.ALLOWED_FORMATS, randNumGen);
ParentPathLabelGenerator labelMaker = new ParentPathLabelGenerator();

ImageRecordReader recordReader = new ImageRecordReader(height, width, channels, labelMaker);
ImageRecordReader recordReader2 = new ImageRecordReader(height, width, channels, labelMaker);
recordReader.initialize(train);
recordReader2.initialize(test);

DataSetIterator dataIter = new RecordReaderDataSetIterator(recordReader, batchSize, 1, outputNum);
DataSetIterator testIter = new RecordReaderDataSetIterator(recordReader2, batchSize, 1, outputNum);

DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
scaler.fit(dataIter);
dataIter.setPreProcessor(scaler);

System.out.println("Build model....");
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
        .seed(rngseed)
        .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
        .iterations(1)
        .learningRate(0.006)
        .updater(Updater.NESTEROVS).momentum(0.9)
        .regularization(true).l2(1e-4)
        .list()
        .layer(0,   new DenseLayer.Builder()
                .nIn(height * width)
                .nOut(1000)
                .activation(Activation.RELU)
                .weightInit(WeightInit.XAVIER)
                .build()
                )
        .layer(1, newOutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD)
                .nIn(1000)
                .nOut(outputNum)
                .activation(Activation.SOFTMAX)
                .weightInit(WeightInit.XAVIER)
                .build()
                )
        .pretrain(false).backprop(true)
        .build();

MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
model.setListeners(new ScoreIterationListener(1));

System.out.println("Train model....");
for (int i = 0; i < numEpochs; i++) {
    try {
        model.fit(dataIter);
    } catch (Exception e) {
        System.out.println(e);
    }
}

错误是

org.deeplearning4j.exception.DL4JInvalidInputException:输入不是矩阵;预期矩阵(秩 2),得到秩为 4 的数组,形状为 [128, 1, 45, 800]

4

1 回答 1

1

你初始化神经网络是错误的。如果您仔细查看dl4j 示例存储库中的每个cnn 示例(提示:这是您应该从中提取代码的规范来源,其他任何内容都可能无效或过时:https ://github.com/deeplearning4j /dl4j-examples)您会注意到在我们所有的示例中,我们都有一个 inputType 配置: https ://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/ deeplearning4j/examples/convolution/LenetMnistExample.java#L114

您应该使用多种类型,永远不要手动设置 nIn。只是nOut。

对于 mnist,我们使用卷积平面并将其自动转换为 4d 数据集。

Mnist 一开始是一个平面向量,但 cnn 只理解 3d 数据。我们为您完成过渡和重塑。

于 2017-04-20T04:19:40.123 回答