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