-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRobotAI(neuronal, final model).py
113 lines (87 loc) · 3.36 KB
/
RobotAI(neuronal, final model).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
102
103
104
105
106
107
108
109
110
111
112
113
from cmath import tan
import numpy as np
def sigmoid(x):
return 1.0/(1.0 + np.exp(-x))
def sigmoid_derivada(x):
return sigmoid(x)*(1.0-sigmoid(x))
def tanh(x):
return np.tanh(x)
def tanh_derivada(x):
return 1.0 - x**2
class NeuralNetwork:
def __init__(self, layers, activation ='tanh'):
if activation == 'sigmoid':
self.activation = sigmoid
self.activation_prime = sigmoid_derivada
elif activation == 'tanh':
self.activation = tanh
self.activation_prime = tanh_derivada
self.weights = []
self.deltas = []
for i in range(1, len(layers) - 1):
r = 2*np.random.random((layers[i-1] + 1, layers[i] + 1)) -1
self.weights.append(r)
r = 2*np.random.random( (layers[i] + 1, layers[i+1])) - 1
self.weights.append(r)
def fit(self, X, y, learning_rate=0.2, epochs=100000):
ones = np.atleast_2d(np.ones(X.shape[0]))
X = np.concatenate((ones.T, X), axis=1)
for k in range(epochs):
i = np.random.randint(X.shape[0])
a = [X[i]]
for l in range(len(self.weights)):
dot_value = np.dot(a[l], self.weights[l])
activation = tanh(dot_value)
a.append(activation)
error = y[i] - a[-1]
deltas = [error * sigmoid_derivada(a[-1])]
for l in range(len(a) - 2, 0, -1):
deltas.append(deltas[-1].dot(self.weights[l].T)*sigmoid_derivada(a[l]))
self.deltas.append(deltas)
deltas.reverse()
for i in range(len(self.weights)):
layer = np.atleast_2d(a[i])
delta = np.atleast_2d(deltas[i])
self.weights[i] += learning_rate * layer.T.dot(delta)
if k % 10000 == 0: print('epochs:', k)
def predict(self, x):
ones = np.atleast_2d(np.ones(x.shape[0]))
a = np.concatenate((np.ones(1).T, np.array(x)), axis=0)
for l in range(0, len(self.weights)):
a = tanh(np.dot(a, self.weights[l]))
return a
def print_weights(self):
print("LISTADO PESOS DE CONEXIONES")
for i in range(len(self.weights)):
print(self.weights[i])
def get_deltas(self):
return self.deltas
nn = NeuralNetwork([2, 3, 4,], activation = 'tahn')
X = np.array([
[0 ,0], # SIN OBSTACULOS
[0, 1], # SIN OBSTACULOS
[0, -1], # SIN OBSTACULOS
[0.5, 1], # OBSTACULO A LA DRCH
[0.5, -1], # OBSTACULO A LA IZQ
[1, 1], # OBSTACULO DEMASIADO CERCA A LA DRCH
[1, -1] # OBSTACULO DEMASIADO CERCA A LA IZQ
])
y = np.array([
[1,0,0,1], # AVANZAR
[1,0,0,1], # AVANZAR
[1,0,0,1], # AVANZAR
[0,1,0,1], # GIRAR A LA DERECHA
[1,0,1,0], # GIRAR A LA IZQUIERDA
[1,0,0,1], # AVANZAR
[0,1,1,0], # RETROCEDER
[0,1,1,0], # RETROCEDER
[0,1,1,0] # RETROCEDER
])
nn.fit(X, y, learning_rate=0.03,epochs=40001)
def Predict(x):
return (int(abs(round(x))))
index=0
for e in X:
prediccion = nn.predict(e)
print("X:",e,"esperado:",y[index],"Obtenido:", Predict(prediccion[0]),Predict(prediccion[1]),Predict(prediccion[2]),Predict(prediccion[3]))
index=index+1