java - 无法更新 arrayList 中的元素

标签 java arraylist machine-learning

我有两个特征和权重元素矩阵。我正在实现一种学习算法。我想更新 arraylist 的元素(用于表示一个特征样本的 vector )。以下是代码。但是我的矩阵元素( vector 元素没有)更新了。我也放了示例解决方案。更新前后的值不会相同。您能告诉我代码中的缺陷在哪里吗?

    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]

最佳答案

我只能想到为什么会发生这种情况的几个合理原因:

  1. 价格 == 0
  2. testList.contains(i) 始终为 false

我强烈建议使用断点来调试它。至少,在调用 featureVector.set() 的地方放置一个 System.out.println 以确保它被调用。我猜它永远不会被调用,因为条件永远不会成为现实。

一定要使用断点,这将是你的救星......

关于java - 无法更新 arrayList 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10305301/

相关文章:

java - 我怎么知道我的神经网络正在被正确训练

java - 尝试使用 Intent 登录时出现错误

java - Firestore 字段的数组值求和

python - python中的操作数无法一起广播错误

android - 按格式化日期列对 sqlite 游标结果进行分组的方法

java - LibGDX -TagGame 类包含位置 11 处格式错误的签名 'LArrayList<>;'

mongodb - 无法将数组从 MongoDB 传递到需要向量的 Spark 机器学习函数

java - 无法在android中设置背景完全透明

java - 为什么主线程被阻塞

java - 非常大的 Java ArrayList 遍历时间很慢