Added Tanh Activation

This commit is contained in:
Pascal Serrarens 2026-04-15 09:50:36 +02:00
parent a99d40c5c9
commit 1c7b8e7940
3 changed files with 1133 additions and 504 deletions

View File

@ -7,6 +7,599 @@ using UnityEngine.UIElements;
namespace NanoBrain { namespace NanoBrain {
[CustomEditor(typeof(ClusterPrefab))]
public class ClusterInspector : ClusterViewer {
public override VisualElement CreateInspectorGUI() {
ClusterPrefab prefab = target as ClusterPrefab;
if (prefab != null)
prefab.EnsureInitialization();
serializedObject.Update();
VisualElement root = new();
CreateInspector(root, prefab, prefab.output, null);
serializedObject.ApplyModifiedProperties();
return root;
}
public static GraphView CreateInspector(VisualElement root, ClusterPrefab cluster, Nucleus output, GameObject gameObject) {
root.style.paddingLeft = 0;
root.style.paddingRight = 0;
root.style.paddingTop = 0;
root.style.paddingBottom = 0;
root.styleSheets.Add(Resources.Load<StyleSheet>("GraphStyles"));
VisualElement mainContainer = new() {
style = {
flexDirection = FlexDirection.Row
}
};
GraphEditor graph = new(cluster);
graph.style.flexGrow = 1;
VisualElement inspectorContainer = new() {
name = "inspector",
style = {
alignSelf = Align.Stretch,
minHeight = 450,
width = 300,
flexGrow = 0
}
};
mainContainer.Add(graph);
mainContainer.Add(inspectorContainer);
root.Add(mainContainer);
graph.SetGraph(gameObject, output, inspectorContainer);
return graph;
}
public class GraphEditor : GraphView {
public GraphEditor(ClusterPrefab prefab) : base(prefab) {
Button addButton = new(() => OnAddClusterOutput()) {
text = "Add"
};
outputContainer.Add(addButton);
Add(outputContainer);
}
void OnAddClusterOutput() {
Nucleus newOutput = new Neuron(this.prefab, "New Output");
this.prefab.RefreshOutputs();
outputsField.choices = this.prefab.outputs.Select(output => output.name).ToList();
outputsField.value = newOutput.name;
this.currentNucleus = newOutput;
}
public void SetGraph(GameObject gameObject, Nucleus nucleus, VisualElement inspectorContainer) {
this.gameObject = gameObject;
//this.cluster = brain;
if (Application.isPlaying == false)
this.serializedBrain = new SerializedObject(this.prefab);
this.currentNucleus = nucleus;
Rebuild(inspectorContainer);
}
void Rebuild(VisualElement inspectorContainer) {
BuildLayers();
if (this.currentNucleus == null) {
inspectorContainer.Clear();
return;
}
string path = AssetDatabase.GetAssetPath(this.prefab); // or known path
this.prefabAsset = AssetDatabase.LoadAssetAtPath<ClusterPrefab>(path);
if (this.prefabAsset == null) {
// create in memory save if it doesn't exist
this.prefabAsset = CreateInstance<ClusterPrefab>();
//Debug.LogError("Cluster Prefab is not found on disk");
}
DrawInspector(inspectorContainer);
}
#region Inspector
private VisualElement inspectorIMGUIContainer;
private bool showSynapses = true;
private bool showActivation = true;
protected bool breakOnWake = false;
protected bool trace = false;
void DrawInspector(VisualElement inspectorContainer) {
if (inspectorContainer == null)
return;
inspectorContainer.Clear();
if (this.currentNucleus == null)
return;
// create a SerializedObject wrapper so Unity inspector controls work (and Undo)
SerializedObject so = new(prefabAsset);
this.inspectorIMGUIContainer = new IMGUIContainer(() => InspectorHandler(so));
inspectorContainer.Add(inspectorIMGUIContainer);
}
void InspectorHandler(SerializedObject serializedObject) {
bool anythingChanged = false;
if (serializedObject == null || serializedObject.targetObject == null)
return;
if (this.currentNucleus == null)
return;
serializedObject.Update();
GUIStyle headerStyle = new(EditorStyles.boldLabel) {
alignment = TextAnchor.MiddleLeft,
margin = new RectOffset(10, 0, 4, 4)
};
GUIStyle boldTextFieldStyle = new(EditorStyles.textField) {
fontStyle = FontStyle.Bold
};
GUILayout.Label(this.currentNucleus.GetType().ToString(), headerStyle);
string newName = EditorGUILayout.TextField(this.currentNucleus.name, boldTextFieldStyle);
if (newName != this.currentNucleus.name) {
this.currentNucleus.name = newName;
this.prefab.RefreshOutputs();
outputsField.choices = this.prefab.outputs.Select(output => output.name).ToList();
anythingChanged = true;
}
if (Application.isPlaying) {
if (currentNucleus is Neuron currentNeuron1) {
GUIContent nameLabel = new("Output", currentNeuron1.outputValue.ToString());
EditorGUILayout.FloatField(nameLabel, currentNeuron1.outputMagnitude);
}
else
EditorGUILayout.LabelField(" ");
}
else
EditorGUILayout.LabelField(" ");
if (this.currentNucleus is MemoryCell memory) {
memory.staticMemory = EditorGUILayout.Toggle("Static Memory", memory.staticMemory);
}
if (this.currentNucleus is IReceptor receptor1) {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.IntField("Array size", receptor1.nucleiArray.Count());
if (GUILayout.Button("Add")) {
Undo.RecordObject(prefabAsset, "Array add " + prefabAsset.name);
receptor1.AddReceptorElement(this.prefab);
anythingChanged = true;
}
if (GUILayout.Button("Del")) {
Undo.RecordObject(prefabAsset, "Array delete " + prefabAsset.name);
receptor1.RemoveReceptorElement();
anythingChanged = true;
}
EditorGUILayout.EndHorizontal();
}
// Synapses
if (this.currentNucleus is not Receptor && this.currentNucleus is not ClusterReceptor) {
showSynapses = EditorGUILayout.BeginFoldoutHeaderGroup(showSynapses, "Synapses");
if (showSynapses) {
if (this.currentNucleus is Neuron neuron2) {
Neuron.CombinatorType newCombinator = (Neuron.CombinatorType)EditorGUILayout.EnumPopup("Combinator", neuron2.combinator);
anythingChanged |= newCombinator != neuron2.combinator;
neuron2.combinator = newCombinator;
}
EditorGUIUtility.wideMode = true;
EditorGUIUtility.labelWidth = 100;
Vector3 newBias = EditorGUILayout.Vector3Field("Bias", this.currentNucleus.bias);
anythingChanged |= newBias != this.currentNucleus.bias;
this.currentNucleus.bias = newBias;
Nucleus[] array = null;
int elementIx = -1;
if (this.currentNucleus.synapses.Count > 0) {
Synapse[] synapses = this.currentNucleus.synapses.ToArray();
foreach (Synapse synapse in synapses) {
if (synapse.neuron == null)
continue;
if (array != null) {
if (synapse.neuron.parent is Cluster iCluster && elementIx > 0) {
int thisElementIx = Cluster.GetNucleusIndex(iCluster.clusterNuclei, synapse.neuron);
if (thisElementIx == elementIx)
continue;
else
elementIx = thisElementIx;
}
// if (array.Contains(synapse.nucleus))
// continue;
else if (array.Contains(synapse.neuron.parent))
continue;
}
else {
if (synapse.neuron.parent is IReceptor iReceptor) {
array = iReceptor.nucleiArray;
if (iReceptor is Cluster iCluster)
elementIx = Cluster.GetNucleusIndex(iCluster.clusterNuclei, synapse.neuron);
}
// else if (synapse.nucleus is Receptor receptor2) // && receptor2.array != null && receptor2.array.nuclei.Length > 1)
// array = receptor2.nucleiArray;
}
EditorGUILayout.Space();
if (Application.isPlaying) {
if (synapse.neuron is Neuron synapseNeuron) {
Vector3 value = synapseNeuron.outputValue * synapse.weight;
GUIContent synapseValueLabel = new(synapse.neuron.name, synapseNeuron.outputValue.ToString());
EditorGUILayout.FloatField(synapseValueLabel, synapseNeuron.outputMagnitude);
}
}
else {
EditorGUILayout.BeginHorizontal();
if (synapse.neuron.parent != null && synapse.neuron.parent != this.currentNucleus) {
// If it is a cluster
GUIStyle labelStyle = new(GUI.skin.label);
float labelWidth = 200;
if (synapse.neuron.clusterPrefab != null) {
labelWidth = labelStyle.CalcSize(new GUIContent($"{synapse.neuron.parent.baseName}.")).x;
GUILayout.Label($"{synapse.neuron.parent.baseName}", GUILayout.Width(labelWidth));
}
string[] options = synapse.neuron.parent.clusterNuclei.Select(n => n.name).ToArray();
int selectedIndex = System.Array.IndexOf(options, synapse.neuron.name);
int newIndex = EditorGUILayout.Popup(selectedIndex, options);
if (newIndex != selectedIndex && synapse.neuron.parent.clusterNuclei[newIndex] is Neuron newNeuron)
ChangeSynapse(synapse, newNeuron);
}
else
GUILayout.Label(synapse.neuron.name);
bool disconnecting = GUILayout.Button("Disconnect", GUILayout.Width(80));
if (disconnecting && synapse.neuron is Neuron synapseNeuron) {
synapseNeuron.RemoveReceiver(this.currentNucleus);
this.prefab.GarbageCollection();
anythingChanged = true;
}
EditorGUILayout.EndHorizontal();
}
EditorGUI.indentLevel++;
float newWeight = EditorGUILayout.FloatField("Weight", synapse.weight);
if (newWeight != synapse.weight) {
if (synapse.neuron.parent is IReceptor receptor) {
Nucleus[] receptorArray = receptor.nucleiArray;
foreach (Synapse s in this.currentNucleus.synapses) {
if (s.neuron.parent is IReceptor r && r.nucleiArray == receptorArray)
s.weight = newWeight;
}
}
else
synapse.weight = newWeight;
anythingChanged = true;
}
EditorGUI.indentLevel--;
}
}
EditorGUILayout.Space();
anythingChanged |= ConnectNucleus(this.prefab, this.currentNucleus);
anythingChanged |= AddSynapse(this.prefab, this.currentNucleus);
}
EditorGUILayout.EndFoldoutHeaderGroup();
}
// Activation
if (this.currentNucleus is not Cluster) {
EditorGUILayout.Space();
showActivation = EditorGUILayout.BeginFoldoutHeaderGroup(showActivation, "Activation");
if (showActivation) {
if (this.currentNucleus is Neuron neuron) {
if (this.currentNucleus is not MemoryCell) {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150));
if (neuron.curveMax > 0)
EditorGUILayout.CurveField(neuron.curve, Color.cyan, new Rect(0, 0, 1, neuron.curveMax));
else
EditorGUILayout.CurveField(neuron.curve, Color.cyan, new Rect(0, neuron.curveMax, 1, -neuron.curveMax));
Neuron.ActivationFunction newPreset = (Neuron.ActivationFunction)EditorGUILayout.EnumPopup(neuron.curvePreset, GUILayout.Width(100));
anythingChanged |= newPreset != neuron.curvePreset;
neuron.curvePreset = newPreset;
EditorGUILayout.EndHorizontal();
}
if (neuron is Receptor receptor2) {
if (receptor2.nucleiArray == null || receptor2.nucleiArray.Count() == 0)
receptor2.array = new NucleusArray(neuron);
}
}
EditorGUILayout.Space();
}
EditorGUILayout.EndFoldoutHeaderGroup();
}
if (GUILayout.Button("Delete this neuron"))
DeleteNucleus(this.currentNucleus);
if (this.currentNucleus is Cluster subCluster) {
if (GUILayout.Button("Edit Cluster"))
EditCluster(subCluster);
}
EditorGUILayout.Space();
breakOnWake = EditorGUILayout.Toggle("Break on wake", breakOnWake);
if (breakOnWake && this.currentNucleus is Neuron currentNeuron) {
if (currentNeuron.isSleeping == false)
Debug.Break();
}
trace = EditorGUILayout.Toggle("Trace", trace);
this.currentNucleus.trace = trace;
serializedObject.ApplyModifiedProperties();
if (anythingChanged) {
EditorUtility.SetDirty(prefabAsset);
AssetDatabase.SaveAssets();
}
}
void OnSceneGUI(SceneView sceneView) {
if (this.gameObject != null) {
if (this.currentNucleus is IReceptor receptor) {
foreach (Nucleus nucleus in receptor.nucleiArray) {
if (nucleus is Neuron neuron) {
Vector3 worldVector = this.gameObject.transform.TransformVector(neuron.outputValue);
Handles.color = Color.yellow;
Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector);
}
}
}
else {
if (this.currentNucleus is Neuron currentNeuron) {
Vector3 worldVector = this.gameObject.transform.TransformVector(currentNeuron.outputValue);
Handles.color = Color.yellow;
Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector);
}
}
}
}
#region Synapses
protected virtual void AddInput(Nucleus.Type selectedType, Nucleus nucleus) {
switch (selectedType) {
case Nucleus.Type.Neuron:
AddNeuronInput(nucleus);
break;
case Nucleus.Type.MemoryCell:
AddMemoryCellInput(nucleus);
break;
// case Nucleus.Type.Selector:
// AddSelectorInput(nucleus);
// break;
case Nucleus.Type.Cluster:
AddClusterInput(nucleus);
break;
// case Nucleus.Type.Pulsar:
// AddPulsarInput(nucleus);
// break;
case Nucleus.Type.Receptor:
AddReceptorInput(nucleus);
break;
// case Nucleus.Type.ReceptorArray:
// AddReceptorArrayInput(nucleus);
// break;
case Nucleus.Type.ClusterReceptor:
AddClusterReceptorInput(nucleus);
break;
default:
break;
}
}
protected virtual void AddNeuronInput(Nucleus nucleus) {
Neuron newNeuroid = new(this.prefab, "New neuron");
newNeuroid.AddReceiver(nucleus);
this.currentNucleus = newNeuroid;
BuildLayers();
}
protected virtual void AddMemoryCellInput(Nucleus nucleus) {
MemoryCell newMemory = new(this.prefab, "New memory cell");
newMemory.AddReceiver(nucleus);
this.currentNucleus = newMemory;
BuildLayers();
}
protected virtual void AddClusterInput(Nucleus nucleus) {
ClusterPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster");
}
private void OnClusterPicked(Nucleus nucleus, ClusterPrefab prefab) {
Cluster subclusterInstance = new(prefab, this.prefab);
subclusterInstance.defaultOutput.AddReceiver(nucleus);
}
protected virtual void AddReceptorInput(Nucleus nucleus) {
Receptor newReceptor = new(this.prefab, "New Receptor");
newReceptor.AddReceiver(nucleus);
this.currentNucleus = newReceptor;
BuildLayers();
}
protected virtual void AddClusterReceptorInput(Nucleus nucleus) {
ClusterPickerWindow.ShowPicker(prefab => OnClusterReceptorPicked(nucleus, prefab), "Select Cluster");
}
private void OnClusterReceptorPicked(Nucleus nucleus, ClusterPrefab selectedPrefab) {
ClusterReceptor clusterInstance = new(selectedPrefab, this.prefab, "New " + selectedPrefab.name);
clusterInstance.defaultOutput.AddReceiver(nucleus);
this.currentNucleus = clusterInstance;
BuildLayers();
}
private void EditCluster(Cluster subCluster) {
// May be used with storedPrefab...
Selection.activeObject = subCluster.prefab;
EditorGUIUtility.PingObject(subCluster.prefab);
var editor = Editor.CreateEditor(subCluster.prefab);
}
int selectedConnectNucleus = -1;
// Connect to another nucleus in the same cluster
protected virtual bool ConnectNucleus(ClusterPrefab cluster, Nucleus nucleusToConnect) {
if (cluster == null)
return false;
IEnumerable<Nucleus> synapseNuclei = this.currentNucleus.synapses
.Where(synapse => synapse.neuron != null)
.Select(synapse => synapse.neuron);
IEnumerable<Nucleus> nuclei = cluster.nuclei
.Except(synapseNuclei);
IEnumerable<string> nucleiNames = nuclei
.Select(n => {
int idx = n.name.IndexOf(':');
return idx < 0 ? n.name : n.name[..idx];
})
.Distinct();
string[] names = nucleiNames.ToArray();
EditorGUILayout.BeginHorizontal();
selectedConnectNucleus = EditorGUILayout.Popup(selectedConnectNucleus, names);
bool connecting = GUILayout.Button("Connect", GUILayout.Width(80));
EditorGUILayout.EndHorizontal();
if (connecting) {
Nucleus nucleus = nuclei.ElementAt(selectedConnectNucleus);
if (nucleus is IReceptor receptor)
receptor.AddArrayReceiver(this.currentNucleus);
else if (nucleus is Neuron neuron)
neuron.AddReceiver(this.currentNucleus);
else if (nucleus is Cluster subCluster)
subCluster.defaultOutput.AddReceiver(this.currentNucleus);
}
return connecting;
}
protected virtual void DeleteNucleus(Nucleus nucleus) {
if (nucleus == null)
return;
if (nucleus is Neuron neuron) {
foreach (Nucleus receiver in neuron.receivers) {
if (receiver != null) {
this.currentNucleus = receiver;
break;
}
}
}
this.prefab.nuclei.Remove(nucleus);
if (outputsField.value == nucleus.name) {
this.prefab.RefreshOutputs();
outputsField.choices = this.prefab.outputs.Select(output => output.name).ToList();
outputsField.index = 0;
}
Neuron.Delete(nucleus);
this.currentNucleus = this.prefab.output;
BuildLayers();
}
Nucleus.Type selectedType = Nucleus.Type.None;
protected virtual bool AddSynapse(ClusterPrefab cluster, Nucleus nucleus) {
if (cluster == null)
return false;
EditorGUILayout.BeginHorizontal();
selectedType = (Nucleus.Type)EditorGUILayout.EnumPopup(selectedType);
bool connecting = GUILayout.Button("Add", GUILayout.Width(80));
EditorGUILayout.EndHorizontal();
if (connecting) {
AddInput(selectedType, this.currentNucleus);
}
return connecting;
// if (selectedType == Nucleus.Type.None)
// return false;
// AddInput(selectedType, this.currentNucleus);
// return true;
}
protected virtual void ChangeSynapse(Synapse synapse, Neuron newNucleus) {
Neuron synapseNeuron = synapse.neuron as Neuron;
if (synapse.neuron.parent is Cluster subCluster && subCluster.prefab != this.prefab) {
if (synapse.neuron.parent is ClusterReceptor receptor) {
// the new nucleus is part of a (cluster) receptor,
// so we have to change all synapses to this nucleus array elements
int oldNucleusIx = Cluster.GetNucleusIndex(subCluster.clusterNuclei, synapse.neuron);
int newNucleusIx = Cluster.GetNucleusIndex(subCluster.clusterNuclei, newNucleus);
foreach (Nucleus element in receptor.nucleiArray) {
if (element is not ClusterReceptor clusterReceptor)
continue;
// Get the same neuron as the synapse.nucleus in a different element
// of the ClusterReceptor array
Nucleus oldElementNucleus = clusterReceptor.clusterNuclei[oldNucleusIx];
if (oldElementNucleus is not Neuron oldElementNeuron)
continue;
// Get the same neuron as newNucleus in a different element
// of the ClusterReceptor array
Nucleus newElementNucleus = clusterReceptor.clusterNuclei[newNucleusIx];
if (newElementNucleus is not Neuron newElementNeuron)
continue;
oldElementNeuron.RemoveReceiver(this.currentNucleus);
newElementNeuron.AddReceiver(this.currentNucleus);
// Now find the synapse which pointed to the old Neuron
// Synapse synapseForUpdate = this.currentNucleus.GetSynapse(oldElementNeuron);
// synapseForUpdate.nucleus = newElementNeuron;
}
}
else {
// it is a neuron in a subcluster
synapseNeuron.RemoveReceiver(this.currentNucleus);
newNucleus.AddReceiver(this.currentNucleus);
}
}
else {
synapseNeuron.RemoveReceiver(this.currentNucleus);
newNucleus.AddReceiver(this.currentNucleus);
}
}
protected virtual void DisconnectNucleus(Neuron nucleus) {
if (this.currentNucleus.clusterPrefab == null)
return;
string[] names = this.currentNucleus.synapses.Select(synapse => synapse.neuron.name).ToArray();
int selectedIndex = -1;
selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names);
if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.clusterPrefab.nuclei.Count) {
Synapse synapse = this.currentNucleus.synapses[selectedIndex];
Neuron synapseNeuron = synapse.neuron as Neuron;
synapseNeuron.RemoveReceiver(this.currentNucleus);
}
}
#endregion Synapses
#endregion Inspector
}
}
/*
[CustomEditor(typeof(ClusterPrefab))] [CustomEditor(typeof(ClusterPrefab))]
public class ClusterInspector : Editor { public class ClusterInspector : Editor {
@ -1072,5 +1665,5 @@ namespace NanoBrain {
public int ix = 0; public int ix = 0;
public List<Nucleus> neuroids = new(); public List<Nucleus> neuroids = new();
} }
*/
} }

View File

@ -10,16 +10,17 @@ namespace NanoBrain {
public class ClusterViewer : Editor { public class ClusterViewer : Editor {
public class GraphView : VisualElement { public class GraphView : VisualElement {
readonly ClusterPrefab prefab; protected readonly ClusterPrefab prefab;
SerializedObject serializedBrain; protected SerializedObject serializedBrain;
Nucleus currentNucleus; protected Nucleus currentNucleus;
GameObject gameObject; protected GameObject gameObject;
private List<NeuroidLayer> layers = new(); private List<NeuroidLayer> layers = new();
private readonly Dictionary<Nucleus, Vector2Int> neuroidPositions = new(); private readonly Dictionary<Nucleus, Vector2Int> neuroidPositions = new();
private bool expandArray = false; private bool expandArray = false;
ClusterPrefab prefabAsset; protected ClusterPrefab prefabAsset;
readonly PopupField<string> outputsField; protected VisualElement outputContainer;
protected readonly PopupField<string> outputsField;
public GraphView(ClusterPrefab prefab) { public GraphView(ClusterPrefab prefab) {
this.prefab = prefab; this.prefab = prefab;
@ -35,7 +36,7 @@ namespace NanoBrain {
graphContainer.focusable = true; graphContainer.focusable = true;
Add(graphContainer); Add(graphContainer);
VisualElement outputContainer = new() { outputContainer = new() {
style = { style = {
flexDirection = FlexDirection.Row, flexDirection = FlexDirection.Row,
alignItems = Align.Center, alignItems = Align.Center,
@ -108,7 +109,7 @@ namespace NanoBrain {
//DrawInspector(inspectorContainer); //DrawInspector(inspectorContainer);
} }
private void BuildLayers() { protected void BuildLayers() {
// A temporary list to track what's been added to layers // A temporary list to track what's been added to layers
this.layers = new(); this.layers = new();
int layerIx = 0; int layerIx = 0;
@ -534,4 +535,9 @@ namespace NanoBrain {
} }
} }
public class NeuroidLayer {
public int ix = 0;
public List<Nucleus> neuroids = new();
}
} }

View File

@ -61,16 +61,17 @@ namespace NanoBrain {
/// <summary> /// <summary>
/// The type of /// The type of
/// </summary> /// </summary>
public enum CurvePresets { public enum ActivationFunction {
Linear, Linear,
Power, Power,
Sqrt, Sqrt,
Reciprocal, Reciprocal,
Tanh,
Custom Custom
} }
[SerializeField] [SerializeField]
public CurvePresets _curvePreset; public ActivationFunction _curvePreset;
public CurvePresets curvePreset { public ActivationFunction curvePreset {
get { return _curvePreset; } get { return _curvePreset; }
set { set {
_curvePreset = value; _curvePreset = value;
@ -82,18 +83,21 @@ namespace NanoBrain {
public AnimationCurve GenerateCurve() { public AnimationCurve GenerateCurve() {
switch (this.curvePreset) { switch (this.curvePreset) {
case CurvePresets.Linear: case ActivationFunction.Linear:
this.curveMax = 1; this.curveMax = 1;
return Presets.Linear(1); return Presets.Linear(1);
case CurvePresets.Power: case ActivationFunction.Power:
this.curveMax = 1; this.curveMax = 1;
return Presets.Power(2.0f, 1); return Presets.Power(2.0f, 1);
case CurvePresets.Sqrt: case ActivationFunction.Sqrt:
this.curveMax = 1; this.curveMax = 1;
return Presets.Power(0.5f, 1); return Presets.Power(0.5f, 1);
case CurvePresets.Reciprocal: case ActivationFunction.Reciprocal:
this.curveMax = 1 / 0.01f * 1; this.curveMax = 1 / 0.01f * 1;
return Presets.Reciprocal(1); return Presets.Reciprocal(1);
case ActivationFunction.Tanh:
this.curveMax = 1;
return Presets.Tanh(1);
default: default:
this.curveMax = 1; this.curveMax = 1;
return this.curve; return this.curve;
@ -142,6 +146,25 @@ namespace NanoBrain {
} }
return curve; return curve;
} }
public static AnimationCurve Tanh(float weight) {
//int samples = 128;
float xMin = 0.001f;
float xMax = 1;
var keys = new Keyframe[samples];
for (int i = 0; i < samples; i++) {
float t = i / (float)(samples - 1);
float x = Mathf.Lerp(xMin, xMax, t);
float y = MathF.Tanh(x * weight);
keys[i] = new Keyframe(x, y);
}
var curve = new AnimationCurve(keys);
for (int i = 0; i < curve.length; i++) {
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
}
return curve;
}
} }
#endregion Serialization #endregion Serialization
@ -348,10 +371,11 @@ namespace NanoBrain {
#if UNITY_MATHEMATICS #if UNITY_MATHEMATICS
public Func<float3, float3> Activator => this.curvePreset switch { public Func<float3, float3> Activator => this.curvePreset switch {
CurvePresets.Linear => ActivatorLinear, ActivationFunction.Linear => ActivatorLinear,
CurvePresets.Sqrt => ActivatorSqrt, ActivationFunction.Sqrt => ActivatorSqrt,
CurvePresets.Power => ActivatorPower, ActivationFunction.Power => ActivatorPower,
CurvePresets.Reciprocal => ActivatorReciprocal, ActivationFunction.Reciprocal => ActivatorReciprocal,
ActivationFunction.Tanh => ActivatorTanh,
_ => ActivatorCustom _ => ActivatorCustom
}; };
@ -378,6 +402,12 @@ namespace NanoBrain {
return result; return result;
} }
protected float3 ActivatorTanh(float3 input) {
float magnitude = length(input);
float3 result = normalize(input) * MathF.Tanh(magnitude);
return result;
}
protected float3 ActivatorCustom(float3 input) { protected float3 ActivatorCustom(float3 input) {
float activatedValue = this.curve.Evaluate(length(input)); float activatedValue = this.curve.Evaluate(length(input));
float3 result = normalize(input) * activatedValue; float3 result = normalize(input) * activatedValue;