我需要在 -1 和 1 的范围内生成浮点数。我正在使用这一行:
weights[i] = random.nextFloat() * (weightMax - weightMin) + weightMin;
在哪里weightMax = 1
和weightMin = -1
但有时它会产生 NaN。我怎样才能防止这种情况发生?
编辑:是的,你是对的。我稍后会这样做:
private void run_neural_network(double[] inputs, double[] outputs) {
int i, j;
int weight_counter = 0;
for (i = 0; i < NB_OUTPUTS; i++) {
double sum = 0.0;
for (j = 0; j < NB_INPUTS; j++) {
sum += inputs[j] * weights[weight_counter];
weight_counter++;
}
outputs[i] = (Math.tanh(sum + weights[weight_counter]));
weight_counter++;
}
}
这就是sum
NaN 的来源:
[EpuckController] inputs0: 0.0, weight0 0.06174159,
[EpuckController] inputs1: 306.1555543759677, weight1 -7.409203E-23,
[EpuckController] inputs2: 0.0, weight2 1.2461361E-29,
[EpuckController] inputs3: 307.56460985278545, weight3 194105.33,
[EpuckController] inputs4: 0.0, weight4 0.41100776,
[EpuckController] inputs5: 0.0, weight5 0.0013858302,
[EpuckController] inputs6: 320.0046791338875, weight6 -1.49799987E14,
[EpuckController] inputs7: 0.0, weight7 195593.81,
[EpuckController] inputs8: 421.5353365457416, weight8 -0.8191289,
[EpuckController] Sum NaN