-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.py
More file actions
72 lines (53 loc) · 2.09 KB
/
Node.py
File metadata and controls
72 lines (53 loc) · 2.09 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
62
63
64
65
66
67
68
69
70
71
72
from Utils import *
import random
class Node:
def __init__(self, layer, value=0):
self.layer = layer
self.bias = 0
self.value = value
self.weights = []
self.prevLayerValues = []
self.biasMax = 200
self.biasMin = -200
self.smallMutateValue = 1
self.smallMutateChance = 0.2
def getPrevLayerValues(self, nodes):
self.prevLayerValues = []
for node in nodes:
if node.layer + 1 == self.layer:
self.prevLayerValues.append(node.value)
def initWeights(self):
for val in self.prevLayerValues:
self.weights.append(0)
# Mutates weights at barin init
def mutateWeights(self):
for i, weight in enumerate(self.weights):
weight += random.uniform(-1, 1)
weight = min(weight, 1)
weight = max(weight, -1)
self.weights[i] = weight
def calculateValue(self):
a = 0
for i, weight in enumerate(self.weights):
a += self.weights[i] * self.prevLayerValues[i]
self.value = a + self.bias
self.value = ReLU(self.value)
if self.layer == 2:
if self.value > 1:
self.value = 1
else:
self.value = int(self.value)
# Mutates weights at a small rate
def smallMutateWeights(self):
for i, weight in enumerate(self.weights):
# Has a self.smallMutateChance % chance of mutating weights
if random.uniform(0, 1) <= self.smallMutateChance:
self.weights[i] += random.uniform(-self.smallMutateValue, self.smallMutateValue)
self.weights[i] = min(self.weights[i], 1)
self.weights[i] = max(self.weights[i], -1)
def smallMutateBias(self):
# Has a 10% chance of mutating the bias
if random.uniform(0, 1) <= 0.1:
self.bias += random.randint(-100, 100)
self.bias = min(self.bias, self.biasMax)
self.bias = max(self.bias, self.biasMin)