-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron.py
39 lines (31 loc) · 1.13 KB
/
perceptron.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
import random as r
from typing import List
from abstractions import APerceptron
class Perceptron(APerceptron):
def __init__(self, inputs: tuple[float, float, float] = (0, 0, 0)):
self.possible_weights: tuple[float, float] = (-1, 1)
self.weights: List[float] = [
r.choice(self.possible_weights),
r.choice(self.possible_weights),
r.choice(self.possible_weights),
]
self.inputs: tuple[float, float, float] = inputs
self.lr: float = 0.1
def set_inputs(self, inputs: tuple[float, float, float]) -> None:
self.inputs = inputs
def sign(self, n: float) -> int:
if n > 0:
return 1
else:
return -1
def guess(self) -> int:
return self.sign(
(self.weights[0] * self.inputs[0])
+ (self.weights[1] * self.inputs[1])
+ (self.weights[2] * self.inputs[2])
)
def train(self, target: int) -> None:
guess: int = self.guess()
error: int = target - guess
for i, w in enumerate(self.weights):
self.weights[i] += error * self.inputs[i] * self.lr