-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperceptron2.py
More file actions
30 lines (25 loc) · 832 Bytes
/
perceptron2.py
File metadata and controls
30 lines (25 loc) · 832 Bytes
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
import math
def normalize(values):
sum = 0
for value in values:
sum += value**2
normalized_values = []
for value in values:
normalized_values.append(value/math.sqrt(sum))
return normalized_values
class Perceptron:
def __init__(self, dim, alfa, language):
self.alfa = alfa
self.weights = [0 for _ in range(dim)]
self.language = language
def calculate(self, inputs):
inputs = normalize(inputs)
result = 0
for i in range(len(self.weights)):
result += inputs[i] * self.weights[i]
return result
def learn(self, inputs, error):
n_inputs = normalize(inputs)
for i in range(len(self.weights)):
self.weights[i] += self.alfa * n_inputs[i] * error
self.weights = normalize(self.weights)