auto stimulus reset

This commit is contained in:
Pascal Serrarens 2026-08-12 17:03:50 +02:00
parent a334d9ca4d
commit a4776f9dea
2 changed files with 48 additions and 19 deletions

View File

@ -392,7 +392,7 @@ namespace NanoBrain {
foreach (Synapse synapse in neuron.synapses) { foreach (Synapse synapse in neuron.synapses) {
if (synapse.trainable) { if (synapse.trainable) {
synapse.weight = (float)randomGenerator.NextDouble() * 2.0f - 1.0f; synapse.weight = (float)randomGenerator.NextDouble() * 2.0f - 1.0f;
Debug.Log($"{neuron.name}-{synapse.neuron.name} weight = {synapse.weight}"); // Debug.Log($"{neuron.name}-{synapse.neuron.name} weight = {synapse.weight}");
} }
} }
} }
@ -859,14 +859,15 @@ namespace NanoBrain {
List<Nucleus> computeOrder = this.computeOrders[startNucleus]; List<Nucleus> computeOrder = this.computeOrders[startNucleus];
foreach (Nucleus nucleus in computeOrder) { foreach (Nucleus nucleus in computeOrder) {
if (nucleus is not Cluster) { if (nucleus is Cluster)
nucleus.UpdateStateIsolated(); continue;
if (nucleus is Neuron neuron) {
foreach (Nucleus receiver in neuron.receivers) { nucleus.UpdateStateIsolated();
if (receiver.parent != this) { if (nucleus is Neuron neuron) {
//Debug.Log($" External: {receiver.parent.name}.{receiver.name}"); foreach (Nucleus receiver in neuron.receivers) {
receiver.parent.UpdateFromNucleus(receiver); if (receiver.parent != this) {
} //Debug.Log($" External: {receiver.parent.name}.{receiver.name}");
receiver.parent.UpdateFromNucleus(receiver);
} }
} }
} }
@ -940,7 +941,7 @@ namespace NanoBrain {
if (synapse.neuron.parent.name != cluster.name) { if (synapse.neuron.parent.name != cluster.name) {
ExternalClusterData clusterData = new(synapse.neuron.parent); ExternalClusterData clusterData = new(synapse.neuron.parent);
if (GetCluster(clusterData.name) == null) { if (GetCluster(clusterData.name) == null) {
//if (this.clusters.Find(data => data.name == clusterData.name) == null) { //if (this.clusters.Find(data => data.name == clusterData.name) == null) {
this.clusters.Add(clusterData); this.clusters.Add(clusterData);
//Debug.Log("Add cluster"); //Debug.Log("Add cluster");
} }

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
#if UNITY_MATHEMATICS #if UNITY_MATHEMATICS
using Unity.Mathematics; using Unity.Mathematics;
@ -237,7 +239,7 @@ namespace NanoBrain {
public Action WhenFiring; public Action WhenFiring;
public Action WhenNotFiring; public Action WhenNotFiring;
/// <summary> /// <summary>
/// When true, the value will not be reset after timeToSleep. /// When true, the value will not be reset after timeToSleep.
/// </summary> /// </summary>
@ -713,14 +715,14 @@ namespace NanoBrain {
foreach (Synapse synapse in this.synapses) foreach (Synapse synapse in this.synapses)
synapse.BackPropagation3D(this, derivative, learningRate); synapse.BackPropagation3D(this, derivative, learningRate);
// As the weight cannot change the direction of the derivative // As the weight cannot change the direction of the derivative
// we can use the simpler, 1D backpropagation here // we can use the simpler, 1D backpropagation here
// But we still need to determine the sign of the derivative // But we still need to determine the sign of the derivative
// if (Synapse.AreOpposed(derivative, synapse.neuron.activation)) // if (Synapse.AreOpposed(derivative, synapse.neuron.activation))
// synapse.BackPropagation(this, -derivativeMagnitude, learningRate); // synapse.BackPropagation(this, -derivativeMagnitude, learningRate);
// else // else
// synapse.BackPropagation(this, derivativeMagnitude, learningRate); // synapse.BackPropagation(this, derivativeMagnitude, learningRate);
// Bias // Bias
if (this.trainableBias) { if (this.trainableBias) {
@ -739,14 +741,40 @@ namespace NanoBrain {
#endregion Back propagation #endregion Back propagation
private CancellationTokenSource _cts;
/// <summary> /// <summary>
/// Process an external stimulus /// Process an external stimulus
/// </summary> /// </summary>
/// <param name="inputValue">The value of the stimulus</param> /// <param name="inputValue">The value of the stimulus</param>
public virtual void ProcessStimulus(Vector3 inputValue) { public virtual void ProcessStimulus(Vector3 inputValue, float autoResetDelay = 0) {
this.lastUpdate = Time.time; this.lastUpdate = Time.time;
this.bias = inputValue; this.bias = inputValue;
this.parent?.UpdateFromNucleus(this); this.parent?.UpdateFromNucleus(this);
if (autoResetDelay > 0) {
_cts?.Cancel();
_cts?.Dispose();
_cts = new CancellationTokenSource();
_ = CallResetAfterDelayAsync(_cts.Token, autoResetDelay);
}
}
private async Task CallResetAfterDelayAsync(CancellationToken token, float autoResetDelay) {
try {
await Task.Delay(TimeSpan.FromSeconds(autoResetDelay), token);
if (!token.IsCancellationRequested)
ResetStimulus();
}
catch (TaskCanceledException) {
// Expected when Stimulus is called again; do nothing
}
}
protected async void ResetStimulus() {
//Debug.Log("reset stimulus");
this.bias = Vector3.zero;
this.parent?.UpdateFromNucleus(this);
} }
} }