From a92a47288cb1c01607fffed89fa8e0792edcfb52 Mon Sep 17 00:00:00 2001 From: Matthias Guillitte Date: Mon, 23 Mar 2026 16:01:22 +0100 Subject: [PATCH] Fixed Regression datasets --- .../LinearOrderDataSetReader.php | 5 --- .../RandomOrderDataSetReader.php | 5 --- .../js/components/PerceptronDecisionGraph.vue | 31 ++++++++++++++++++- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/app/Services/DatasetReader/LinearOrderDataSetReader.php b/app/Services/DatasetReader/LinearOrderDataSetReader.php index cd99e5f..548ecf2 100644 --- a/app/Services/DatasetReader/LinearOrderDataSetReader.php +++ b/app/Services/DatasetReader/LinearOrderDataSetReader.php @@ -29,11 +29,6 @@ class LinearOrderDataSetReader implements IDataSetReader $newLine[] = (float) $value; } - // if the dataset is for regression, we add a fake label of 0 - if (count($newLine) === 2) { - $newLine[] = 0.0; - } - $this->lines[] = $newLine; } } diff --git a/app/Services/DatasetReader/RandomOrderDataSetReader.php b/app/Services/DatasetReader/RandomOrderDataSetReader.php index 61c33c7..d8e4354 100644 --- a/app/Services/DatasetReader/RandomOrderDataSetReader.php +++ b/app/Services/DatasetReader/RandomOrderDataSetReader.php @@ -29,11 +29,6 @@ class RandomOrderDataSetReader implements IDataSetReader $newLine[] = (float) $value; } - // if the dataset is for regression, we add a fake label of 0 - if (count($newLine) === 2) { - $newLine[] = 0.0; - } - $this->lines[] = $newLine; } } diff --git a/resources/js/components/PerceptronDecisionGraph.vue b/resources/js/components/PerceptronDecisionGraph.vue index 728b681..adbbab0 100644 --- a/resources/js/components/PerceptronDecisionGraph.vue +++ b/resources/js/components/PerceptronDecisionGraph.vue @@ -35,6 +35,30 @@ const farRightDataPointX = computed(() => { return maxX; }); +function getPerceptronOutput(weightsNetwork: number[][], inputs: number[]): number[] { + for (const layer of weightsNetwork) { + const nextInputs: number[] = []; + + for (const neuron of layer) { + const bias = neuron[0]; + const weights = neuron.slice(1); + + let sum = bias; + + for (let i = 0; i < weights.length; i++) { + sum += weights[i] * inputs[i]; + } + + const activated = props.activationFunction(sum); + + nextInputs.push(activated); + } + + inputs = nextInputs; + } + return inputs; +} + function getPerceptronDecisionBoundaryDataset( networkWeights: number[][][], activationFunction: (x: number) => number = (x) => x, @@ -48,12 +72,17 @@ function getPerceptronDecisionBoundaryDataset( if ( networkWeights.length == 1 && networkWeights[0].length == 1 && - networkWeights[0][0].length == 3 + networkWeights[0][0].length <= 3 ) { // Unique, 3 weights perceptron const perceptronWeights = networkWeights[0][0]; // We take the unique perceptron function perceptronLine(x: number): number { + if (perceptronWeights.length < 3) { + // If we have less than 3 weights, we assume missing weights are zero + perceptronWeights.push(getPerceptronOutput(networkWeights, [x])[0]); + } + // w0 + w1*x + w2*y = 0 => y = -(w1/w2)*x - w0/w2 const w2 = perceptronWeights[2] == 0 ? 1e-6 : perceptronWeights[2]; // Avoid division by zero return -(perceptronWeights[1] / w2) * x - perceptronWeights[0] / w2;