2012-04-24 59 views
0

我有兩個特徵和權重元素矩陣。我正在實現一個學習算法。我想更新arraylist的元素(用於表示一個特徵樣本的向量)。以下是代碼。但我的矩陣元素(矢量元素不)更新。我也提供了樣本解決方案。預計不會更新前後的相同值。請讓我知道代碼中的缺陷在哪裏?無法更新arrayList中的元素

for(int i =0 ; i< N ; i++){ //N is a large real number 
    ArrayList<Double> featureVector = new ArrayList<Double>(); 
    featureVector = FeatureMatrix.get(i); 
    System.out.println("Before::"+ featureVector); 
    if(testList.contains(i)){ 
    for(int j=0 ; j< testList.size(); j++){ 
     if(i == testList.get(j)){  
     int indexInTestList= j; 
     List<Double> subListNextCandidate ; 
     subListNextCandidate = weightVectorNextCandidate.subList((10*indexIntTestList),((10)*(indexInTestList+1))); //clips a portion of member from long list of members 
     List<Double> approxWeight = new ArrayList<Double>(); 
     approxWeight = getApproxWeight(FeatureVector, indexInTestList, FeatureMatrix,WeightMatrix, bias); //approxWeight is a vector of same dimension as of featureVector 

     for(int l=0 ; l< 10;l++){     
      double Update = featureVector.get(l)+ Rate*((subListCandidate.get(l)-approxWeight.get(l))-(lambda*featureVector.get(l)*(1/M)));//M is large real number 
      featureVector.set(l,Update); 

     }     
     } 
    } 
    } 

    else{ 
    for(int l=0 ; l< 10;l++){ 
     double Update = featureVector.get(l) -Rate*(lambda*featureVector.get(l)*(1/M)); 
     featureVector.set(l, Update); 
    }     
    } 
    System.out.println("After:::"+ FeatureMatrix.get(i)); 
} 

示例輸出::

Before::[0.04539928251182193, -0.16233604402485394, 0.905018369795912, -1.2817141994528614, 0.7065420460225843, -0.8946090188977665, -1.74892020689701, -2.1539901172158187, 1.8229765478806985, -1.8109945435256574] 
After:::[0.04539928251182193, -0.16233604402485394, 0.905018369795912, -1.2817141994528614, 0.7065420460225843, -0.8946090188977665, -1.74892020689701, -2.1539901172158187, 1.8229765478806985, -1.8109945435256574] 
+3

您應該使用調試器並逐步執行代碼中的行。 – 2012-04-24 20:16:37

+1

1.格式化您的代碼2.「testList」來自何處? (在您發佈的代碼中沒有初始化。) – aviad 2012-04-24 20:28:52

+0

測試列表作爲方法的參數。 – thetna 2012-04-24 20:35:33

回答

2

我能想到的只有一對夫婦的合理原因,這會發生的:

  1. 率== 0
  2. testList.contains (i)始終爲假

我強烈建議使用斷點來調試。至少,放置一個System.out.println,其中調用featureVector.set()以確保它被調用。我猜測它永遠不會被調用,因爲條件永遠不會成真。

確實使用斷點,這將是一個生命的救星......

0

什麼是testList.get(j)返回類型?你正在比較一個整數,我猜想是一個整數。這不太可能順利......