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) {
if (synapse.trainable) {
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];
foreach (Nucleus nucleus in computeOrder) {
if (nucleus is not Cluster) {
nucleus.UpdateStateIsolated();
if (nucleus is Neuron neuron) {
foreach (Nucleus receiver in neuron.receivers) {
if (receiver.parent != this) {
//Debug.Log($" External: {receiver.parent.name}.{receiver.name}");
receiver.parent.UpdateFromNucleus(receiver);
}
if (nucleus is Cluster)
continue;
nucleus.UpdateStateIsolated();
if (nucleus is Neuron neuron) {
foreach (Nucleus receiver in neuron.receivers) {
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) {
ExternalClusterData clusterData = new(synapse.neuron.parent);
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);
//Debug.Log("Add cluster");
}

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
#if UNITY_MATHEMATICS
using Unity.Mathematics;
@ -237,7 +239,7 @@ namespace NanoBrain {
public Action WhenFiring;
public Action WhenNotFiring;
/// <summary>
/// When true, the value will not be reset after timeToSleep.
/// </summary>
@ -713,14 +715,14 @@ namespace NanoBrain {
foreach (Synapse synapse in this.synapses)
synapse.BackPropagation3D(this, derivative, learningRate);
// As the weight cannot change the direction of the derivative
// we can use the simpler, 1D backpropagation here
// But we still need to determine the sign of the derivative
// As the weight cannot change the direction of the derivative
// we can use the simpler, 1D backpropagation here
// But we still need to determine the sign of the derivative
// if (Synapse.AreOpposed(derivative, synapse.neuron.activation))
// synapse.BackPropagation(this, -derivativeMagnitude, learningRate);
// else
// synapse.BackPropagation(this, derivativeMagnitude, learningRate);
// if (Synapse.AreOpposed(derivative, synapse.neuron.activation))
// synapse.BackPropagation(this, -derivativeMagnitude, learningRate);
// else
// synapse.BackPropagation(this, derivativeMagnitude, learningRate);
// Bias
if (this.trainableBias) {
@ -739,14 +741,40 @@ namespace NanoBrain {
#endregion Back propagation
private CancellationTokenSource _cts;
/// <summary>
/// Process an external stimulus
/// </summary>
/// <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.bias = inputValue;
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);
}
}