-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxor.py
More file actions
61 lines (46 loc) · 1.65 KB
/
xor.py
File metadata and controls
61 lines (46 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from nn import NeuralNetwork
import numpy as np
population_size = 100
num_of_generations = 1000
shape = [2, 2, 1]
def fitness(nn, log=True):
inputs = [[0, 0], [1, 0], [0, 1], [1, 1]]
targets = [[0], [1], [1], [0]]
outputs = nn.predict_set(inputs)
if log:
print("Starting Fitness: ")
print(outputs, targets)
cost = np.subtract(targets, outputs)
if log:
print(cost)
cost = sum(np.multiply(cost, cost))
if log:
print("COST: ",cost)
return 1/cost[0]
def softmax(fitnesses):
return [np.exp(f) / sum(np.exp(fitnesses)) for f in fitnesses]
def test():
nn = NeuralNetwork(shape)
print("0", nn.forward([0, 0]))
print("fitness", fitness(nn), "\n")
for i in range(10):
nn.mutate(1)
print("0", nn.forward([0, 0]))
print("fitness", fitness(nn), "\n")
def main():
population = []
for i in range(population_size):
population.append(NeuralNetwork(shape))
for i in range(num_of_generations):
population = sorted(population, key=(lambda network: fitness(network)))
fitnesses = softmax([fitness(nn) for nn in population])
new_population = []
for i in range(population_size):
new_population.append(np.random.choice(population, p=fitnesses).mutate(0.1))
# new_population.append(NeuralNetwork.crossover(np.random.choice(population, p=fitnesses), np.random.choice(population, p=fitnesses)).mutate(0.01))
population = new_population
print(population[0].forward([0, 0]))
print(population[0].forward([1, 0]))
print(population[0].forward([0, 1]))
print(population[0].forward([1, 1]))
main()