39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
import random
|
|
import math
|
|
|
|
class Neuron:
|
|
def __init__(self):
|
|
self.bias = random.uniform(-1.0, 1.0)
|
|
self.in_ = []
|
|
self.out_ = []
|
|
self.value = 0.0
|
|
|
|
def connect_in(self, connection):
|
|
self.in_.append(connection)
|
|
|
|
def connect_out(self, connection):
|
|
self.out_.append(connection)
|
|
|
|
def calc(self, input_val=None):
|
|
if input_val is not None:
|
|
self.value = input_val
|
|
else:
|
|
z = self.bias
|
|
for connection in self.in_:
|
|
z += connection.value * connection.weight
|
|
|
|
z = max(-500, min(500, z))
|
|
self.value = 1.0 / (1.0 + math.exp(-z))
|
|
|
|
for connection in self.out_:
|
|
connection.transfer(self.value)
|
|
|
|
def adjust(self, error:float, learning_rate:float = 0.1):
|
|
derivative = self.value * (1.0 - self.value)
|
|
|
|
delta = error * derivative
|
|
|
|
self.bias += learning_rate * delta
|
|
|
|
for connection in self.in_:
|
|
connection.adjust(delta, learning_rate) |