2017-08-10 134 views
1

我相信deeplearning4j和R完全相同的參數應該執行相同的可比較的MSE。但我不確定如何實現這一點。Deeplearning4j與R結果不同

我有一個csv文件,格式如下,其中包含46變量和2輸出。完全有1,0000樣本。所有數據都進行了歸一化處理,模型用於迴歸分析。

S1 | S2 | ... | S46 | X | Y 

在R,我使用neuralnet包,以及將碼是:

rn <- colnames(traindata) 
f <- as.formula(paste("X + Y ~", paste(rn[1:(length(rn)-2)], collapse="+"))) 
nn <- neuralnet(f, 
       rep=1, 
       data=traindata, 
       hidden=c(10), 
       linear.output=T, 
       threshold = 0.5) 

這是相當簡單的。

由於我想將算法整合到Java項目中,所以我認爲dl4j來訓練模型。火車組與R代碼完全一樣。測試集是隨機選擇的。的dl4j代碼是:

MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() 
      .seed(rngSeed) //include a random seed for reproducibility 
      // use stochastic gradient descent as an optimization algorithm 
      .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) 
      .iterations(100) 
      .learningRate(0.0001) //specify the learning rate 
      .updater(Updater.NESTEROVS).momentum(0.9) //specify the rate of change of the learning rate. 
      .regularization(true).l2(0.0001) 
      .list() 
      .layer(0, new DenseLayer.Builder() //create the first, input layer with xavier initialization 
        .nIn(46) 
        .nOut(10) 
        .activation(Activation.TANH) 
        .weightInit(WeightInit.XAVIER) 
        .build()) 
      .layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MSE) //create hidden layer 
        .nIn(10) 
        .nOut(outputNum) 
        .activation(Activation.IDENTITY) 
        .build()) 
      .pretrain(false).backprop(true) //use backpropagation to adjust weights 
      .build(); 

時期的數量是10和batchsize是128

使用測試組,R的性能是 enter image description here

和dl4j的性能是以下,我認爲它沒有發揮其全部潛力。

dl4j的mornitor是

由於存在dl4j如updaterregulizationweightInit更多參數。所以我認爲一些參數設置不正確。順便說一句,爲什麼在mornitor圖中有周期性的刺。

任何人都可以幫忙嗎?

回答

1

大多數神經網絡訓練發生在minibatches。 Deeplearning4j假設默認情況下你沒有玩玩具問題(內存中的所有數據< 10例子等)

神經網絡配置有一個叫做minibatch的功能,你應該查找。

在配置上將minibatch設置爲false,您應該得到相同的結果。

如果你想知道爲什麼會發生這種情況,這是因爲小批次學習不同於記憶中的所有事情。小批次學習會自動按照小批量大小分配梯度。當你在記憶中做所有事情時,你都不想那樣做。

當您運行其他實驗時請注意這一點。 欲瞭解更多關於此,請參閱: https://deeplearning4j.org/toyproblems