-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageDetector.py
195 lines (193 loc) · 16.7 KB
/
LanguageDetector.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import random
import math
import time
def Sigmoid(x):
try:
return 1 / (1 + math.exp(-x))
except:
return x*(0<=x<=1)
TransferDerivative = lambda x: x * (1.0 - x)
ReLU = lambda x: x * (x > 0)
rand = lambda a,b: (b-a)*random.random() + a
neux = lambda x: (-1*(x<0.0))+(x>0.0)+(x==0)
class NeuralNetwork:
def __init__(self,inp,hidlyr,hid,outp,activation='Sigmoid',n=1,minloss=0.4,iterations=-1):
self.seed = 1603445364#round(time.time())
random.seed(self.seed)
counter = time.time()
self.iterations = iterations
self.minloss = minloss
self.testresult = []
self.n = n
self.activation = activation
self.inp = [0 for i in range(inp)]
self.h = [[0 for i in range(hid)] for i in range(hidlyr)]
self.outp = [0 for i in range(outp)]
self.roundoutp = [0 for i in range(outp)]
self.hw = [[[0 for i in range(hid)] for i in range(hid)] for i in range(hidlyr-1)]
self.inpw = [[0 for i in range(hid)] for i in range(inp)]
self.outpw = [[0 for i in range(hid)] for i in range(outp)]
self.outpb = [0 for i in range(outp)]
self.hb = [[0 for i in range(hid)] for i in range(hidlyr-1)]
self.inpb = [0 for i in range(inp)]
self.random(0.001)
print('\n','Time Spend to Init Network ',time.time()-counter,'\n\n input =',self.inp,'\n\n','hidden = ',self.h,'\n\n','output =',self.outp,'\n\n','\n\n weight input to hidden =',self.inpw,'\n\n','weight hidden = ',self.hw,'\n\n','weight hidden to output =',self.outpw,'\n\n')
def calculate(self,a):
self.h = [[0 for i in range(len(self.h[0]))] for i in range(len(self.h))]
self.outp = [0 for i in range(len(self.outp))]
for b in range(len(self.inp)):
for c in range(len(self.inpw[0])):
self.inp[b] = a[b]
self.h[0][c] = (self.h[0][c]+((self.inp[b]*self.inpw[b][c])+self.inpb[b]))
for b in range(len(self.h)-1):
for c in range(len(self.hw[0])):
for d in range(len(self.hw[0][0])):
self.h[b+1][c] = (self.h[b+1][c]+((self.h[b][c]*self.hw[b][c][d])+self.hb[b][c]))
for b in range(len(self.outp)):
for c in range(len(self.outpw[0])):
self.outp[b] = self.outp[b]+((self.h[len(self.h)-1][c]*self.outpw[b][c])+self.outpb[b])
exec('self.outp = ['+self.activation+'(x) for x in self.outp]')
try:
self.roundoutp = [round(b) for b in self.outp]
except:
pass
self.testresult.append(self.roundoutp)
def random(self,a):
for b in range(len(self.h)-1):
for c in range(len(self.h[0])):
self.hb[b][c] = (rand(-1*a,a))
for d in range(len(self.h[0])):
self.hw[b][c][d] = (rand(-1*a,a))
for b in range(len(self.inp)):
self.inpb[b] = (rand(-1*a,a))
for c in range(len(self.h[0])):
self.inpw[b][c] = (rand(-1*a,a))
for b in range(len(self.outp)):
self.outpb[b] = (rand(-1*a,a))
for c in range(len(self.h[0])):
self.outpw[b][c] = (rand(-1*a,a))
def getloss(self,pola):
loss = 0
for a in pola:
self.calculate(a[0])
for b in range(len(self.outp)):
loss += abs((a[1][b]-self.outp[b])*(a[1][b]-self.outp[b]))
return loss
def train(self,pola):
counter = time.time()
OLoss = 1
literation = 0
self.oldloss = 9999999
self.oldData = [self.inpw,self.hw,self.outpw,self.inpb,self.hb,self.outpb]
for a in pola:
self.calculate(a[0])
while not ((([a[1] for a in pola] == self.testresult) or (self.getloss(pola)<self.minloss)) or (self.iterations <= literation and self.iterations != -1)):
try:
print('literation',literation,'loss',self.getloss(pola))#,',loss change',OLoss-self.getloss(pola))
if 0.0==OLoss-self.getloss(pola):
print('stuck at local minima')
if self.getloss(pola) < self.oldloss:
self.oldloss = self.getloss(pola)
self.oldData = [eval(str(self.inpw)+','+str(self.hw)+','+str(self.outpw)+','+str(self.inpb)+','+str(self.hb)+','+str(self.outpb))]
self.random(0.001)
OLoss = self.getloss(pola)
for b in range(len(self.h)-1):
for c in range(len(self.h[0])):
for d in range(len(self.h[0])):
Oloss = self.getloss(pola)
self.hb[b][c] += self.n
if Oloss<self.getloss(pola):
self.hb[b][c] -= self.n*2
if Oloss<self.getloss(pola):
self.hb[b][c] += self.n
Oloss = self.getloss(pola)
self.hw[b][c][d] += self.n
if Oloss<self.getloss(pola):
self.hw[b][c][d] -= self.n*2
if Oloss<self.getloss(pola):
self.hw[b][c][d] += self.n
for b in range(len(self.inp)):
for c in range(len(self.h[0])):
Oloss = self.getloss(pola)
self.inpb[b] += self.n
if Oloss<self.getloss(pola):
self.inpb[b] -= self.n*2
if Oloss<self.getloss(pola):
self.inpb[b] += self.n
Oloss = self.getloss(pola)
self.inpw[b][c] += self.n
if Oloss<self.getloss(pola):
self.inpw[b][c] -= self.n*2
if Oloss<self.getloss(pola):
self.inpw[b][c] += self.n
for b in range(len(self.outp)):
for c in range(len(self.h[0])):
Oloss = self.getloss(pola)
self.outpb[b] += self.n
if Oloss<self.getloss(pola):
self.outpb[b] -= self.n*2
if Oloss<self.getloss(pola):
self.outpb[b] += self.n
Oloss = self.getloss(pola)
self.outpw[b][c] += self.n
if Oloss<self.getloss(pola):
self.outpw[b][c] -= self.n*2
if Oloss<self.getloss(pola):
self.outpw[b][c] += self.n
literation += 1
except KeyboardInterrupt:
print('Litaration End Because KeyboardInterrupt')
break
if self.getloss(pola) > self.oldloss:
self.data_set(self.oldData[0][0],self.oldData[0][1],self.oldData[0][2],self.oldData[0][3],self.oldData[0][4],self.oldData[0][5])
print('DATA-----------------')
print(str(self.inpw)+','+str(self.hw)+','+str(self.outpw)+','+str(self.inpb)+','+str(self.hb)+','+str(self.outpb))
print('END------------------')
print('literation',literation,'loss',self.getloss(pola))#,',loss change',OLoss-self.getloss(pola))
print('Random seed : ',self.seed)
def test(self,a):
counter = time.time()
self.calculate(a)
return [self.outp,self.roundoutp,time.time()-counter]
def data_set(self,winput,whidden,woutput,binput,bhidden,boutput):
self.inpw = winput
self.outpw = woutput
self.hw = whidden
self.inpb = binput
self.hb = bhidden
self.outpb = boutput
def wtl(text):
global letters
nrange = 20
b = text+''.join([' ' for x in range(nrange-len(text.split(' ')))])
return [letters.index(x) for x in b]
#input = ... , hiddenlayer = ... , node per hiddenlayer = ... , output = ...
a = NeuralNetwork(20,3,10,2,activation='Sigmoid',n=1,minloss=0,iterations=-1)
letters = [' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9',',','*','’','.','?','!','+','-','=','(',')',"'",'"']
a.data_set([[5.999638505441361, -0.0006086508317433292, 1.0008294816673313, 1.000165307041454, 10.000598617458296, 5.000138243944074, -3.0003875848218113, -1.9995870613673077, 6.000879215274479, 8.000609959942357], [2.0004147393514486, -0.9995974933470897, 1.000585678694363, 0.0007235204150290997, -1.0007676719332856, 0.9994855919929191, -0.0008037068314163953, 1.0007786533943221, 2.0000285803418336, 1.0007266917493087], [4.000205004107427, -2.000562081787989, 2.9991543866380255, -0.0005957174055901682, 6.000652472603784, 5.000653243569879, 1.0007799311272265, -1.0007433313417469, 6.000979096524645, 8.999978247329382], [-5.999514962978231, 2.0006517594340645, -3.9995143094435264, 2.9995755201129315, -2.0006903767828494, -4.999475238481133, 1.000591399295696, 5.0009957967478496, -2.99964291613783, -1.9999022316206947], [11.00061311118202, -6.999798796836277, 11.000803384725867, -4.999270243136915, 7.0003834509484495, 11.000634982802778, 6.999186002261119, -5.000760922823423, 10.999192603091258, 10.999448617164038], [-3.9996356304053773, 2.0000637483881927, 2.0006259446868957, 2.999216386963867, -0.0005972178196711475, -0.00025241312868740096, 3.0006016087341134, 2.9990625568265488, -1.0000209966299183, -1.0001736696667427], [6.999949275779, -9.000955972764224, -1.9990847700439458, 1.9993875962266863, -4.999469665110341, -1.0006025574925994, -5.000883469835382, 3.0007352217331142, 8.99912623483611, 1.0001447707268172], [-5.999106605010089, 2.0007623357155286, -6.999792510245207, 3.999640605352122, 0.9996012725915362, -0.0005093119640671162, 5.000782174302083, 7.0002174343462515, -2.000100412666008, -3.0009663231321646], [2.9990693510955646, -2.000055913372181, 2.000777892178002, 2.0000373227096375, -1.999330630055363, 0.9997978187677208, 2.9992564918586613, 0.0002838332261791976, -4.0001858072779966, 4.000559089667246], [-4.999494563895039, 11.000885001522022, 1.9993449920750335, 8.99993018694381, 6.999121998428825, 2.999825716390428, 9.0005104620205, 4.9993972967551255, -0.9990246638219169, 6.999497993804891], [11.00062607903673, -3.000108054269223, 10.999255169926204, 1.000375363382486, 6.9995748485849685, 10.99957986683139, 6.999773547047868, 2.9995147449628075, 11.000496236895485, 11.000233524312884], [10.99933014735751, 10.999405922400145, 10.99900011838394, 10.999953588623494, 11.000692485621391, 11.000680296331343, 10.999724505268784, 11.000453517779116, 10.999013856896257, 10.999567504323696], [11.000960595950218, 11.000661618278606, 11.000134856913261, 11.00006868553667, 10.999308131133038, 11.00043649180965, 11.000650517617842, 11.00046457461617, 10.999073183144539, 10.99952449420176], [10.999018913559956, 11.000555518401534, 11.000708748331464, 11.000476567881504, 11.000267971885732, 11.000970817715555, 11.000959891956734, 10.999270222972942, 10.999450106100753, 10.999379172347965], [11.000491608628451, 10.999632723367075, 11.000901010298264, 11.000614802662415, 10.999661362668977, 10.999376197519302, 10.99982805064743, 10.99930056182069, 11.000560076740772, 11.000227385063925], [11.000626011012736, 11.000420808996747, 10.99924775723482, 10.9994365560511, 10.999000324316686, 11.000719365118059, 10.999078803621764, 11.000443687619232, 10.999772900747583, 11.00052309495399], [11.000054093328114, 10.999806507739622, 11.000583130740281, 11.000886530230165, 10.999151642175658, 10.999657448910618, 11.000958586421355, 11.000517319701606, 10.999874237559828, 11.00089661348923], [10.99953548329229, 11.000323110904514, 10.999150985656527, 10.999272836528508, 11.000696893855661, 10.999532060264016, 10.999444753007502, 10.999530180387733, 10.999254614028317, 10.999986034989602], [10.99999850156251, 10.999169776826985, 10.999950621763405, 10.999511431531108, 11.000883723433855, 10.999765209510379, 11.000958088454707, 10.999158471859591, 10.999896135490467, 10.999751345719883], [10.999195927919704, 10.999993109250902, 11.000501244123022, 11.000862542434241, 11.000991053187638, 10.999325642373162, 11.000251530521204, 11.000161618180881, 11.000753252928389, 11.000466501338682]],[[[1.0001980268105477, 1.0008392971790272, 1.9996726578271224, 1.9993756483757572, 2.0005479479968953, 1.9992096129511516, 4.000315359517387, 2.0003864491800654, 2.0008069819244083, 3.9993322683985837], [10.999696812624693, 8.00030874794294, 8.000085387136316, 5.999496336673424, 4.000946243430422, 3.9993613579377714, 3.999953794409701, 6.999807296777227, 3.999203120859778, 2.9997223759553737], [10.999666515402293, 11.000063435607673, 11.000006028323007, 10.000342141813473, 9.99903898736042, 7.999523317482685, 9.000027755363325, 6.000799624260747, 6.999667668373438, 5.999675772263045], [-8.999119629024037, -8.999168580106975, -8.999440876688382, -9.000300186460407, -7.000299089350131, -3.999586489516382, -3.999331492074341, -5.000230438492192, -4.00043026955915, -2.000463705186686], [6.9998353438622205, 7.0007397591977, 7.999356730686426, 9.000039163964804, 9.9995541449526, 9.999495452943902, 8.999465615041984, 9.00024538887309, 9.000388216379664, 9.999744094892394], [10.999959401217492, 11.000877071606922, 9.999044198784748, 8.999101240750376, 4.999223596214072, 3.99988884706377, 5.999201572027194, 4.000283020460189, 4.000548252258659, 3.0001735477026044], [-2.9995689599152957, -2.9992074779049824, -2.999935240112065, -3.0005492289679756, -3.000640012223924, -2.9992753409162507, -2.9992931517383443, -3.000534878170658, -2.999656266956647, -2.999670877233571], [9.000708088619444, 8.999394962980661, 9.000535788727934, 8.99965548215348, 8.999635813293526, 9.000871210274312, 9.000274068711274, 9.000249085752404, 8.99913253281068, 9.000792967799278], [2.0008752103413765, 1.0008662454686776, 3.000865069600289, 1.9993464421554452, 1.9990836845400894, 3.0003271874069237, 1.9998171546866876, 1.9995899061102405, 5.000381638791193, 3.999666920126807], [-0.0005710906566225304, -1.9993691431192593, -1.9997485751488675, -3.0007620941019546, -2.000174789780538, -3.000470348926201, -2.000618697320699, -1.9999299474902203, -2.000224743433356, -2.0008089111873364]], [[-1.0005858981489593, -0.0006801313101072459, 0.9999153831768963, -0.9992539314918336, 1.0008801388676196, -0.9999703273194007, 1.00023673574766, -1.0009620672982484, 1.0008976464009036, -1.0007459126280727], [10.000101682981473, 2.0008268155770823, 0.9990684392654439, 1.0009568578257984, 1.0002832568000706, 0.9997972639079027, 0.9998881637940695, -1.0003249240568455, 0.9996893545494119, -0.9993697639012431], [9.000399308518704, 1.000068294119013, 1.0001778590511388, 1.0007771662596594, 0.9996392155966478, 1.0008976079496033, 0.9995524406061644, 1.0005985217992952, 0.9998278802842822, 0.9997335592236396], [-3.0009929535425646, 0.9992871024358725, -1.0004347542305743, 1.000456255213921, -0.9992949425795743, 0.9997846192249626, -0.9995962050869382, 1.0008553008901375, -1.0006194626241574, 0.9993859057421861], [-1.000997999083717, 0.0009005216281821049, 0.9998469937575911, -0.0006953313978450559, -0.0001714578126548183, -0.0008397285088395101, -0.0006462110139109356, 9.92824363479361e-05, -0.000929645191796169, 8.78985338315097e-05], [2.0008468165968982, 0.999220835982652, 0.9991111004789746, -1.000957005442884, 1.0003827399048886, -0.9993405442261474, 1.0004158832193928, -0.9993176184025447, 1.0006134233864592, -0.9999891230012017], [0.9994985052442567, 0.9999095819007513, 1.0009445199053137, 0.9997603365411594, -1.0004766116857007, 0.9996628114177186, -1.0007106416883549, 1.0005460326642326, -1.0003891261734539, 1.0008679063827586], [-2.0004146888000967, 0.9999142812936623, -0.999616542000862, 0.9999133049070974, -1.000563614452346, 1.0001734596396312, -1.000166939679566, 1.0000408057072838, -0.9996630904966224, 0.9992621474717565], [7.542145423999891e-05, -0.0009569824489523704, 0.00025645736899293503, -0.0008913567521329213, 0.0008192165537697083, 0.00042060748618100163, -0.00030329548565655173, -0.0002540590755382155, 0.00014498965655351803, 0.0009567265328087604], [1.999930868745931, 0.9994780747562249, 1.0004380402070125, 1.0004176455390805, -1.0006058997290945, 1.0000140301810558, -0.9996880137191404, 1.0007500512005345, -0.9999854368420222, 1.0002990678116639]]],[[1.9997447528933394, 1.000927535867909, -0.0002132067137416982, -1.9991827026076632, 2.00009953312964, -0.0009533850660017329, 2.0008263552201546, -2.0001590742586512, -4.0008669669842725, 1.0002854429621424], [-2.000694172757368, -1.000875472061077, 0.00016339866491121846, 0.9996088796574749, 1.9996920290017224, 1.0003408578902224, 2.000107719532024, 0.0009118868699320437, 7.000081063234506, -2.0004360248065693]],[26.999489367634652, 24.00007179185126, 30.00053322608014, 7.000533399917149, 25.000875764291482, 1.9998265315513208, 33.99961280043165, -11.999599537341565, 5.000730418648253, -9.000666835151064, 4.000321686118911, -1.954653320113131e-05, -0.000649782704651436, 0.0006759266784457374, 2.1172330676222373e-05, -0.00031789031927043077, -0.0008184862727218523, 0.0006602951511673893, -0.00022803977175689383, -0.0006282424239518747],[[-50.00068948160795, -59.999401645380104, 28.00088243150165, 64.99956974508208, 49.99951035539782, -27.00038359647793, -10.000407252565783, 42.000549364769526, -69.00095357219948, 69.99952857775101], [14.999917005200473, -50.999333426664975, -54.999694111581434, 57.00003291572834, 20.999877369048505, -37.99903811058927, 24.000923806134313, 36.9994581423397, 9.999863178847347, 11.999249104676323]],[16.999717452798834, 16.999223815318402])
a.train([
[wtl('halo'),[1,0]],
[wtl('apakabar'),[1,0]],
[wtl('siapa kamu'),[1,0]],
[wtl('yang mana'),[1,0]],
[wtl('baik'),[1,0]],
[wtl('aneh'),[1,0]],
[wtl('hello'),[0,1]],
[wtl('forgot'),[0,1]],
[wtl('im fine'),[0,1]],
[wtl('you'),[0,1]],
[wtl('how are you'),[0,1]],
[wtl('say'),[0,1]],
[wtl('well done'),[0,1]],
[wtl('maybe later'),[0,1]]
])
language = ['indonesia','english']
while True:
try:
c = input('|Input<')
if len(c)>20:
print(f'WARNING: Character {c[20:]} is not include')
b = a.test(wtl(c))
print(f"|Output>{','.join([language[x] if round(b[1][x])!= 0 else '' for x in range(len(b[1]))])}")
except Exception as e:
print(e)