请帮帮我!我正在使用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]
答案 0 :(得分:1)
您正在初始化神经网络错误。如果您仔细观察dl4j示例中的每个 cnn示例(提示:这是您应该从中提取代码的规范来源,其他任何内容都可能无效或过时:{{ 3}}) 您将在我们的所有示例中注意到我们有一个inputType配置: https://github.com/deeplearning4j/dl4j-examples
如果从不手动设置nIn,您应该使用各种类型。只是nOut。
对于mnist,我们使用卷积平面并将其转换为自动4d数据集。
Mnist以平面向量开始,但是cnn只能理解3d数据。我们为您做过这种转变并重塑。