2016-11-08 25 views
0

我有一個自定義的7(高度)和24(寬度)矩陣輸入用於培訓。輸出是年齡(年輕,成熟,老)的標籤。 我想和Deeplearning4J卷積神經網絡一起去。DeepLearning4J IllegalArgumentException自定義矩陣的CNN

建立一個非常基本的卷積神經網絡後,第一個培訓項目給出了以下錯誤,我不知道這是什麼。

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) 

我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); 

我使用DL4J版本0.6,Java版本1.8,行家3.3+

我懷疑的錯誤在庫中。

回答

0

藉助gitter支持。我發現模型和輸入不匹配。正確的工作代碼如下。

我希望那些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.");