3D DeltaWeight fix

This commit is contained in:
Pascal Serrarens 2026-07-06 11:22:23 +02:00
parent 50e09c0db3
commit 10e1dd0126

View File

@ -36,9 +36,7 @@ namespace NanoBrain {
this.weight = weight; this.weight = weight;
} }
public virtual void BackPropagation(Neuron receiver, float error, float learningRate) { public virtual void BackPropagation(Neuron receiver, float derivative, float learningRate) {
float derivative = error;
switch (receiver.activator) { switch (receiver.activator) {
case Neuron.ActivationType.Linear: case Neuron.ActivationType.Linear:
derivative *= 1; derivative *= 1;
@ -67,8 +65,6 @@ namespace NanoBrain {
} }
public virtual void BackPropagation(Neuron receiver, Vector3 derivative, float learningRate) { public virtual void BackPropagation(Neuron receiver, Vector3 derivative, float learningRate) {
//Vector3 derivative = error;
switch (receiver.activator) { switch (receiver.activator) {
case Neuron.ActivationType.Linear: case Neuron.ActivationType.Linear:
derivative *= 1; derivative *= 1;
@ -83,21 +79,28 @@ namespace NanoBrain {
break; break;
default: default:
Debug.Log("other activator"); Debug.Log("other activator");
break; break;
} }
this.neuron.BackPropagation3D(derivative * this.weight, learningRate); this.neuron.BackPropagation3D(derivative * this.weight, learningRate);
derivative = Vector3.Scale(derivative, this.neuron.activation); derivative *= math.length(this.neuron.activation);
if (this.trainable) { if (this.trainable) {
// Compared to the 1D solution, this does not decrease the weight. float deltaWeight = learningRate * derivative.magnitude;
// direction and sign are different.... // Compared to the 1D solution, this does not decrease the weight because magnitude is always positive
// Perhaps the sign is determine by the direction of the derivative and the neuron activation? // derivative.direction and derivative.sign are different....
// When they are oppositie, the sign is negative? (or the other way round...) if (AreOpposed(derivative, this.neuron.activation))
this.weight += learningRate * derivative.magnitude; 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;
}
} }
} }