Add trivial mutation
This commit is contained in:
parent
1f7c3ef7e8
commit
8cb6b979bf
@ -401,6 +401,21 @@ namespace NanoBrain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void GaussianAdditiveMutation(float sigma) {
|
||||||
|
foreach (Nucleus nucleus in this.nuclei) {
|
||||||
|
if (nucleus is Neuron neuron) {
|
||||||
|
foreach (Synapse synapse in neuron.synapses) {
|
||||||
|
if (synapse.trainable) {
|
||||||
|
synapse.GaussianAdditiveMutation(sigma);
|
||||||
|
//synapse.weight = (float)randomGenerator.NextDouble() * 2.0f - 1.0f;
|
||||||
|
Debug.Log($"{neuron.name}-{synapse.neuron.name} weight = {synapse.weight}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#endregion Init
|
#endregion Init
|
||||||
|
|
||||||
#region Cluster Array
|
#region Cluster Array
|
||||||
|
|||||||
@ -110,6 +110,30 @@ namespace NanoBrain {
|
|||||||
// Check if the angle between the vectors is > 90 degrees
|
// Check if the angle between the vectors is > 90 degrees
|
||||||
return Vector3.Dot(a, b) < 0f;
|
return Vector3.Dot(a, b) < 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void GaussianAdditiveMutation(float sigma) {
|
||||||
|
if (this.trainable == false)
|
||||||
|
return;
|
||||||
|
|
||||||
|
float deltaWeight = NormalDistribution.Sample(sigma);
|
||||||
|
this.weight += deltaWeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class NormalDistribution {
|
||||||
|
private static readonly System.Random rng = new();
|
||||||
|
|
||||||
|
// Returns a single sample from N(0, sigma^2)
|
||||||
|
public static float Sample(float sigma) {
|
||||||
|
// u1 must be > 0 to avoid log(0)
|
||||||
|
float u1 = 1.0f - (float)rng.NextDouble(); // in (0,1]
|
||||||
|
float u2 = (float) rng.NextDouble(); // in [0,1)
|
||||||
|
|
||||||
|
float stdNormal =
|
||||||
|
Mathf.Sqrt(-2.0f * Mathf.Log(u1)) * Mathf.Cos(2.0f * Mathf.PI * u2);
|
||||||
|
|
||||||
|
return sigma * stdNormal;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user