-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
296 lines (228 loc) · 9.38 KB
/
main.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
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
# https://www.kaggle.com/code/alincijov/nlp-starter-continuous-bag-of-words-cbow/
import re
import numpy as np
import string
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from operator import itemgetter
from subprocess import check_output
epochs = 50
embed_dim = 100 # 2 * sqrt(tokenizer.sentences.sz)
context_wnd = 4 # 2, 3 or 4: [(context_wnd), target]
# - the word embeddings as inputs (idx)
# - the linear model as the hidden layer
# - the log_softmax as the output
class Sentencizer: #from NLPTools
def __init__(self, split_characters=['.', '?', '!', ':', ';', ','], delimiter_token='<split>'):
self.sentences = []
self._split_characters = split_characters
self._delimiter_token = delimiter_token
self._index = 0
self._stopwords = [line.replace('\n', '') for line in open("stopwords.txt", 'r', encoding='utf-8').readlines()]
self.vocab = set()
self.vocab_freq = {}
self.vocab_freq_sorted = {}
def sentencize(self, input_line):
work_sentence = input_line.strip()
sentences = []
if (work_sentence == ""):
return
for character in self._split_characters:
work_sentence = work_sentence.replace("\n", " ")
work_sentence = work_sentence.replace(character, character + "" + self._delimiter_token)
sentences = [x.strip().lower() for x in work_sentence.split(self._delimiter_token) if x !='']
token_boundaries = [' ', ',', '.']
for i in range(len(sentences)):
work_sentence = sentences[i]
for delimiter in token_boundaries:
work_sentence = work_sentence.replace(delimiter, self._delimiter_token)
sentences[i] = [x.strip() for x in work_sentence.split(self._delimiter_token) if (x != '')]
work_sentence = []
for w in sentences[i]:
w = w.strip(string.punctuation)
if ((w != '') and (w not in self._stopwords) and not w.isdigit()):
work_sentence.append(w)
if w in self.vocab_freq:
self.vocab_freq[w] += 1
continue
# print (word, vocab[word])
self.vocab_freq[w] = 1
if (len(work_sentence) > 0):
#print(' '.join(sentences[i]))
#print(' '.join(work_sentence))
self.sentences.append(work_sentence)
self.vocab.update(set(work_sentence))
#print(self.vocab)
def __iter__(self):
return self
def __next__(self):
if self._index < len(self.sentences):
result = self.sentences[self._index]
self._index += 1
return result
raise StopIteration
def readFile(self, filename):
f = open(filename, 'r', encoding='utf-8')
count = 0;
while True:
line = f.readline()
if not line:
break;
count+=1
self.sentencize(line)
f.close()
self.vocab = sorted(self.vocab)
self.vocab_freq_sorted = sorted(self.vocab_freq.items(), key=itemgetter(1), reverse=True)
sentences = """We are about to study the idea of computational process.
Computational processes are abstract beings that inhabit computers.
As they evolve, processes manipulate other abstract things called data.
The evolution of a process is directed by a pattern of rules
called a program. People create programs to direct processes. In effect,
we conjure the spirits of the computer with our spells."""
tokenizer = Sentencizer()
#tokenizer.sentencize(sentences)
tokenizer.readFile("train-nn.txt")
vocab_size = len(tokenizer.vocab)
word_to_ix = {word: i for i, word in enumerate(tokenizer.vocab)}
ix_to_word = {i: word for i, word in enumerate(tokenizer.vocab)}
# data - [(context), target]
data = []
############
for sentence in tokenizer.sentences:
if (context_wnd == 4):
for i in range(2, len(sentence) - 2):
context = [sentence[i - 2], sentence[i - 1], sentence[i + 1], sentence[i + 2]]
target = sentence[i]
data.append((context, target))
for i in range(0, len(sentence) - 4):
context = [sentence[i + 1], sentence[i + 2], sentence[i + 3], sentence[i + 4]]
target = sentence[i]
data.append((context, target))
if (context_wnd == 3):
for i in range(0, len(sentence) - 3):
context = [sentence[i + 1], sentence[i + 2], sentence[i + 3]]
target = sentence[i]
data.append((context, target))
#print("#" + target + " : " + context[0] + ", " + context[1] + ", " + context[2])
#context = [sentence[i], sentence[i + 1], sentence[i + 3]]
#target = sentence[i + 2]
#data.append((context, target))
#print("#" + target + " : " + context[0] + ", " + context[1] + ", " + context[2])
#context = [sentence[i], sentence[i + 2], sentence[i + 3]]
#target = sentence[i + 1]
#data.append((context, target))
#print("#" + target + " : " + context[0] + ", " + context[1] + ", " + context[2])
if len(sentence) > 3:
context = [sentence[-4], sentence[-3], sentence[-2]]
target = sentence[-1]
data.append((context, target))
if (context_wnd == 2):
for i in range(0, len(sentence) - 2):
context = [sentence[i], sentence[i + 1]]
target = sentence[i + 2]
data.append((context, target))
#print("#" + target + " : " + sentence[i + 1] + ", " + sentence[i + 2])
############
print("Tokens:", str(len(data)))
#print(str(len(tokenizer.sentences)))
# Embeddings
embeddings = np.random.random_sample((vocab_size, embed_dim))
# Training:
theta = np.random.uniform(-1, 1, (context_wnd * embed_dim, vocab_size))
# Linear-model
def linear(m, theta):
w = theta
return m.dot(w)
# Log softmax + NLLloss = Cross Entropy
def log_softmax(x):
e_x = np.exp(x - np.max(x))
return np.log(e_x / e_x.sum())
def NLLLoss(logs, targets):
out = logs[range(len(targets)), targets]
return -out.sum()/len(out)
def grad_softmax_crossentropy_with_logits(logits, target):
one_hot = np.zeros_like(logits)
one_hot[np.arange(len(logits)), target] = 1
softmax = np.exp(logits) / np.exp(logits).sum(axis=-1, keepdims=True)
return (-one_hot + softmax) / logits.shape[0]
# Forward propagation
def forward(context_idxs, theta):
m = embeddings[context_idxs].reshape(1, -1)
n = linear(m, theta)
o = log_softmax(n)
return m, n, o
# Backward propagation
def backward(preds, theta, target_idxs):
m, n, o = preds
dlog = grad_softmax_crossentropy_with_logits(n, target_idxs)
dw = m.T.dot(dlog)
return dw
def optimize(theta, grad, lr = 0.03):
theta -= grad * lr
return theta
##########################
epoch_losses = {}
success = []
##########################
data_ids = []
for context, target in data:
context_ids = np.array([word_to_ix[w] for w in context])
target_ids = np.array([word_to_ix[target]])
data_ids.append((context_ids, target_ids))
##########################
for epoch in range(epochs):
losses = []
hits = 0
for context_ids, target_ids in data_ids:
preds = forward(context_ids, theta)
loss = NLLLoss(preds[-1], target_ids)
losses.append(loss)
grad = backward(preds, theta, target_ids)
theta = optimize(theta, grad, lr=0.03)
##########################################
word_id = np.argmax(preds[-1])
if (word_id == target_ids): hits += 1
##########################################
epoch_losses[epoch] = losses
success.append(hits/len(data) * 100.0)
print("<< " + str(epoch) + " : " + str(hits/len(data) * 100.0))
# Analyze: Plot loss/epoch
def plot_loss():
ix = np.arange(0, epochs)
fig = plt.figure()
fig.suptitle('Epoch/Losses', fontsize=20)
plt.plot(ix,[epoch_losses[i][0] for i in ix])
plt.xlabel('Epochs', fontsize=12)
plt.ylabel('Losses', fontsize=12)
fig.show()
def plot_precision():
ix = np.arange(0, epochs)
fig = plt.figure()
fig.suptitle('Epoch/Precision%, '+str(int(success[epochs-1])), fontsize=20)
plt.plot(ix,[success[i] for i in ix])
plt.xlabel('Epochs', fontsize=12)
plt.ylabel('Precision,%', fontsize=12)
fig.show()
def predict(words):
context_idxs = np.array([word_to_ix[w] for w in words])
preds = forward(context_idxs, theta)
word = ix_to_word[np.argmax(preds[-1])]
return word
def verify():
sz = len(data)
success = 0;
for context, target in data:
context_idxs = np.array([word_to_ix[w] for w in context])
preds = forward(context_idxs, theta)
word_id = np.argmax(preds[-1])
word = ix_to_word[word_id]
target_id = word_to_ix[target]
if (word_id == target_id) : success += 1
print("sucess:", str(100.0 * success/sz))
plot_loss()
plot_precision()
verify()
# (['evolve', 'processes', 'abstract', 'things'], 'manipulate')
#w = predict(['evolve', 'processes', 'abstract', 'things'])
#print(w)