Reputation: 107
How to code a sigmoid function in Java?, my output for hx = [0.51,0.51,0.51,0.51,0.51]
is sigmoid =[1.6,1.6,1.6,1.6,1.6]
and when I do it in Matlab the value of sigmoid is [ 0.6248 ,0.6248, 0.6248, 0.6248]
:
public double[] sigmoidFunction() {
int i;
double[] sigmoid = new double[x_theta.length];
for(i=0;i<x_theta.length; i++)
sigmoid[i] = 1 / 1 + StrictMath.exp(-x_theta[i]);
return sigmoid;
}
Upvotes: 2
Views: 440
Reputation: 66775
You forgot about parenthesis:
sigmoid[i] = 1 / 1 + StrictMath.exp(-x_theta[i]);
is equivalent to
sigmoid[i] = (1 / 1) + StrictMath.exp(-x_theta[i]);
and so
sigmoid[i] = 1 + StrictMath.exp(-x_theta[i]);
while you seem to need
sigmoid[i] = 1 / ( 1 + StrictMath.exp(-x_theta[i]) );
Upvotes: 1