From fa9b3145f9eedef1df73b6a8ba4b1fb2b706263f Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 1 Jul 2026 17:17:36 +0200 Subject: [PATCH] Backpropagation over cluster part 1 --- Runtime/Scripts/Core/Cluster.cs | 45 ++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index 18726f0..7dcfafb 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -366,6 +366,49 @@ namespace NanoBrain { thingClusters.Remove(thingId); } + + public List GetAllInstances(Neuron nucleus) { + List 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 otherNuclei = instance.nuclei; + if (otherNuclei[ix] is Neuron otherNeuron) + allInstances.Add(otherNeuron); + } + return allInstances; + } + + public void Backpropagation(Func 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 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 /// @@ -724,7 +767,7 @@ namespace NanoBrain { foreach (Nucleus nucleus in this.nuclei.ToArray()) { if (nucleus is not Cluster cluster) continue; - List receivers = cluster.CollectReceivers(); + List receivers = cluster.CollectReceivers(); Debug.Log($"cluster receiver count = {receivers.Count}"); if (receivers.Count == 0) this.nuclei.Remove(nucleus);