Improvements

This commit is contained in:
Pascal Serrarens 2026-05-06 17:51:14 +02:00
parent 32b58852d4
commit da370bb701
2 changed files with 25 additions and 34 deletions

View File

@ -827,7 +827,7 @@ namespace NanoBrain {
} }
} }
} }
UpdateNuclei(); //UpdateNuclei();
} }
public override void UpdateStateIsolated() { public override void UpdateStateIsolated() {
@ -835,10 +835,10 @@ namespace NanoBrain {
} }
// Don't think this does anything anymore... // Don't think this does anything anymore...
public override void UpdateNuclei() { // public override void UpdateNuclei() {
foreach (Nucleus nucleus in this.nuclei) // foreach (Nucleus nucleus in this.nuclei)
nucleus.UpdateNuclei(); // nucleus.UpdateNuclei();
} // }
#endregion Update #endregion Update

View File

@ -117,8 +117,6 @@ namespace NanoBrain {
Sum, Sum,
/// Multiply the weighted values /// Multiply the weighted values
Product, Product,
/// Take the maximum of all the weighted values
Max,
} }
/// <summary> /// <summary>
/// The type of combinator used for this Neuron /// The type of combinator used for this Neuron
@ -377,20 +375,14 @@ namespace NanoBrain {
} }
public override void UpdateStateIsolated() { public override void UpdateStateIsolated() {
CheckSleepingSynapses();
var result = Combinator(); var result = Combinator();
this.outputValue = Activator(result); this.outputValue = ApplyActivator(result);
this.lastUpdate = Time.time; this.lastUpdate = Time.time;
// Debug.Log($"Update Neuron {this.name}");
} }
protected void CheckSleepingSynapses() { protected void CheckSleepingSynapses() {
foreach (Synapse synapse in this.synapses) { foreach (Synapse synapse in this.synapses)
synapse.neuron.SleepCheck(); synapse.neuron.SleepCheck();
// if (synapse.isSleeping) {
// synapse.neuron.outputValue = Vector3.zero;
// }
}
} }
#region Combinator #region Combinator
@ -400,42 +392,27 @@ namespace NanoBrain {
protected Func<float3> Combinator => combinator switch { protected Func<float3> Combinator => combinator switch {
CombinatorType.Sum => CombinatorSum, CombinatorType.Sum => CombinatorSum,
CombinatorType.Product => CombinatorProduct, CombinatorType.Product => CombinatorProduct,
CombinatorType.Max => CombinatorMax,
_ => CombinatorSum _ => CombinatorSum
}; };
public float3 CombinatorSum() { public float3 CombinatorSum() {
float3 sum = this.bias; float3 sum = this.bias;
foreach (Synapse synapse in this.synapses) foreach (Synapse synapse in this.synapses) {
synapse.neuron.SleepCheck();
sum += synapse.weight * synapse.neuron.outputValue; sum += synapse.weight * synapse.neuron.outputValue;
}
return sum; return sum;
} }
public float3 CombinatorProduct() { public float3 CombinatorProduct() {
float3 product = this.bias; float3 product = this.bias;
foreach (Synapse synapse in this.synapses) { foreach (Synapse synapse in this.synapses) {
synapse.neuron.SleepCheck();
product *= synapse.weight * synapse.neuron.outputValue; product *= synapse.weight * synapse.neuron.outputValue;
} }
return product; return product;
} }
public float3 CombinatorMax() {
float3 max = this.bias;
float maxLength = length(max);
//Applying the weight factors
foreach (Synapse synapse in this.synapses) {
float3 input = synapse.weight * synapse.neuron.outputValue;
float inputLength = length(input);
if (inputLength > maxLength) {
max = input;
maxLength = inputLength;
}
}
return max;
}
#else #else
protected Func<Vector3> Combinator => combinator switch { protected Func<Vector3> Combinator => combinator switch {
@ -484,6 +461,20 @@ namespace NanoBrain {
#if UNITY_MATHEMATICS #if UNITY_MATHEMATICS
// This does not allocate memory and seems faster than the solution below
float3 ApplyActivator(float3 x) {
switch (curvePreset) {
case ActivationType.Linear: return ActivatorLinear(x);
case ActivationType.Sqrt: return ActivatorSqrt(x);
case ActivationType.Power: return ActivatorPower(x);
case ActivationType.Reciprocal: return ActivatorReciprocal(x);
case ActivationType.Tanh: return ActivatorTanh(x);
case ActivationType.Binary: return ActivatorBinary(x);
case ActivationType.Normalized: return ActivatorNormalized(x);
default: return ActivatorCustom(x);
}
}
public Func<float3, float3> Activator => this.curvePreset switch { public Func<float3, float3> Activator => this.curvePreset switch {
ActivationType.Linear => ActivatorLinear, ActivationType.Linear => ActivatorLinear,
ActivationType.Sqrt => ActivatorSqrt, ActivationType.Sqrt => ActivatorSqrt,