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
| import numpy as np
import random as rd
import matplotlib.pyplot as plt
def output_calcul(val,weight):
output = 0
for i in range(len(weight)):
output += val[i] * weight[i]
return output
def output_neuron_calcul(output):
neuron = 1/(1 + np.exp(-output))
return neuron
def sigm_calcul(output):
#sigm = output * (1 - output)
sigm = 1/(2 + np.exp(output) + np.exp(-output))
return sigm
def adjust_calcul(error,val,sigm):
return error * val * sigm
##
plt.close("all")
weight_goal = np.array([0.8,0.6,0.2])
weight = np.array([0.1,0.5,0.7])
Error = []
Weight = [[],[],[]]
T = []
nb_error = 0
t = 0
while nb_error < 100:
t += 1
val = np.array((rd.randint(0,10),rd.randint(0,10),rd.randint(0,10)))
expected_output = output_calcul(val,weight_goal)
output = output_calcul(val,weight)
neuron = output_neuron_calcul(output)
sigm = sigm_calcul(output)
error = expected_output - output
if abs(error) < 10e-2:
nb_error += 1
else:
nb_error = 0
T.append(t)
Error.append(error)
for i in range(len(weight)):
input_val = val[i]
adjust = adjust_calcul(error,input_val,sigm)
weight[i] += adjust
Weight[i].append(weight[i])
if t % 1000 == 0:
plt.pause(0.001)
plt.clf()
plt.plot(T,Error,label=Error[-1])
plt.plot(T,Weight[0],label=Weight[0][-1])
plt.plot(T,Weight[1],label=Weight[1][-1])
plt.plot(T,Weight[2],label=Weight[2][-1])
plt.legend()
plt.show()
plt.clf()
plt.plot(T,Error,label=Error[-1])
plt.plot(T,Weight[0],label=Weight[0][-1])
plt.plot(T,Weight[1],label=Weight[1][-1])
plt.plot(T,Weight[2],label=Weight[2][-1])
plt.legend()
plt.show() |
Partager