fixed error graph + added toggle only epoch error
Some checks failed
linter / quality (push) Successful in 5m36s
tests / ci (8.4) (push) Failing after 4m11s
tests / ci (8.5) (push) Failing after 4m37s

This commit is contained in:
2026-03-22 14:58:53 +01:00
parent 42e07de287
commit af67830fbb
5 changed files with 109 additions and 31 deletions

View File

@@ -1,46 +1,55 @@
<script setup lang="ts">
import type { ChartData } from 'chart.js';
import { computed, ref } from 'vue';
import { Bar } from 'vue-chartjs';
import { colors, gridColor, gridColorBold } from '@/types/graphs';
import type { Iteration } from '@/types/perceptron';
import Toggle from './ui/toggle/Toggle.vue';
const props = defineProps<{
iterations: Iteration[];
}>();
const epochErrorOnly = ref<boolean>(false);
/**
* Return the datasets of the iterations with the form { label: `Exemple ${exampleIndex}`, data: [error for iteration 1, error for iteration 2, ...] }
* Datasets of the iterations with the form { label: `Exemple ${exampleIndex}`, data: [error for iteration 1, error for iteration 2, ...] }
*/
function getPerceptronErrorsPerIteration(): ChartData<
'bar',
(number | [number, number] | null)[]
>[] {
const datasets = computed<
ChartData<'bar', (number | [number, number] | null)[]>[]
>(() => {
const datasets: ChartData<'bar', (number | [number, number] | null)[]>[] =
[];
const epochAverageError: number[] = [];
const backgroundColors = colors;
const exampleCountPerEpoch: Record<number, number> = {};
props.iterations.forEach((iteration) => {
const exampleLabel = `Exemple ${iteration.exampleIndex}`;
let dataset = datasets.find((d) => d.label === exampleLabel);
if (!dataset) {
dataset = {
label: exampleLabel,
data: [],
order: 1,
backgroundColor:
backgroundColors[
iteration.exampleIndex % backgroundColors.length
],
};
datasets.push(dataset);
if (!epochErrorOnly.value) {
const exampleLabel = `Exemple ${iteration.exampleIndex}`;
let dataset = datasets.find((d) => d.label === exampleLabel);
if (!dataset) {
dataset = {
label: exampleLabel,
data: [],
order: 1,
backgroundColor:
backgroundColors[
iteration.exampleIndex % backgroundColors.length
],
};
datasets.push(dataset);
}
dataset.data.push(iteration.error);
}
dataset.data.push(iteration.error);
exampleCountPerEpoch[iteration.epoch] = (exampleCountPerEpoch[iteration.epoch] || 0) + 1;
// Epoch error
epochAverageError[iteration.epoch - 1] =
(epochAverageError[iteration.epoch - 1] || 0) +
epochAverageError[iteration.epoch] =
(epochAverageError[iteration.epoch] || 0) +
iteration.error ** 2 / 2;
});
@@ -54,7 +63,7 @@ function getPerceptronErrorsPerIteration(): ChartData<
// Epoch error
const epochErrorDataset = {
type: 'line',
label: "Erreur de l'époque",
label: "Erreur quadratique moyenne de l'époque",
data: [],
backgroundColor: '#fff',
borderColor: '#fff',
@@ -62,14 +71,15 @@ function getPerceptronErrorsPerIteration(): ChartData<
tension: 0.3,
};
epochAverageError.forEach((error) => {
epochErrorDataset.data.push(error);
epochAverageError.forEach((error, index) => {
const exampleCount = exampleCountPerEpoch[index] || 1; // Avoid division by zero
epochErrorDataset.data.push(error / exampleCount);
});
datasets.push(epochErrorDataset);
return datasets;
}
});
</script>
<template>
@@ -111,7 +121,12 @@ function getPerceptronErrorsPerIteration(): ChartData<
}
return labels;
}, [] as string[]),
datasets: getPerceptronErrorsPerIteration(),
datasets: datasets,
}"
/>
<div class="flex items-center gap-3">
<Toggle v-model="epochErrorOnly" class="cursor-pointer" variant="outline" id="epoch-error-only"
>Afficher uniquement l'erreur quadratique moyenne de l'époque</Toggle
>
</div>
</template>