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++;
serializedObject.ApplyModifiedProperties();
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
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}]");
}
else {
@ -168,7 +168,7 @@ namespace NanoBrain {
continue;
// 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}");
}
}

View File

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

View File

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