-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTALK support
More file actions
349 lines (290 loc) · 15.3 KB
/
TALK support
File metadata and controls
349 lines (290 loc) · 15.3 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import numpy
import pprint
import sys
import operator
import math
import time
class SENTENCE():
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
memory_matrix_rows=4
memory_matrix_columns=50
ascii_matrix_columns=10 # its the length of binary input. i have written a code to set it up. so dont change this number, its not flexible
percentage_of_on_bits_sdr=10
threshold=1 #connection strength threshold
on_bits_sdr=math.floor((percentage_of_on_bits_sdr) * (memory_matrix_columns/100))
#print(on_bits_sdr)
#print('on_bits_sdr' )
def Memory_Matrix_1(self,synapse_matrix_one):
self.memory_matrix_1=numpy.zeros((self.number_of_words,SENTENCE.memory_matrix_rows,SENTENCE.memory_matrix_columns))
for wrd in range(0,self.number_of_words):
self.memory_matrix_1[wrd,0,:]=self.sdr_matrix_1[wrd,0,:]*1
new_memory_matrix=self.memory_matrix_1[wrd,:,:]*0
#print(self.sdr_matrix_1)
#input('above was sdr matrix 1 ' )
#print(self.memory_matrix_1)
#input('above was mem matrix 1 ' )
history_matrix=self.memory_matrix_1[wrd,:,:]*1
for rw in range(1,self.len_of_longestword):
prediction_matrix=SENTENCE.Next_prediction(history_matrix,synapse_matrix_one)
#history_matrix=history_matrix*0
for col in range(0,SENTENCE.memory_matrix_columns):
if self.sdr_matrix_1[wrd,rw,col]>0:
self.memory_matrix_1[wrd,numpy.argmax(prediction_matrix[1:,col])+1,col]=1
new_memory_matrix[numpy.argmax(prediction_matrix[1:,col])+1,col]=1
#print (new_memory_matrix)
#input('above is new_memory_matrix ')
synapse_matrix_one=SENTENCE.strengthening_synapse(synapse_matrix_one,history_matrix,new_memory_matrix)
history_matrix=new_memory_matrix*1
new_memory_matrix=new_memory_matrix*0
return(synapse_matrix_one)
def Memory_Matrix_2(self,synapse_matrix_two):
self.memory_matrix_2=numpy.zeros((SENTENCE.memory_matrix_rows,SENTENCE.memory_matrix_columns))
self.memory_matrix_2[0,:]=self.sdr_matrix_2[0,:]*1
new_memory_matrix=self.memory_matrix_2[:,:]*0
#print(self.sdr_matrix_2)
#input('above was sdr matrix 2 ' )
#print(self.memory_matrix_2)
#input('above was mem matrix 2 ' )
history_matrix=self.memory_matrix_2[:,:]*1
for rw in range(1,self.number_of_words):
prediction_matrix=SENTENCE.Next_prediction(history_matrix,synapse_matrix_two)
#history_matrix=history_matrix*0
for col in range(0,SENTENCE.memory_matrix_columns):
if self.sdr_matrix_2[rw,col]>0:
self.memory_matrix_2[numpy.argmax(prediction_matrix[1:,col])+1,col]=1
new_memory_matrix[numpy.argmax(prediction_matrix[1:,col])+1,col]=1
#print (new_memory_matrix)
#input('above is new_memory_matrix ')
synapse_matrix_two=SENTENCE.strengthening_synapse(synapse_matrix_two,history_matrix,new_memory_matrix)
history_matrix=new_memory_matrix*1
new_memory_matrix=new_memory_matrix*0
return(synapse_matrix_two)
def strengthening_synapse(synapse_matrix,history_matrix,memory_matrix):
m=SENTENCE.memory_matrix_rows
n=SENTENCE.memory_matrix_columns
for j in range(0,(m*n)):
if memory_matrix.flat[j]>0:
for i in range(0,(m*n)):
if history_matrix.flat[i]>0:
''' OLD PATCH
temp=synapse_matrix[i,:,:]
#if i!=j:
if temp.flat[j] <SENTENCE.threshold:
temp.flat[j]=temp.flat[j]+1
synapse_matrix[i,:,:]=temp
'''
#strengthening the whole column with next cell
(row,col)=SENTENCE.row_column_from_flatinput(m,n,i) # row is 0-m... column is 0-n
for r in range(0,m):
i=(r*n)+col
temp=synapse_matrix[i,:,:]
#if i!=j:
if temp.flat[j] <SENTENCE.threshold:
temp.flat[j]=temp.flat[j]+0.2
synapse_matrix[i,:,:]=temp
return (synapse_matrix)
def Next_prediction(history_matrix,synapse_matrix):
# RETURNS A "MATRIX" (it may have more than required columns active and also more cells per column active.. !! loads of prediction)
#OF NEXT PREDICTION.
trial2=history_matrix*0
for i in range(0,SENTENCE.memory_matrix_rows*SENTENCE.memory_matrix_columns):
if history_matrix.flat[i]>0:
trial2=trial2+synapse_matrix[i,:,:]
return trial2
def Sdr_Matrix_3(self,connection_matrix_3):
#build SDR matrix 3
connection_matrix_zero_3=SENTENCE.replacing_nan_with_zero(connection_matrix_3)
self.sdr_matrix_3 = numpy.zeros((SENTENCE.memory_matrix_columns))
for bit in range(0,SENTENCE.memory_matrix_columns):
self.sdr_matrix_3[bit]=sum(sum((connection_matrix_zero_3[bit,:,:]*(self.memory_matrix_2[:,:]))))
#multiplication is weird in python 3.3. one multiplication will give one SDR2 bit. after forming all bits in one row
# we will take top bits now and then strengthen there connections
self.sdr_matrix_3[:]=SENTENCE.top_bit(self.sdr_matrix_3[:],SENTENCE.on_bits_sdr)
# strengthening CM
'''
for bitt in range(0,SENTENCE.memory_matrix_columns):
if self.sdr_matrix_3[bitt]==1:
connection_matrix_3[bitt,:,:]=connection_matrix_3[bitt,:,:]+((self.memory_matrix_2[:,:])/5)
#keeping it below threshold
for coll in range(0,SENTENCE.memory_matrix_columns):
for rrw in range(0,SENTENCE.memory_matrix_rows):
if connection_matrix_3[bitt,rrw,coll]> SENTENCE.threshold:
connection_matrix_3[bitt,rrw,coll]=SENTENCE.threshold
#input('\nthreshold in SDR matrix 3 reached\n')
'''
return(connection_matrix_3)
def Sdr_Matrix_2(self,connection_matrix_2):
#build SDR matrix 2
connection_matrix_zero_2=SENTENCE.replacing_nan_with_zero(connection_matrix_2)
self.sdr_matrix_2 = numpy.zeros((self.number_of_words,SENTENCE.memory_matrix_columns))
for row in range(0,self.number_of_words):
for bit in range(0,SENTENCE.memory_matrix_columns):
self.sdr_matrix_2[row,bit]=sum(sum((connection_matrix_zero_2[bit,:,:]*(self.memory_matrix_1[row,:,:]))))
#multiplication is weird in python 3.3. one multiplication will give one SDR2 bit. after forming all bits in one row
# we will take top bits now and then strengthen there connections
self.sdr_matrix_2[row,:]=SENTENCE.top_bit(self.sdr_matrix_2[row,:],SENTENCE.on_bits_sdr)
# strengthening CM
'''
for bitt in range(0,SENTENCE.memory_matrix_columns):
if self.sdr_matrix_2[row,bitt]==1:
connection_matrix_2[bitt,:,:]=connection_matrix_2[bitt,:,:]+(self.memory_matrix_1[row,:,:]/5)
#keeping it below threshold
for coll in range(0,SENTENCE.memory_matrix_columns):
for rrw in range(0,SENTENCE.memory_matrix_rows):
if connection_matrix_2[bitt,rrw,coll]> SENTENCE.threshold:
connection_matrix_2[bitt,rrw,coll]=SENTENCE.threshold
'''
return(connection_matrix_2)
def Sdr_Matrix_1(self,connection_matrix_1):
#build SDR matrix 1
connection_matrix_zero_1=SENTENCE.replacing_nan_with_zero(connection_matrix_1)
#print('connection matrix replaced NAN with zero')
#print(connection_matrix_zero_1)
self.sdr_matrix_1 = numpy.zeros((self.number_of_words,self.len_of_longestword,SENTENCE.memory_matrix_columns))
#print('SRD_matrix_1 initialized to zero')
#print(self.sdr_matrix_1)
for wrd in range(0,self.number_of_words):
for row in range(0,self.len_of_longestword):
self.sdr_matrix_1[wrd,row,:]=sum((connection_matrix_zero_1*(self.ascii_matrix[wrd,row,:])).T)
#multiplication is weird in python 3.3
self.sdr_matrix_1[wrd,row,:]=SENTENCE.top_bit(self.sdr_matrix_1[wrd,row,:],SENTENCE.on_bits_sdr)
# strengthening CM
'''
for bitt in range(0,SENTENCE.memory_matrix_columns):
if self.sdr_matrix_1[wrd,row,bitt]==1:
connection_matrix_1[bitt,:]=connection_matrix_1[bitt,:]+(self.ascii_matrix[wrd,row,:]/10)
#keeping it below threshold
for coll in range(0,SENTENCE.ascii_matrix_columns):
if connection_matrix_1[bitt,coll]> SENTENCE.threshold:
connection_matrix_1[bitt,coll]=SENTENCE.threshold
'''
#print('sdr_matrix_1 ')
#print(self.sdr_matrix_1)
#print('connection_matrix_zero_1')
#print(connection_matrix_zero_1)
#print('self.ascii_matrix')
#print(self.ascii_matrix)
return(connection_matrix_1)
def Connection_Matrix_1(connection_matrix):
#setting 50% of connections to NAN
for i in range(0,SENTENCE.memory_matrix_columns):
for count in range(0,numpy.int(math.floor(0.5*SENTENCE.ascii_matrix_columns))):
while True:
rand_num=numpy.random.random_integers(0,SENTENCE.ascii_matrix_columns-1)
if (numpy.isnan(connection_matrix[i,rand_num]))== False:
#if that connection is not already a NAN then set it as NAN and also increase the count, else just continue iterations without increasing the count
connection_matrix[i,rand_num]=numpy.nan
break
return (connection_matrix)
def Connection_Matrix_2(connection_matrix):
for third_dimension in range(0,SENTENCE.memory_matrix_columns):
for i in range(0,SENTENCE.memory_matrix_rows):
for count in range(0,numpy.int(math.floor(0.5*SENTENCE.memory_matrix_columns))):
while True:
rand_num=numpy.random.random_integers(0,SENTENCE.memory_matrix_columns-1)
if (numpy.isnan(connection_matrix[third_dimension,i,rand_num]))== False:
connection_matrix[third_dimension,i,rand_num]=numpy.nan
break
return (connection_matrix)
def __init__(self):
self.number_of_words=0 # number of words in a sentence
def RemovePunctuations(self,my_str):
#also converts it to lower case
self.my_str=my_str
# remove punctuations from the string
self.no_punct = ""
for char in my_str:
if char not in SENTENCE.punctuations:
self.no_punct = self.no_punct + char
# conver to lower case and display the unpunctuated string
self.no_punct_lower=self.no_punct.lower()
#print(self.no_punct_lower)
return (self.no_punct_lower)
def SeparateWords(self,my_str):
#self.my_str=my_str
#print('words separated')
#print(my_str.split())
return my_str.split()
def NumberofWords(self,my_str):
self.words=my_str #['how','are','you']
self.number_of_words= (len(self.words))
#print(self.words)
#input()
self.len_of_longestword=len(max(self.words,key=len))
#print (self.len_of_longestword)
#input('len of max word\n')
def Length_of_each_word(self,k):
self.length_of_each_word=[0]*self.number_of_words
for i in range (0,self.number_of_words):
self.length_of_each_word[i]=len(list(k[i]))
def Ascii_Matrix(self):
#self.ascii_matrix = [[[0 for k in range(SENTENCE.ascii_matrix_columns)] for j in range(self.len_of_longestword)] for i in range(self.number_of_words)]
self.ascii_matrix = numpy.zeros((self.number_of_words,self.len_of_longestword,SENTENCE.ascii_matrix_columns))
#pprint.pprint (self.ascii_matrix )
for i in range(0,self.number_of_words):
word=list(self.words[i]) # it has the current word under consideration
for j in range(0,self.length_of_each_word[i]):
bin_character=self.Converting_char_2binary(word[j])
self.ascii_matrix[i,j,:]=bin_character
#print('ascii matrix \n')
#pprint.pprint (self.ascii_matrix )
def Converting_char_2binary(self,x):
#x is a char input . FUNCTION TAKES A CHAR AND RETURNS ITS BINARY THAT I MADE
y=[0]*SENTENCE.ascii_matrix_columns
if ord(x)>=97 and ord(x)<=105:
m=0
n=97
elif ord(x)>=106 and ord(x)<=113:
m=1
n=106
elif ord(x)>=114 and ord(x)<=120:
m=2
n=114
elif ord(x)>=121 and ord(x)<=124:
m=3
n=121
y[m]=1
#print (y)
#print((ord(x)),n,m)
y[(ord(x)-n)+m+1]=1
#print (y)
return y
def replacing_nan_with_zero(connection_matrix):
matrx=connection_matrix+(connection_matrix*0)
#PYTHON DOESNT HAVE PASS BY VALUE, AND I DONT WANT IT TO MODULATTE CONNECTION MATRIX BY REPLACING NAN.
#THIS NEW MATRX IS RETURNED AND IS STORED IN CONNECTION_MATRIX_ZERO
if len(matrx.shape)==3:
for i in range(0,matrx.shape[0]):
for j in range(0,matrx.shape[1]):
for k in range(0,matrx.shape[2]):
if numpy.isnan(matrx[i,j,k]):
matrx[i,j,k]=0
elif len(matrx.shape)==2:
for i in range(0,matrx.shape[0]):
for j in range(0,matrx.shape[1]):
if numpy.isnan(matrx[i,j]):
matrx[i,j]=0
return matrx
def top_bit(vect_in,num):
temp= vect_in*0
if sum(vect_in)>0:
vect=vect_in+(vect_in*0)
#CREATED A NEW OBJECT SO THAT THE PASSED ORIGINAL VECTOR IS UNCHANGED
for count in range(0,num):
index_to_change=numpy.argmax(vect)
#temp[index_to_change]=vect[index_to_change]
temp[index_to_change]=1 # SETS TOP BITS 1
vect[index_to_change]=0
return (temp)
def row_column_from_flatinput(numberofrows,numberofcolumns,flatinput): # row is 0-m... column is 0-n
for r in range (0,numberofrows):
for c in range(0,numberofcolumns):
if flatinput==(r*numberofcolumns)+c:
row = r
column = c
return (row,column)
'''print(no_punct.split())
k=no_punct.split()
print(list(k[0]))
print(list(k[1]))
'''