-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron.py
48 lines (35 loc) · 1 KB
/
neuron.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
45
46
47
48
from sigmoid import Sigmoid
import random
class Neuron:
def __init__(self, previousRow):
self.weightedNeurons = getWeightedNeurons(previousRow)
self.sigmoid = Sigmoid()
def evaluate(self):
sum = 0
for weightedNeuron in self.weightedNeurons:
sum += weightedNeuron.evaluate()
if len(self.weightedNeurons) > 0:
sum /= len(self.weightedNeurons)
return self.sigmoid.evaluate(sum)
class InputNeuron:
def __init__(self):
self.value = 0
def evaluate(self):
return self.value
def setValue(self, value):
self.value = value
class WeightedNeuron:
def __init__(self, neuron, coefficient):
self.neuron = neuron
self.coefficient = coefficient
def evaluate(self):
return self.coefficient * self.neuron.evaluate()
def getCoefficient(self):
return self.coefficient
def getWeightedNeurons(row):
weightedNeurons = []
for neuron in row:
coefficient = random.random()
weightedNeuron = WeightedNeuron(neuron, coefficient)
weightedNeurons.append(weightedNeuron)
return weightedNeurons