我目前正在使用JAMA lib编写逻辑回归的成本函数。但它不起作用。我也不知道为什么它应该返回一个值:0.6743

public matrix cost () {
    double[][] sigmoid = sigmoidFunction().getArray();
    double[][] sigmoid2 = sigmoidFunction().getArray();
    int m = sigmoidFunction().getRowdimension();
    int n = sigmoidFunction().getColdimension();

    for (int i = 0; i<m; i++) {
        for (int j =0; j< n; j++) {
            sigmoid[i][j] = Math.log(sigmoid[i][j]);
        }
    }
    for (int i = 0; i<m; i++) {
        for (int j =0; j< n; j++) {
            sigmoid2[i][j] = Math.log(1-sigmoid2[i][j]);
        }
    }

    matrix regularized = theta.transpose().times(theta);
    double[][] reg = regularized.getArray();
    for(int i = 0; i< regularized.getRowdimension(); i++ ) {
        for (int j = 0; j< regularized.getColdimension(); j++) {
            reg[i][j] = lambda/(2*m) * (reg[i][j]);
        }
    }
    regularized = new matrix(reg);

    matrix log_hx = new matrix(sigmoid);
    matrix log1_hx = new matrix(sigmoid2);

    matrix y_1 = Y;
    y_1 = y_1.transpose().subtract(1);
    Y = Y.uminus();
    Y= Y.transpose();
    //J = 1/m * (-y' * log(hx) - (1-y)' * log(1-hx))
     matrix J  = Y.times(log_hx).subtract(y_1.times(log1_hx));

    double [][] cost =  J.getArray();
    for(int i = 0; i< J.getRowdimension(); i++ ) {
        for (int j =0; j< J.getColdimension(); j++) {
            cost[i][j]= 1/m * cost[i][j];
        }
    }
    //J = new matrix(cost);
    //J.addEquals(regularized);
    return J;
}
}


当我如上所述返回矩阵J时,它返回0.0。但是当我直接返回Y.times(log_hx).subtract(y_1.times(log1_hx))时,它神奇地返回了3.3715的值。当它不乘以1 / m并乘以正则化时是正确的

最佳答案

我知道了:我删除了ff代码:

double [][] cost =  J.getArray();
for(int i = 0; i< J.getRowdimension(); i++ ) {
    for (int j =0; j< J.getColdimension(); j++) {
        cost[i][j]= 1/m * cost[i][j];
    }


并替换成这个

double[][] cost = J.getArray();
double cost_temp = cost[0][0]*1/m;
J.set_element(0,0,cost_temp);
J.addEquals(regularized);
return J;


现在,我不知道它为什么起作用。

08-28 18:50