From 10e1dd01269e2097e5077d4a8d4ba8ac009b6417 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 6 Jul 2026 11:22:23 +0200 Subject: [PATCH] 3D DeltaWeight fix --- Runtime/Scripts/Core/Synapse.cs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Runtime/Scripts/Core/Synapse.cs b/Runtime/Scripts/Core/Synapse.cs index 640cf04..9116e12 100644 --- a/Runtime/Scripts/Core/Synapse.cs +++ b/Runtime/Scripts/Core/Synapse.cs @@ -36,9 +36,7 @@ namespace NanoBrain { this.weight = weight; } - public virtual void BackPropagation(Neuron receiver, float error, float learningRate) { - float derivative = error; - + public virtual void BackPropagation(Neuron receiver, float derivative, float learningRate) { switch (receiver.activator) { case Neuron.ActivationType.Linear: derivative *= 1; @@ -67,8 +65,6 @@ namespace NanoBrain { } public virtual void BackPropagation(Neuron receiver, Vector3 derivative, float learningRate) { - //Vector3 derivative = error; - switch (receiver.activator) { case Neuron.ActivationType.Linear: derivative *= 1; @@ -83,21 +79,28 @@ namespace NanoBrain { break; default: Debug.Log("other activator"); - break; + break; } this.neuron.BackPropagation3D(derivative * this.weight, learningRate); - derivative = Vector3.Scale(derivative, this.neuron.activation); + derivative *= math.length(this.neuron.activation); if (this.trainable) { - // Compared to the 1D solution, this does not decrease the weight. - // direction and sign are different.... - // Perhaps the sign is determine by the direction of the derivative and the neuron activation? - // When they are oppositie, the sign is negative? (or the other way round...) - this.weight += learningRate * derivative.magnitude; + float deltaWeight = learningRate * derivative.magnitude; + // Compared to the 1D solution, this does not decrease the weight because magnitude is always positive + // derivative.direction and derivative.sign are different.... + if (AreOpposed(derivative, this.neuron.activation)) + this.weight -= deltaWeight; + else + this.weight += deltaWeight; } } + + public static bool AreOpposed(Vector3 a, Vector3 b) { + // Check if the angle between the vectors is > 90 degrees + return Vector3.Dot(a, b) < 0f; + } } } \ No newline at end of file