Files
Reseaux-de-neurones-artific…/resources/js/components/PerceptronIterationsErrorsGraph.vue
Matthias Guillitte f0e7be4476
Some checks failed
linter / quality (push) Failing after 18s
tests / ci (8.4) (push) Failing after 10s
tests / ci (8.5) (push) Failing after 11s
Gradient descent training + Added all dataset + graphs improvements
2026-03-13 22:06:08 +01:00

118 lines
3.4 KiB
Vue

<script setup lang="ts">
import type { ChartData } from 'chart.js';
import { Bar } from 'vue-chartjs';
import { colors, gridColor, gridColorBold } from '@/types/graphs';
import type { Iteration } from '@/types/perceptron';
const props = defineProps<{
iterations: Iteration[];
}>();
/**
* Return the 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: ChartData<'bar', (number | [number, number] | null)[]>[] =
[];
const epochAverageError: number[] = [];
const backgroundColors = colors;
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);
}
dataset.data.push(iteration.error);
// Epoch error
epochAverageError[iteration.iteration - 1] =
(epochAverageError[iteration.iteration - 1] || 0) +
iteration.error ** 2 / 2;
});
// Sort dataset by label (Exemple 0, Exemple 1, ...)
datasets.sort((a, b) => {
const aIndex = parseInt(a.label.split(' ')[1]);
const bIndex = parseInt(b.label.split(' ')[1]);
return aIndex - bIndex;
});
// Epoch error
const epochErrorDataset = {
type: 'line',
label: "Erreur de l'époque",
data: [],
backgroundColor: '#fff',
borderColor: '#fff',
order: 0,
tension: 0.3,
};
epochAverageError.forEach((error) => {
epochErrorDataset.data.push(error);
});
datasets.push(epochErrorDataset);
return datasets;
}
</script>
<template>
<Bar
class="flex"
:options="{
responsive: true,
maintainAspectRatio: true,
plugins: {
title: {
display: true,
text: 'Nombre d\'erreurs par itération',
},
},
scales: {
x: {
stacked: true,
min: 0,
},
y: {
stacked: true,
beginAtZero: true,
grid: {
color: function (context) {
if (context.tick.value == 0) {
return gridColorBold;
}
return gridColor;
},
},
},
},
}"
:data="{
labels: props.iterations.reduce((labels, iteration) => {
if (!labels.includes(`Itération ${iteration.iteration}`)) {
labels.push(`Itération ${iteration.iteration}`);
}
return labels;
}, [] as string[]),
datasets: getPerceptronErrorsPerIteration(),
}"
/>
</template>