-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuron.py
44 lines (34 loc) · 1.25 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
import random
import numpy as np
# Perceptron class
# Default weights and bias
class Neuron:
def __init__(self, nInputs, learningRate):
self.weights = [(random.uniform(-1, 1)) for i in range(0, nInputs)]
self.learningRate = learningRate
self.bias = 0.5
self.error = 0
self.delta = 0
self.output = 0
def getWeight(self, i):
return self.weights[i]
def getDelta(self):
return self.delta
def getOutput(self):
return self.output
# Calculates result for inputs with sigmoid function
def operate(self, inputs):
self.output = self.sigmoidFunction(inputs)
return self.output
# Sigmoid Function
def sigmoidFunction(self, inputs):
return 1 / (1 + np.exp(- sum([a*b for a,b in zip(self.weights, inputs)]) - self.bias))
def setDelta(self, error):
self.delta = error * self.output * (1.0 - self.output)
def setWeights(self, input):
newWeights = map(lambda i : self.weights[i] + self.learningRate * self.delta * input[i],
range(0, len(self.weights)))
self.weights = newWeights
def update(self, input):
self.setWeights(input)
self.bias = self.bias + self.learningRate * self.delta