Le repaire du Daimaô

It's like a mystic cave. But in the world wide web.

Films

Évasion

Affiche du film Évasion

Réalisateur :

# -*- coding: utf-8 -*- import random pas = 0.05 tableV = [ (0, 0, -1, 0), (0, 1, -1, 0), (1, 0, -1, 0), (1, 1, -1, 1) ] class Synapse: def __init__(self): self.val = 0 self.weight = random.random() def getVal(self, val): self.val = val return self.weight * val class Neurone: def __init__(self): synapseSeuil = Synapse() synapseSeuil.val = -1 synapseSeuil.weight = 0 self.synapses = [Synapse(), Synapse(), synapseSeuil] def response(self, *args): potentiel = 0 i = 0 for val in args: potentiel += self.synapses[i].getVal(val) i+= 1 potentiel += self.synapses[-1].getVal(-1) # Apply f return signe(potentiel) def learn(self): for line in tableV: reponseR = self.response(line[0], line[1]) reponseD = line[3] if error(reponseR, reponseD): print(error(reponseR, reponseD)) synapseNumber = 0 for synapse in self.synapses: synapse.weight = synapse.weight + pas * (reponseD – reponseR) * line[synapseNumber] synapseNumber += 1 for synapse in self.synapses: print(str(synapse.val) +  »  » + str(synapse.weight)) def error(v1, v2): #print(str(v1) +  » ;  » + str(v2)) #return not((v1 >=0 and v2 >= 0) or (v1 < 0 and v2 < 0)) return not((v1 >=0 and v2 > 0) or (v1 < 0 and v2 <= 0)) def signe(val): if val >= 0: return 1 else: return 0 n = Neurone() n.learn() nbNeurons = 5 * 5 poids = [0] * nbNeurons for i in range(nbNeurons): poids[i] = [0] * nbNeurons def afficheMotif(tab): for ligne in range(len(tab)/5): for col in range(5): if tab[ligne*5 + col] == 1: print(‘*’), else: print(‘ ‘), print  » #print(tab[(i*4)+0], tab[(i*4)+1], tab[(i*4)+2], tab[(i*4)+3]) print ‘ ‘ print ‘ ‘ def adaptTab(tab): for i in range(len(tab)): if tab[i] == 0: tab[i] = -1 def train(): for i in range(nbNeurons): for j in range(nbNeurons): s = 0 for k in range(len(patterns)): pattern = patterns[k] s+= pattern[i] * pattern[j] poids[i][j] = s / nbNeurons for i in range(nbNeurons): poids[i][i] = 0 def evoluer(neurone, poids): # ordre alea ici for i in range(nbNeurons): s = 0 for j in range(nbNeurons): s += poids[i][j] * neurone[j] if(s >= 0): s = 1 else: s = -1 neurone[i] = s lettreL = [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0 ] lettreJ = [ 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0 ] lettreA = [ 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0 ] test = [ 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0 ] adaptTab(lettreL) adaptTab(lettreJ) adaptTab(lettreA) patterns = (lettreL, lettreJ, lettreA) afficheMotif(lettreL) afficheMotif(lettreJ) afficheMotif(lettreA) afficheMotif(test) train() for i in range(100): evoluer(test, poids) afficheMotif(test)

Pas de commentaires

Vous pouvez laissez un commentaire grâce au formulaire ci-dessous :

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *