I have a code, which implements Perceptron manually. Unfortunately, I am receiving an error that I have no knowledge of how to fix it... I searched and saw that this error normally occurs when a method is used and is not called, but in this point, I do not know where is any method I should be calling in the error line.
My code is here:
import random
class Perceptron:
def __init__(self, amostras, saidas, taxa_aprendizado = 0.1, epocas = 1000, limiar = -1):
self.amostras = amostras
self.saidas = saidas
self.taxa_aprendizado = taxa_aprendizado
self.epocas = epocas
self.limiar = limiar
self.n_amostras = len(amostras)
self.n_atributos = len(amostras[0])
self.pesos = []
def treinar(self):
for amostra in self.amostras:
amostra.insert(0, -1)
for i in range(self.n_atributos):
self.pesos.append(random.random)
self.pesos.insert(0, self.limiar)
n_epocas = 0
while True:
erro = False
for i in range(self.n_amostras):
uzin = 0
for j in range(self.n_atributos + 1):
uzin += (self.pesos[j])*(self.amostras[i][j])
y = self.degrau(uzin)
if y != saidas[i]:
erro_aux = self.saidas[i] - y
for j in range(self.n_atributos + 1):
uzin += self.pesos[j] + (self.taxa_aprendizado) * (erro_aux) * (self.amostras[i][j])
erro = True
n_epocas +=1
if not erro or n_epocas > self.epocas:
break
print(self.pesos)
def degrau(self, u):
if u >=0:
return 1
else:
return 0
entradas = [[0,0], [0,1], [1,0], [1,1]]
saidas = [0,1,1,1]
rede = Perceptron(entradas, saidas)
rede.treinar()
print(rede.amostras)
Traceback (most recent call last): File "C:/Users/nick/PycharmProjects/CursoMachineLearning/redes_neurais/perceptronzin.py", line 58, in rede.treinar() File "C:/Users/nick/PycharmProjects/CursoMachineLearning/redes_neurais/perceptronzin.py", line 31, in treinar uzin += (self.pesos[j])*(self.amostras[i][j]) TypeError: unsupported operand type(s) for *: 'builtin_function_or_method' and 'int'
In line 19 you created a list of function objects instead of floats.
Use:
self.pesos.append(random.random())
instead of
self.pesos.append(random.random)