使用JAMA lib的Java中Logistic回归的成本函数

时间:2013-12-14 10:00:21

标签: java machine-learning logistic-regression

我目前正在使用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并且通过正则化添加时是正确的

1 个答案:

答案 0 :(得分:1)

我明白了:我删除了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;

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