-
Notifications
You must be signed in to change notification settings - Fork 0
/
Neural.py
96 lines (83 loc) · 3.01 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
import numpy as np
#funkcja aktywacji (sigmoida)
def nonlin(x,deriv=False):
if(deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
#normalizacja wejscia
def normalize(array):
for i in range (3):
if (i == 0):
array[i] = float(array[i])/45
if (i == 1):
array[i] = float(array[i])/30
if (i == 2):
array[i] = float(array[i])/51
array[i] = np.around(array[i], 3)
return array
class Neural:
def __init__(self):
#deklaracja wag
self.syn0 = 0
self.syn1 = 0
#czytanie zbioru treningowego
file = "neural.csv"
lines = open(file,"r").readlines()
y = []
x = []
for line in lines:
data = map(float, map(str.strip, line.split(";")))
x.append(data[:3])
tmpArr = []
tmpArr.append(data[3])
y.append(tmpArr)
#normalizacja zb. treningowego
for elem in x:
for i in range (len(elem)):
if (i == 0):
elem[i] = elem[i]/45
if (i == 1):
elem[i] = elem[i]/30
if (i == 2):
elem[i] = elem[i]/51
elem[i] = np.around(elem[i], 3)
for elem in y:
elem[0] = np.around(elem[0]/10, 3)
x = np.array(x)
y = np.array(y)
#inicjalizacja wag losowymi wartosciami o sredniej zero
np.random.seed(2)
self.syn0 = 2*np.random.random((3,len(y))) - 1
self.syn1 = 2*np.random.random((len(y),1)) - 1
#petla uczaca
for j in xrange(80000):
#warstwa 0 = wejscie
l0 = x
#warstwa 1 - ukryta
l1 = nonlin(np.dot(l0,self.syn0))
#warstwa 2 - ostatnia
l2 = nonlin(np.dot(l1,self.syn1))
#blad
l2_error = y - l2
#blad razy pochodna -> wektor delta
#jesli pochodna niewielka tzn oszacowanie bylo dosc pewne i nie powinnismy za bardzo zmieniac wag
l2_delta = l2_error*nonlin(l2,deriv=True)
#Propagacja wsteczna:
#Bierzemy wektor delta (wazony blad) warstwy drugiej
#i wysylamy go przez synapsy do warstwy pierwszej.
#W ten sposob zachowujemy informacje o tym, jak bardzo
#poszczegolne wezly w. 1. przyczynily sie do bledu w. 2.
l1_error = l2_delta.dot(self.syn1.T)
#blad razy pochodna -> wektor delta
l1_delta = l1_error * nonlin(l1,deriv=True)
#aktualizacja wag
self.syn1 += l1.T.dot(l2_delta)
self.syn0 += l0.T.dot(l1_delta)
#funkcja przepuszczajaca prawdziwe dane przez nauczona siec
def solve(self, array):
array = normalize(array)
l1 = nonlin(np.dot(array,self.syn0))
result = nonlin(np.dot(l1,self.syn1))
# print "result: "
# print str(result)
return result