我需要生成一個範圍的-1和1內浮標我使用這條線:代-1內隨機浮點數和1產生的NaN
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
是楠來源:
[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
你是否在權重數組上做其他操作?我看不到您發佈的代碼中存在任何缺陷。 –