static FloatMatrix Relu(FloatMatrix m) { float[,] output = new float[m.x, m.y]; FloatMatrix.MatrixLoop((i, j) => { output[i, j] = m[i, j] > 0 ? m[i, j] : 0; }, m.x, m.y); return(output); }
static FloatMatrix Sigmoid(FloatMatrix m) { float[,] output = m; FloatMatrix.MatrixLoop((i, j) => { output[i, j] = 1 / (1 + UnityEngine.Mathf.Exp(-output[i, j])); }, m.x, m.y); return(output); }
public static Genoma Mutate(Random r, Genoma gen, float mutationRate, float maxPerturbation) { for (int layer = 0; layer < gen.W.Length; layer++) { float[,] m = gen.W[layer]; FloatMatrix.MatrixLoop((i, j) => { if ((float)r.NextDouble() < mutationRate) { m[i, j] += ((float)r.NextDouble() * 2f - 1f) * maxPerturbation; } }, gen.W[layer].x, gen.W[layer].y); gen.W[layer] = m; } return(gen); }
public static Genoma Cross(Random r, Genoma parent1, Genoma parent2) { FloatMatrix[] SonW = new FloatMatrix[parent1.W.Length]; for (int layer = 0; layer < parent1.W.Length; layer++) { float[,] w = new float[parent1.W[layer].x, parent1.W[layer].y]; FloatMatrix.MatrixLoop((i, j) => { if ((float)r.NextDouble() > 0.5) { w[i, j] = parent1.W[layer].GetValue(i, j); } else { w[i, j] = parent2.W[layer].GetValue(i, j); } }, parent1.W[layer].x, parent1.W[layer].y); SonW[layer] = w; } return(new Genoma(SonW)); }