user3065339
user3065339

Reputation: 107

Cost function of Logistic Regression in Java using JAMA lib

I'm currently coding the cost function of logistic regression using JAMA lib. but it's not working. and I don't know why. it supposed to return a value: 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;
}
}

when I return the matrix J as seen above, it returns 0.0 . but when I directly return Y.times(log_hx).subtract(y_1.times(log1_hx)), it magically returns a value of 3.3715. which is correct when it is not multiplied by 1/m and added by regularization

Upvotes: 1

Views: 606

Answers (1)

user3065339
user3065339

Reputation: 107

I figure it out: I remove the ff code:

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];
    }

and replace it with this

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

now, I dont know why it works.

Upvotes: 1

Related Questions