Simple example with cluster

This commit is contained in:
Pascal Serrarens 2026-07-03 11:31:08 +02:00
parent 9568cdfcfb
commit c16c3c5dc9
4 changed files with 28 additions and 24 deletions

View File

@ -57,7 +57,7 @@ namespace NanoBrain.Unity {
versionProp.intValue++; versionProp.intValue++;
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(target); EditorUtility.SetDirty(target);
Debug.Log($"{name} Prefab changed, version {versionProp.intValue}"); // Debug.Log($"{name} Prefab changed, version {versionProp.intValue}");
} }
} }

View File

@ -157,7 +157,7 @@ namespace NanoBrain {
// Could not find the neuron in the cloned cluster // Could not find the neuron in the cloned cluster
continue; continue;
clonedSender.AddReceiver(clonedNeuron, prefabSynapse.weight); clonedSender.AddReceiver(clonedNeuron, prefabSynapse.weight, prefabSynapse.trainable);
//Debug.Log($"Add synapse {clonedCluster.name}.{clonedSender.name} -> {clonedNeuron.name} [{clonedSender.receivers.Count}]"); //Debug.Log($"Add synapse {clonedCluster.name}.{clonedSender.name} -> {clonedNeuron.name} [{clonedSender.receivers.Count}]");
} }
else { else {
@ -168,7 +168,7 @@ namespace NanoBrain {
continue; continue;
// Copy the receivers which will also create the synapse // Copy the receivers which will also create the synapse
clonedSender.AddReceiver(clonedNeuron, prefabSynapse.weight); clonedSender.AddReceiver(clonedNeuron, prefabSynapse.weight, prefabSynapse.trainable);
// Debug.Log($"Add synapse {clonedSender.name} -> {clonedNeuron.name}"); // Debug.Log($"Add synapse {clonedSender.name} -> {clonedNeuron.name}");
} }
} }

View File

@ -63,8 +63,10 @@ namespace NanoBrain {
/// <param name="weight">The weight applied to the input. Default value = 1</param> /// <param name="weight">The weight applied to the input. Default value = 1</param>
/// <returns>The created Synapse</returns> /// <returns>The created Synapse</returns>
/// This will add a new input to this nucleus with the given weight. /// This will add a new input to this nucleus with the given weight.
public Synapse AddSynapse(Neuron sendingNucleus, float weight = 1) { public Synapse AddSynapse(Neuron sendingNucleus, float weight = 1, bool trainable = false) {
Synapse synapse = new(sendingNucleus, weight); Synapse synapse = new(sendingNucleus, weight) {
trainable = trainable
};
this.synapses.Add(synapse); this.synapses.Add(synapse);
return synapse; return synapse;
} }
@ -627,11 +629,11 @@ namespace NanoBrain {
/// </summary> /// </summary>
/// <param name="receiverToAdd">The receiver to add</param> /// <param name="receiverToAdd">The receiver to add</param>
/// <param name="weight">The weight to use for the synapse to his neuron</param> /// <param name="weight">The weight to use for the synapse to his neuron</param>
public virtual void AddReceiver(Nucleus receiverToAdd, float weight = 1) { public virtual void AddReceiver(Nucleus receiverToAdd, float weight = 1, bool trainable = false) {
if (receiverToAdd is not Neuron receiverNeuron) if (receiverToAdd is not Neuron receiverNeuron)
return; return;
this._receivers.Add(receiverNeuron); this._receivers.Add(receiverNeuron);
receiverNeuron.AddSynapse(this, weight); receiverNeuron.AddSynapse(this, weight, trainable);
//Debug.Log($"Add synapse {this.clusterPrefab.name}.{this.name} -> {receiverToAdd.name} --- [{this.receivers.Count}]"); //Debug.Log($"Add synapse {this.clusterPrefab.name}.{this.name} -> {receiverToAdd.name} --- [{this.receivers.Count}]");
} }
@ -762,25 +764,26 @@ namespace NanoBrain {
this.bias -= stepSize; this.bias -= stepSize;
foreach (Synapse synapse in this.synapses) { foreach (Synapse synapse in this.synapses) {
// derivative for the weight? synapse.BackPropagation(length(derivative), learningRate);
float3 deltaSynapse = derivative; // dSSR/dActivator // // derivative for the weight?
// float3 deltaSynapse = derivative; // dSSR/dActivator
// derivative for the activator // // derivative for the activator
// dActivator/dCombinator // // dActivator/dCombinator
switch (activator) { // switch (activator) {
case ActivationType.Linear: // case ActivationType.Linear:
//deltaSynapse *= 1; // //deltaSynapse *= 1;
break; // break;
default: // default:
break; // break;
} // }
// derivative for the previous activation // // derivative for the previous activation
// dCombinator/dWeight // // dCombinator/dWeight
deltaSynapse *= synapse.neuron.activation; // deltaSynapse *= synapse.neuron.activation;
float deltaWeight = length(deltaSynapse); // float deltaWeight = length(deltaSynapse);
synapse.weight += learningRate * deltaWeight; // synapse.weight += learningRate * deltaWeight;
BackPropagation2(derivative * synapse.weight, learningRate); BackPropagation2(derivative * synapse.weight, learningRate);
} }

View File

@ -13,6 +13,7 @@ namespace NanoBrain {
/// The neuron from which input is received /// The neuron from which input is received
/// </summary> /// </summary>
[SerializeReference] [SerializeReference]
[HideInInspector]
public Neuron neuron; public Neuron neuron;
/// <summary> /// <summary>
@ -32,7 +33,7 @@ namespace NanoBrain {
this.weight = weight; this.weight = weight;
} }
public virtual void BasicBackPropagation(float error, float learningRate) { public virtual void BackPropagation(float error, float learningRate) {
float derivative = error; float derivative = error;
switch (neuron.activator) { switch (neuron.activator) {
case Neuron.ActivationType.Linear: case Neuron.ActivationType.Linear: