【问题标题】:Code error using MNIST example of deeplearning4j使用 deeplearning4j 的 MNIST 示例的代码错误
【发布时间】:2017-09-15 02:07:39
【问题描述】:

请帮帮我!我正在使用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]

【问题讨论】:

  • 我认为有必要将DataSetIterator函数更改为另一个函数。在 MNIST 示例中,就像将数据导入函数一样。 DataSetIterator mnistTrain = new MnistDataSetIterator (batchSize, true, rngseed); 不知道用什么函数。
  • @TriV TriV 非常感谢您让我知道需要改进的地方!我不知道,因为我是第一次使用堆栈溢出。非常感谢!

标签: deep-learning mnist deeplearning4j


【解决方案1】:

您错误地初始化了神经网络。如果您仔细查看 dl4j 示例 repo 中的 每个 cnn 示例(提示:这是您应该从中提取代码的规范来源,其他任何内容都可能无效或过时:@987654321 @) 您会注意到在我们所有的示例中,我们都有一个 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 数据。我们为您完成过渡和重塑。

【讨论】:

    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 2011-07-18
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    相关资源
    最近更新 更多