Permalink
Browse files

melhorando docs

  • Loading branch information...
herberthamaral committed Jan 2, 2012
1 parent 82b9267 commit 3589965b2fbca11c454cec97acdc7e06f25655f1
Showing with 190 additions and 142 deletions.
  1. +148 −140 pydaline.py
  2. +42 −2 readme.mkd
View
@@ -71,7 +71,7 @@ def aprender(self):
Realiza o treinamento por épocas.
"""
if self.saidas_desejadas.__len__()!=self.entradas.__len__():
- raise Exception("Whoops! O número de entradas é diferente do número de saídas desejadas.")
+ raise RuntimeError("Whoops! O número de entradas é diferente do número de saídas desejadas.")
while self.erroGlobal != 0 and not self.criterio_parada():
self.epocas += 1
if self.debug: print "Iniciando epoca: ",self.epocas,". Erro Global:",self.erroGlobal
@@ -105,7 +105,15 @@ def ajusta_pesos(self,erro,numEntrada):
self.pesos[j] = self.pesos[j] + self.alpha*erro*self.entradas[numEntrada][j]
if self.debug: print " Pesos atualizados:",self.pesos
- def testa(self):
+ def obter_resposta_para_entrada(self, entrada):
+ if entrada.__len__() != self.entradas[0].__len__():
+ raise RuntimeError("A entrada deve ter o mesmo tamanho das usadas no treinamento")
+ self.entradas.append(entrada)
+ resultado = self.ativacao(self.entradas.__len__()-1)
+ self.entradas.pop()
+ return resultado
+
+ def teste_manual(self):
"""
Teste individual
"""
@@ -122,146 +130,146 @@ def testa(self):
r = raw_input("Continuar testando?")
entradas_fornecidas = []
-##############################################################
-digito_a0 = [1,1,1,1,1,
- 1,1,0,1,1,
- 1,0,0,0,1,
- 1,1,1,1,1,
- 1,0,0,0,1]
-
-digito_a1 = [0,1,1,1,0,
- 1,1,0,1,1,
- 1,0,0,0,1,
- 1,1,1,1,1,
- 1,0,0,0,1]
-
-digito_b0 = [1,1,1,1,0,
- 1,0,0,0,1,
- 1,1,1,1,1,
- 1,0,0,0,1,
- 1,1,1,1,0]
-
-digito_b1 = [1,1,1,1,1,
- 1,0,0,0,1,
- 1,1,1,1,1,
- 1,0,0,0,1,
- 1,1,1,1,1]
-
-digito_c0 = [1,1,1,1,1,
- 1,0,0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 1,0,1,0,0,
+ 1,1,0,0,0,
+ 1,0,1,0,0,
+ 1,0,0,1,1]
+
+ digito_k1 = [0,0,0,0,0,
+ 1,0,1,0,0,
+ 1,1,0,0,0,
+ 1,0,1,0,0,
+ 1,0,0,1,0]
# a = [-1,-1,-1,-1] => neuron_1 = -1, neuron_2 =-1, neuron_3 = -1, etc
# b = [-1,-1,-1, 1]
# c = [-1,-1, 1,-1]
View
@@ -5,5 +5,45 @@ Esta é uma simples implementação de uma rede neural utilizando o algoritmo
Adaline, feito durante a disciplina de Introdução à inteligência artificial na
Unimontes, ministrada pelo professor Clarimundo.
-O código está relativamente simples de se entender, mas eu colocarei mais
-explicações aqui em breve.
+Uso básico
+----------
+
+Um exemplo clássico no ensino de redes neurais é o treinamento com portas
+lógicas. Suponhamos que nosso neurônio tenha que aprender a porta lógica OR. As
+entradas são as seguintes:
+
+ A or B
+ --------
+ 0 | 0
+ 0 | 1
+ 1 | 0
+ 1 | 1
+
+
+E a saída desejada é a seguinte:
+
+ | Resultado |
+ |-----------|
+ | 0 |
+ | 1 |
+ | 1 |
+ | 1 |
+
+
+É razoavelmente fácil fazer isso com o pydaline:
+
+ >>> import pydaline
+ >>> neuronio = pydaline.Pydaline()
+ >>> neuronio.setEntradas([[0,0],[0,1],[1,0],[1,1]])
+ >>> neuronio.setSaidasDesejadas([0,1,1,1])
+ >>> neuronio.setDebug(False) #opcional: remova se quiser ver as mensagens de
+ >>> neuronio.aprender()
+ >>> neuronio.obter_resposta_para_entrada([0,1])
+ 1
+ >>> neuronio.obter_resposta_para_entrada([0,0])
+ 0
+ >>> neuronio.obter_resposta_para_entrada([1,0])
+ 1
+ >>> neuronio.obter_resposta_para_entrada([1,1])
+ 1
+

0 comments on commit 3589965

Please sign in to comment.