Backpropagation over cluster part 1

This commit is contained in:
Pascal Serrarens 2026-07-01 17:17:36 +02:00
parent 06a7a6d1fa
commit fa9b3145f9

View File

@ -366,6 +366,49 @@ namespace NanoBrain {
thingClusters.Remove(thingId);
}
public List<Neuron> GetAllInstances(Neuron nucleus) {
List<Neuron> allInstances = new();
int ix = -1;
//foreach (Nucleus myNucleus in this.nuclei) {
for (; ix < this.nuclei.Count; ix++) {
if (this.nuclei[ix] == nucleus)
break;
}
if (ix == this.nuclei.Count)
return allInstances;
foreach (Cluster instance in this.instances) {
List<Nucleus> otherNuclei = instance.nuclei;
if (otherNuclei[ix] is Neuron otherNeuron)
allInstances.Add(otherNeuron);
}
return allInstances;
}
public void Backpropagation(Func<Cluster, Vector3> Observer, Vector3 target, float learningRate) {
foreach (Nucleus nucleus in this.instances[0].nuclei) {
if (nucleus is not Neuron neuron)
continue;
foreach (Synapse synapse in neuron.synapses) {
List<Neuron> allSynapseNeurons = GetAllInstances(synapse.neuron);
Vector3 dSSRdW = Vector3.zero;
for (int clusterIx = 0; clusterIx < this.instances.Length; clusterIx++) {
Cluster clusterInstance = this.instances[clusterIx];
Neuron neuronInstance = allSynapseNeurons[clusterIx];
// Simple case, without receivers...
dSSRdW += (Vector3)(-2 * (Observer(clusterInstance) - target) * neuronInstance.activation);
}
synapse.weight += learningRate * dSSRdW.magnitude;
}
}
}
#endregion ClusterArray
/// <summary>
@ -724,7 +767,7 @@ namespace NanoBrain {
foreach (Nucleus nucleus in this.nuclei.ToArray()) {
if (nucleus is not Cluster cluster)
continue;
List<Nucleus> receivers = cluster.CollectReceivers();
List<Nucleus> receivers = cluster.CollectReceivers();
Debug.Log($"cluster receiver count = {receivers.Count}");
if (receivers.Count == 0)
this.nuclei.Remove(nucleus);