[英]DeepLearning4J IllegalArgumentException for CNN with custom Matrix
I have a custom 7(height) and 24(width) matrix input to be used for training. 我有一个自定义的7(高度)和24(宽度)矩阵输入用于训练。 The output are labels with Age ( Young, Mature, Old).
输出是带有年龄(青年,成熟,老年)的标签。 I would like to go with Deeplearning4J Convolutional Neural Networks.
我想使用Deeplearning4J卷积神经网络。
After building a very basic Convolutional Neural Network the very first training item gives the following error and i have no clue what is this about. 在构建了非常基本的卷积神经网络之后,第一个训练项目给出了以下错误,我不知道这是怎么回事。
Exception in thread "main" java.lang.IllegalArgumentException: Invalid size index 2 wher it's >= rank 2
at org.nd4j.linalg.api.ndarray.BaseNDArray.size(BaseNDArray.java:4066)
at org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.preOutput(ConvolutionLayer.java:192)
at org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.activate(ConvolutionLayer.java:247)
at org.deeplearning4j.nn.graph.vertex.impl.LayerVertex.doForward(LayerVertex.java:88)
at org.deeplearning4j.nn.graph.ComputationGraph.feedForward(ComputationGraph.java:983)
at org.deeplearning4j.nn.graph.ComputationGraph.computeGradientAndScore(ComputationGraph.java:889)
My DL4J code 我的DL4J代码
//Model Config here
MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder()
.seed(seed)
.iterations(iterations)
.regularization(true).l2(0.0005)
.learningRate(0.01)//.biasLearningRate(0.02)
//.learningRateDecayPolicy(LearningRatePolicy.Inverse).lrPolicyDecayRate(0.001).lrPolicyPower(0.75)
.weightInit(WeightInit.XAVIER)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(Updater.NESTEROVS).momentum(0.9)
.list()
.layer(0, new ConvolutionLayer.Builder(4, 1)
//nIn and nOut specify depth. nIn here is the nChannels and nOut is the number of filters to be applied
.name("hzvt1")
.nIn(nChannels)
.stride(1, 1)
.nOut(26)
.activation("relu")//.activation("identity")
.build())
.layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.nOut(outputNum)
.activation("softmax")
.build())
.setInputType(InputType.convolutional(nChannels,height,width))
.backprop(true).pretrain(false);
//Model build here
model.fit(wmTrain);MultiLayerConfiguration conf = builder.build();
model.fit(wmTrain);MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
//Training data creation here
INDArray weekMatrix = Nd4j.ones(DLAgeGender.nChannels,DLAgeGender.height*DLAgeGender.width);
double[] vector = new double[] { 0.0, 1.0, 0.0 };
INDArray intLabels = Nd4j.create(vector);
DataSet ds=new DataSet(weekMatrix,intLabels);
//Train the first item
model.fit(wmTrain);
I am using DL4J version 0.6 , Java Version 1.8, maven 3.3+ 我正在使用DL4J 0.6版,Java 1.8版,Maven 3.3+
I suspect a bug in the library. 我怀疑库中有错误。
With the help of gitter support. 借助gitter支持。 I found out that the model and input is not matching.
我发现模型和输入不匹配。 The correct working code is as follows.
正确的工作代码如下。
I hope those DL4J error/exception messages are more clear in the next releases. 我希望这些DL4J错误/异常消息在以后的版本中更加清楚。
log.info("Build model....");
System.out.println("Building model...");
MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder()
.seed(seed)
.iterations(iterations)
.regularization(true).l2(0.0005)
.learningRate(0.01)//.biasLearningRate(0.02)
//.learningRateDecayPolicy(LearningRatePolicy.Inverse).lrPolicyDecayRate(0.001).lrPolicyPower(0.75)
.weightInit(WeightInit.XAVIER)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(Updater.NESTEROVS).momentum(0.9)
.list()
.layer(0, new ConvolutionLayer.Builder(4, 1)
//nIn and nOut specify depth. nIn here is the nChannels and nOut is the number of filters to be applied
.name("hzvt1")
.nIn(nChannels)
.stride(1, 1)
.nOut(26)
.activation("relu")//.activation("identity")
.build())
.layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.nOut(classes)
.activation("softmax")
.build())
.setInputType(InputType.convolutional(height,width,nChannels))
.backprop(true).pretrain(false);
//Model build here
model.fit(wmTrain);MultiLayerConfiguration conf = builder.build();
model.fit(wmTrain);MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
//Training data creation here
INDArray weekMatrix = Nd4j.ones(new int[]{1,DLAgeGender.nChannels,DLAgeGender.height,DLAgeGender.width});
INDArray intLabels;
double[] vector = new double[] { 0.0, 1.0, };
intLabels = Nd4j.create(vector);
DataSet ds=new DataSet(weekMatrix,intLabels);
log.info("Train model....");
model.setListeners(new ScoreIterationListener(1));
model.fit(wmTrain);
System.out.println("Data train OK.");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.