Neuron 101

Nature-inspired optimization / Neuroevolution

Türkçe

Plate61

Unit 6 · Topic 7 of 11

Neuroevolution

Medium, 8 min, 10 steps

0.25−0.990.960.940.230.44x₁x₂h₁, b₁ = 0.05h₂, b₂ = −0.44ŷ, c = −0.15ŷ = 0.54, 0.55, 0.56, 0.57; target 0, 1, 1, 0
Keyboard: left and right arrows change the step, space plays and pauses.

Step 1 of 10 Network

Training the network by evolution

The small network from unit 5: two inputs, two hidden neurons, one output. It has 9 numbers: 6 weights on the edges and 3 biases on the nodes. Backpropagation set these numbers with gradients. This time there is no gradient: an individual is the list of these 9 numbers, so its chromosome is made of numbers instead of bits. The network on the scene is the first random individual, with fitness 0.748.

Open in Colab
  1. import math, random
  2. X = [(0, 0), (0, 1), (1, 0), (1, 1)]
  3. T = [0, 1, 1, 0]
  4. s = lambda z: 1 / (1 + math.exp(-z))
  5. def ileri(w, a, b):
  6. h = s(w[0]*a + w[1]*b + w[2])
  7. g = s(w[3]*a + w[4]*b + w[5])
  8. return s(w[6]*h + w[7]*g + w[8])
  9. def uygun(w):
  10. e = [(ileri(w, a, b) - t) ** 2
  11. for (a, b), t in zip(X, T)]
  12. return 1 - sum(e) / 4
  13. def turnuva():
  14. a, b = random.choices(nufus, k=2)
  15. return max(a, b, key=uygun)
  16. def mutasyon(c):
  17. return [x + random.gauss(0, 0.5)
  18. if random.random() < 0.2
  19. else x for x in c]
  20. random.seed(1)
  21. nufus = [[random.uniform(-1, 1)
  22. for _ in range(9)]
  23. for _ in range(20)]
  24. for nesil in range(100):
  25. yeni = [max(nufus, key=uygun)]
  26. while len(yeni) < 20:
  27. a, b = turnuva(), turnuva()
  28. kes = random.randint(1, 8)
  29. c = a[:kes] + b[kes:]
  30. yeni.append(mutasyon(c))
  31. nufus = yeni
  32. en_iyi = max(nufus, key=uygun)
  33. print(uygun(en_iyi))
  34. print([ileri(en_iyi, a, b)
  35. for a, b in X])
nesil
0
uygunluk
0.75
dogru
2
b1
0.05
b2
−0.44
c
−0.15
y00
0.54
y01
0.55
y10
0.56
y11
0.57