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