-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
217 lines (163 loc) · 5.93 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
import sys, os
import tensorflow as tf
import numpy as np
from definedChars.charDict import CharDictionary
import os
import random as rand
from datetime import datetime
dataset_path = '../data/wiki-2014-02.ver'
is_ascii = lambda s: len(s) == len(s.encode())
def getTokens():
tokens = {}
with open('../tokeny_vyznam') as f:
for line in f:
arr = line.split('-')
# print(arr)
arr[-1] = arr[-1].strip()
tokens[arr[0]] = { 'name':'', 'number' : -1}
tokens[arr[0]]['name'] = arr[1]
tokens[arr[0]]['number'] = int(arr[2])
return tokens
def getVectorFromWord(word, charDict):
vec = []
for char in word:
char_vec = charDict.getVector(char)
vec.append(char_vec)
return vec
def rand_seek(file):
rand.seed(datetime.now())
statinfo = os.stat(file.fileno())
size = statinfo.st_size
file.seek(rand.randint(0,size))
try:
next(file)
except:
file.seek(0)
def getNextBatch(file, dictObj, definedTokens, n=10, randomSeek=False):
data_x, data_y = [],[]
lines = []
max_len = 0
if randomSeek:
rand_seek(file)
while len(data_x) < n:
line = file.readline()
if line == '':
file.seek(0)
continue
if '<' in line: # this is not valid line, so find next valid
continue
columns = line.split('\t')
word = columns[0]
lemma = columns[1]
information = columns[2]
if len(word) > max_len:
max_len = len(word)
tmp_list = []
for char in word:
tmp_list.append(dictObj.getVector(char))
data_x.append(tmp_list)
position = definedTokens[information[0]]['number'] - 1
tmp_list = [0]*19
tmp_list[position] = 1
data_y.append(tmp_list)
for w in data_x:
for i in range(max_len - len(w)):
w.append([0] * dictObj.len)
return data_x, data_y
# dictionary containing methods for getting vector from one character
# all characters are defined in file 'definedChars/chars'
charDict = CharDictionary()
# get ionary of all tokens
tokens = getTokens()
# Open dataset for reading words from file
dataset = open(dataset_path)
if '--vyznamy_tokenov' in sys.argv:
for token in tokens:
print('{} {} {}'.format(token, tokens[token]['number'], tokens[token]['name']))
exit(0)
######################## NEURAL NETWORK ####################################
epochs = 150
batch_size = 4
num_hidden = 32
# no_of_batches = int(len(data_x) / batch_size
save_dir = "saved/{}hidden".format(num_hidden)
save_path = "saved/{}hidden/model".format(num_hidden)
# Create directory if necessary
if not os.path.exists(save_dir):
os.makedirs(save_dir)
print("Creating Computational Graph")
# [batch_size, sequence_length, size_of_vector]
data = tf.placeholder(tf.float32, [None, None, charDict.len])
target = tf.placeholder(tf.float32, [None, 19])
cell = tf.contrib.rnn.LSTMCell(num_hidden, state_is_tuple=True)
val, state = tf.nn.dynamic_rnn(
cell,
data,
dtype=tf.float32
)
transposed = tf.transpose(val, [1, 0, 2])
last = tf.gather(transposed, tf.shape(val)[1] - 1)
weight = tf.Variable(tf.truncated_normal([num_hidden, int(target.get_shape()[1])]), name='weights')
bias = tf.Variable(tf.constant(0.1, shape=[target.get_shape()[1]]), name='biases')
multiplication = tf.matmul(last, weight) + bias
prediction = tf.nn.softmax(multiplication)
cross_entropy = -tf.reduce_sum(target * tf.log(prediction))
optimizer = tf.train.AdamOptimizer(0.01)
minimize = optimizer.minimize(cross_entropy)
mistakes = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1))
error = tf.reduce_mean(tf.cast(mistakes, tf.float32))
init = tf.global_variables_initializer()
saver = tf.train.Saver()
# -----------------------------------------
if '--get-tag' in sys.argv:
word = sys.argv[2]
word_vec = getVectorFromWord(word, charDict)
with tf.Session() as sess:
try:
saver.restore(sess, 'saved/{}hidden/model'.format(num_hidden))
except:
print("exception")
sess.run(init)
result = np.around(sess.run(prediction, {data: [word_vec]}), decimals=2)
print(result)
print(tokens)
exit(1)
#----------------------------------------
if '--performance' in sys.argv:
successful_marked = 0
total = 10000
rand_seek(dataset)
with tf.Session() as sess:
try:
saver.restore(sess, 'saved/{}hidden/model'.format(num_hidden))
except:
print("exception")
sess.run(init)
inp, out = getNextBatch(dataset, charDict, tokens, n=total)
for i in range(len(inp)):
m_pred = sess.run(prediction, {data: [inp[i]]})
if np.argmax(m_pred) == np.argmax(out[i]):
successful_marked = successful_marked + 1
percents = (successful_marked * 100) / total
print(percents)
exit(1)
#----------------------------------------
with tf.Session() as sess:
try:
saver.restore(sess, 'saved/{}hidden/model'.format(num_hidden))
except:
print("exception")
sess.run(init)
for e in range(10000):
# Get tensor, send to placeholder and minimize error
inp, out = getNextBatch(dataset, charDict, tokens, n=batch_size)
vysledok = sess.run(minimize, {data: inp, target: out })
# Save model and try evaluate successfulness
if e % 101 == 0 :
saver.save(sess, 'saved/{}hidden/model'.format(num_hidden))
tmp_cross_entr, tmp_err = sess.run([cross_entropy, error], {data: inp, target: out })
print("Saving model", tmp_cross_entr, tmp_err)
valid_x, valid_y = getNextBatch(dataset, charDict, tokens, 1, True)
m_pred = sess.run([prediction], {data: valid_x, target: valid_y })
# Print if prediction match target
print("{} === {}".format(np.argmax(m_pred),np.argmax(valid_y)))