Containment training added

This commit is contained in:
Pascal Serrarens 2026-08-07 09:55:07 +02:00
parent 0541a08c68
commit fb51cc8914
2 changed files with 53 additions and 37 deletions

View File

@ -695,15 +695,22 @@ namespace NanoBrain {
} }
public void BackPropagation3D(Vector3 derivative, float learningRate) { public void BackPropagation3D(Vector3 derivative, float learningRate) {
float derivativeMagnitude = derivative.magnitude;
if (derivativeMagnitude < 1e-03f)
// Small changes are not processed
// We are probably very close to the desired output
return;
foreach (Synapse synapse in this.synapses) foreach (Synapse synapse in this.synapses)
// synapse.BackPropagation(this, derivative, learningRate); synapse.BackPropagation3D(this, derivative, learningRate);
// As the weight cannot change the direction of the derivative // As the weight cannot change the direction of the derivative
// we can use the simpler, 1D backpropagation here // we can use the simpler, 1D backpropagation here
// But we still need to determine the sign of the derivative // But we still need to determine the sign of the derivative
if (Synapse.AreOpposed(derivative, synapse.neuron.activation))
synapse.BackPropagation(this, -derivative.magnitude, learningRate); // if (Synapse.AreOpposed(derivative, synapse.neuron.activation))
else // synapse.BackPropagation(this, -derivativeMagnitude, learningRate);
synapse.BackPropagation(this, derivative.magnitude, learningRate); // else
// synapse.BackPropagation(this, derivativeMagnitude, learningRate);
// Bias // Bias
if (this.trainableBias) { if (this.trainableBias) {

View File

@ -64,37 +64,46 @@ namespace NanoBrain {
} }
} }
public virtual void BackPropagation(Neuron receiver, Vector3 derivative, float learningRate) { public virtual void BackPropagation3D(Neuron receiver, Vector3 derivative, float learningRate) {
switch (receiver.activator) { // As the weight cannot change the direction of the derivative
case Neuron.ActivationType.Linear: // we can use the simpler, 1D backpropagation here
derivative *= 1; // But we still need to determine the sign of the derivative
break;
case Neuron.ActivationType.Power:
// untested
derivative *= 2 * math.length(this.neuron.combination);
break;
case Neuron.ActivationType.Reciprocal:
// untested
derivative *= -1 / Mathf.Pow(math.length(this.neuron.combination), 2);
break;
default:
Debug.Log("other activator");
break;
}
this.neuron.BackPropagation3D(derivative * this.weight, learningRate); if (Synapse.AreOpposed(derivative, this.neuron.activation))
BackPropagation(receiver, -derivative.magnitude, learningRate);
derivative *= math.length(this.neuron.activation);
if (this.trainable) {
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 else
this.weight += deltaWeight; BackPropagation(receiver, derivative.magnitude, learningRate);
}
// switch (receiver.activator) {
// case Neuron.ActivationType.Linear:
// derivative *= 1;
// break;
// case Neuron.ActivationType.Power:
// // untested
// derivative *= 2 * math.length(this.neuron.combination);
// break;
// case Neuron.ActivationType.Reciprocal:
// // untested
// derivative *= -1 / Mathf.Pow(math.length(this.neuron.combination), 2);
// break;
// default:
// Debug.Log("other activator");
// break;
// }
// this.neuron.BackPropagation3D(derivative * this.weight, learningRate);
// derivative *= math.length(this.neuron.activation);
// if (this.trainable) {
// 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) { public static bool AreOpposed(Vector3 a, Vector3 b) {