-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural.py
145 lines (114 loc) · 5.73 KB
/
neural.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import numpy as np
from utils import *
# import matplotlib
# import matplotlib.pyplot as plt
epsilon = 0.00001
def neural_network(network, weights, regularization, inputs, predictions, max_iterations, alpha, name_fig):
iterations = 0
Jtotal = 0
while iterations < max_iterations:
iterations = iterations+1
# print("\r Iteration", iterations)
input_propagate = []
prev_J = Jtotal
Jtotal = 0
for example in range(0, len(inputs)):
input_propagate.append([])
for layer in range(0, len(network)-1):
if layer == 0:
input_propagate[example].append(np.array(inputs[example], ndmin=2))
else:
input_propagate[example].append(np.array(sig(z), ndmin=2))
input_propagate[example][layer] = np.insert(input_propagate[example][layer], 0, 1, 0)
z = np.dot(weights[layer], input_propagate[example][layer])
layer = layer + 1
input_propagate[example].append(np.array(sig(z), ndmin=2))
# -np.average(y * np.log(p) + (1 - y) * np.log(1 - p))
# J = log_loss + regTerm * np.linalg.norm(theta[1:]) / (2 * m)
J = -1*predictions[example]*np.log(input_propagate[example][layer]+epsilon)-(1-predictions[example])*np.log(1-input_propagate[example][layer]+epsilon)
Jtotal += np.sum(J)
Jtotal/=len(inputs)
S = 0
for layer in range(0, len(network)-1):
S += np.sum(np.delete(weights[layer], 0, axis=1)**2)
S = regularization/(2*len(inputs))*S
# plt.scatter(iterations, Jtotal+S, c="g")
# plt.pause(0.001)
# plt.draw()
# plt.pause(0.05)
# Jtotal = Jtotal+S
# print("iterations:", iterations)
# print("J", Jtotal)
# print("Prev J", prev_J)
# print("Error", abs(prev_J-Jtotal))
delta = []
D = []
for layer in range(0, len(network)-1):
D.append(np.zeros(weights[layer].shape))
for example in range(0, len(inputs)):
delta.append([])
for layer in range(0, len(network)):
delta[example].append([])
delta[example][layer] = input_propagate[example][layer]-predictions[example]
for layer in reversed(range(1, len(network)-1)):
delta[example][layer] = np.dot(weights[layer].T,delta[example][layer + 1])*input_propagate[example][layer]*(1 - input_propagate[example][layer])
delta[example][layer] = np.delete(delta[example][layer], 0)
delta[example][layer] = np.array(delta[example][layer], ndmin=2).T
for layer in reversed(range(0, len(network)-1)):
Dtemp = np.dot(delta[example][layer+1], input_propagate[example][layer].T)
D[layer] = D[layer]+Dtemp
P = []
for layer in range(0, len(network)-1):
P.append([])
for layer in range(0, len(network)-1):
weightsTemp = weights[layer].copy()
weightsTemp[:, 0] = 0
P[layer] = regularization*weightsTemp
D[layer] = (1/len(inputs))*(D[layer]+P[layer])
for layer in range(0,len(network)-1):
weights[layer]=weights[layer]-alpha*D[layer]
if iterations > 1 and abs(prev_J-Jtotal) <= 0.0001:
max_iterations = iterations
# plt.xlabel('EPHOCS')
# plt.ylabel('J Total + Regularização')
# plt.savefig(name_fig)
return weights
def feedfoward(network, weights, inputs, predictions):
input_propagate = []
output = []
for example in range(0, len(inputs)):
input_propagate.append([])
for layer in range(0, len(network)-1):
if layer == 0:
input_propagate[example].append(np.array(inputs[example], ndmin=2))
else:
input_propagate[example].append(np.array(sig(z), ndmin=2))
input_propagate[example][layer] = np.insert(input_propagate[example][layer], 0, 1, 0)
z = np.dot(weights[layer], input_propagate[example][layer])
layer = layer + 1
input_propagate[example].append(np.array(sig(z), ndmin=2))
#input_propagate[example][layer] = np.around(input_propagate[example][layer])
if len(input_propagate[example][layer]) == 2:
if input_propagate[example][layer][0] >= input_propagate[example][layer][1]:
input_propagate[example][layer][0] = 1
input_propagate[example][layer][1] = 0
else:
input_propagate[example][layer][0] = 0
input_propagate[example][layer][1] = 1
else:
if input_propagate[example][layer][0] >= input_propagate[example][layer][1] and (input_propagate[example][layer][0] >= input_propagate[example][layer][2]):
input_propagate[example][layer][0] = 1
input_propagate[example][layer][1] = 0
input_propagate[example][layer][2] = 0
elif input_propagate[example][layer][1] >= input_propagate[example][layer][0] and (input_propagate[example][layer][1] >= input_propagate[example][layer][2]):
input_propagate[example][layer][0] = 0
input_propagate[example][layer][1] = 1
input_propagate[example][layer][2] = 0
else:
input_propagate[example][layer][0] = 0
input_propagate[example][layer][1] = 0
input_propagate[example][layer][2] = 1
# print("\tSaida predita para o exemplo :", print1D(input_propagate[example][layer]))
# print("\tSaida esperada para o exemplo :", print1D(predictions[example]))
output.append(input_propagate[example][layer])
return output