(detector): fix, trainModel averages weights result after multiple epochs

master
nicode3141 2026-11-15 11:21:55 +01:00
parent 2e442ca526
commit f20c6209bf
1 changed files with 20 additions and 5 deletions

View File

@ -116,6 +116,9 @@ public class Detector {
double[] weights = new double[WEIGHTS_COUNT];
double bias = 0.0d;
double[] weightsSum = new double[WEIGHTS_COUNT];
double biasSum = 0.0d;
// All texts
List<TextCounts> texts = new ArrayList<>();
List<Integer> expected = new ArrayList<>();
@ -130,7 +133,8 @@ public class Detector {
}
}
double[] modelParams = null;
Random random = new Random(2);
int totalRuns = 0;
for(int j = 0; j < EPOCH_SIZE; j++) {
@ -139,7 +143,7 @@ public class Detector {
for (int i = 0; i < texts.size(); i++) {
orderIdx.add(i);
}
Collections.shuffle(orderIdx, new Random(2));
Collections.shuffle(orderIdx, random);
// Go randomly thru texts
for (int i : orderIdx) {
@ -159,12 +163,23 @@ public class Detector {
}
bias += error;
}
}
modelParams = Arrays.copyOf(weights, weights.length + 1);
modelParams[WEIGHTS_COUNT] = bias;
// Update sum to calc average later
for(int k = 0; k < WEIGHTS_COUNT; k++){
weightsSum[k] += weights[k];
}
biasSum += bias;
totalRuns++;
}
}
// Average after epochs
double[] modelParams = new double[WEIGHTS_COUNT+1];
for(int k = 0; k < WEIGHTS_COUNT; k++){
modelParams[k] = weightsSum[k] / totalRuns;
}
modelParams[WEIGHTS_COUNT] = biasSum / totalRuns;
return modelParams;
}