-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron.py
101 lines (82 loc) · 2.25 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
###############################################################################
#
# Autor: Petr Vácha
# Název: Backpropagation NN
#
###############################################################################
import sys, os
import random
#wahy = (0.1, 0.4, -0.5, 0.2, -0.2, -0.3, 0.1, -0.4, 0.2, -0.4, 0.0, 0.5, 0.1)
class Perceptron:
"""Perceptron"""
def __init__(self, numberInput, lamda):
self.__numberInput = numberInput
self.__weight = []
self.__inpt = []
self.__outpt = 0
self.__u = 0
self.__lamda = lamda
self.__e = 2.71828182845904523536
self.setWeight(0, 0)
self.setInput(0, 1)
self.setWeights()
def __repr__(self):
outStr = ""
outStr += "weights: "
outStr += "\n"
for w in range(len(self.__weight)):
outStr += str(self.__weight[w])
outStr += "\n"
outStr += "input: "
for i in range(len(self.__inpt)):
outStr += str(self.__inpt[i])
outStr += "\n"
outStr += "u: "
outStr += str(self.__u)
outStr += "\n"
outStr += "output: "
outStr += str(self.__outpt)
outStr += "\n"
return outStr
def setWeight(self, index, weight):
if index < len(self.__weight):
self.__weight[index] = weight
else:
self.__weight.append(weight)
def setWeights(self):
for i in range(1, self.__numberInput + 1):
self.setWeight(i, random.random()-0.5)
#self.setWeight(i,wahy[i-1]) #debug
def setInput(self, index, inpt):
if index < len(self.__inpt):
self.__inpt[index] = inpt
else:
self.__inpt.append(inpt)
def setOutput(self, outpt):
self.__outpt = outpt
def setU(self, u=0):
self.__u = u
def getNumberInput(self):
return self.__numberInput
def getU(self):
return self.__u
def getWeight(self, index):
if index < len(self.__weight):
return self.__weight[index]
def getInput(self, index):
if index < len(self.__inpt):
return self.__inpt[index]
def getOutput(self):
return self.__outpt
def computeBaseFunction(self):
self.setU()
for nI in range(0, self.__numberInput + 1):
self.__u = self.__u + self.getWeight(nI) * self.getInput(nI)
def computeOutput(self):
y= 1/(1+self.__e**(-self.__lamda*self.__u)) #sigmoidální aktivační funkce
self.setOutput(y)
def setDelta(self, delta):
self.__delta = delta
def getDelta(self):
return self.__delta