-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrain.py
44 lines (35 loc) · 1.22 KB
/
brain.py
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
from numpy import exp, dot
class Brain(object):
def __init__(self, weights):
self.layer1_weights = [
tuple(weights[0:2]),
tuple(weights[2:4])
]
self.layer2_weights = [
tuple(weights[4:6]),
tuple(weights[6:8])
]
self.layer3_weights = tuple(weights[8:10])
def __sigmoid(self, x):
"""Output a value from 0 to 1"""
return 1 / (1 + exp(-x))
def __layer_1(self, input):
return (
self.__sigmoid(dot(input, self.layer1_weights[0])),
self.__sigmoid(dot(input, self.layer1_weights[1]))
)
def __layer_2(self, input):
return (
self.__sigmoid(dot(input, self.layer2_weights[0])),
self.__sigmoid(dot(input, self.layer2_weights[1]))
)
def __layer_3(self, input):
return self.__sigmoid(dot(input, self.layer3_weights))
def representation(self):
return self.weights
def think(self, params):
"""The input should be the distance to the borders."""
layer_1_out = self.__layer_1(params)
layer_2_out = self.__layer_2(layer_1_out)
layer_3_out = self.__layer_3(layer_2_out)
return layer_3_out