-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.jl
376 lines (302 loc) · 10 KB
/
main.jl
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using Flux:LSTM,functor,Dense,Chain,params,ADAM,softmax,onehotbatch,DataLoader,gradient,update!,OneHotArray,gpu,onecold
using Statistics: mean
using Base:size,show
using Flux.Losses:logitcrossentropy
using NNlib:gather
#using CUDA
#using BSON:@load
import Flux.functor
import Base.size,Base.show
#include("model.jl")
#Embedding layer: (From Transformers.jl)
#
# """
# Embed(size::Int, vocab_size::Int)
# The Embedding Layer, `size` is the hidden size. `vocab_size` is the number of the vocabulary. Just a wrapper for embedding matrix.
# """
# abstract type AbstractEmbed{F} end
# struct Embed{F ,W <: AbstractArray{F}} <: AbstractEmbed{F}
# scale::F
# embedding::W
# end
# functor(e::Embed) = (e.embedding,), m -> Embed(e.scale, m...)
# size(e::Embed, s...) = size(e.embedding, s...)
# Embed(size::Int, vocab_size::Int; scale = one(Float32)) = Embed(Float32(scale), randn(Float32, size, vocab_size))
# function (e::Embed)(x::AbstractArray{Int})
# if isone(e.scale)
# gather(e.embedding, x)
# else
# e(x, e.scale)
# end
# end
# (e::Embed{F})(x, scale) where {F} = gather(e.embedding, x) .* convert(F, scale)
# (e::Embed)(x::Vector{Vector{Int64}}) = (e::Embed).(x)
# show(io::IO, e::Embed) = print(io, "Embed($(size(e.embedding, 1)))")
struct DiacritizedText
text::String
diacritization::Vector{Int}
end
#Diacritazation Hashing ===============
# diac_dict = Dict{Char,Int}(
# '\u064e' => 1,
# '\u064f' => 2,
# '\u0650' => 3,
# '\u0652' => 4,
# '\u064b' => 5,
# '\u064c' => 6,
# '\u064d' => 7,
# '\u0651' => 20,
# )
d_without_shadda = "\u064b\u064c\u064d\u064e\u064f\u0650\u0652"
function construct_diac_dict()
diac_dict=Dict{String,Int}()
diac_dict[""] = 0
counter = 1
for diac in d_without_shadda
diac_dict[string(diac)] = counter
counter += 1
end
for diac in d_without_shadda
diac_dict["\u0651"*diac] = counter
diac_dict[diac*"\u0651"] = counter
counter += 1
end
diac_dict["\u0651"] = counter
return diac_dict
end
diac_dict = construct_diac_dict()
function hash_diac(diac::String)
hash = 0
for char in diac
hash += diac_dict[char]
end
return hash
end
d = "\u064b\u064c\u064d\u064e\u064f\u0650\u0651\u0652"
d_re = r"[\u064b\u064c\u064d\u064e\u064f\u0650\u0651\u0652]"
function normalize_med(input)
capturing_regex_w = r"[و](?![\u064b\u064c\u064d\u064e\u064f\u0650\u0651\u0652])"
capturing_regex_y = r"[ي](?![\u064b\u064c\u064d\u064e\u064f\u0650\u0651\u0652])"
capturing_regex_a = r"[ا](?![\u064b\u064c\u064d\u064e\u064f\u0650\u0651\u0652])"
capturing_regex_a_m = r"[ى](?![\u064b\u064c\u064d\u064e\u064f\u0650\u0651\u0652])"
input = replace(input,capturing_regex_a => "اَ")
input = replace(input,capturing_regex_w => "اُ")
input = replace(input,capturing_regex_y => "اِ")
input = replace(input,capturing_regex_a_m => "اْ")
return input
end
function un_diacritize(input)
input = replace(input,r"[[:punct:]]"=>"")
input = replace(input,r"[\u060c]"=>"")
input = replace(input, "\u200f" => "")
input = replace(input,r"[0-9]"=>"")
input = replace(input, r"[ ][ ]+" => " ")
input = lstrip(input)
input = rstrip(input)
text = ""
diac = zeros(Int, length(input)-length(collect(eachmatch(d_re,input))))
input = split(input, "")
diac_count = 0
diac_str = ""
for char in input
if !occursin(char, d)
if diac_str != ""
diac[diac_count] = diac_dict[diac_str]
end
text *= char
diac_count += 1
diac_str = ""
else
diac_str *= char
end
end
if diac_str != ""
diac[diac_count] = diac_dict[diac_str]
end
return DiacritizedText(text,diac)
end
function diacritize(input)
output = ""
text = split(input.text, "")
for i=1:length(input.text)
output *= text[i]
output *= input.diacritization[i]
end
output
end
display(un_diacritize("قٌيِثً :"))
token_str = " \u0621\u0622\u0623\u0624\u0625\u0626\u0627\u0628\u0629\u062a\u062b\u062c\u062d\u062e\u062f\u0630\u0631\u0632\u0633\u0634\u0635\u0636\u0637\u0638\u0639\u063a\u0641\u0642\u0643\u0644\u0645\u0646\u0647\u0648\u0649\u064a"
function token_dict_constructor()
D = Dict{Char,Int}()
counter = Int(1)
for char in token_str
D[char] = counter
counter += Int(1)
end
return D
end
function un_tokenize_dict_constructor()
D = Dict{Int,Char}()
counter = Int(1)
for char in token_str
D[counter] = char
counter += Int(1)
end
return D
end
if !(@isdefined token_dict)
token_dict = token_dict_constructor()
un_token_dict = un_tokenize_dict_constructor()
end
function un_tokenize(word::Vector{Int})
str = ""
for letter in word
str *= un_token_dict[letter]
end
return str
end
function retype_data(line)
line_int = Array{Int,1}([token_dict[only(char)] for char in line])
return line_int
end
function partition_data(training_data)
training_data = un_diacritize.(training_data)
training_text = [retype_data(training_data[i].text) for i = 1:length(training_data)]
training_diac_in = [training_data[i].diacritization for i = 1:length(training_data)]
return training_text, training_diac_in
end
function partition_data_test(input)
diac = Vector{Vector{Int}}([])
text = retype_data(input.text)
cursor = 1
for word in text
diac = append!(diac, [input.diacritization[cursor:cursor+length(word)-1]])
cursor += length(word)
end
return text, diac
end
#Construct dataset, don't run if already defined
function pad_input_vector(vec,i,type)
return append!(vec,ones(type,i-length(vec)))
end
function pad_output_vector(vec,i,type)
return append!(vec,zeros(type,i-length(vec)))
end
function returned_lines(train, trunc)
lines = 0
for line in train[1]
line_over = false
ind = 0
while !line_over
if length(line) - ind <= trunc
lines += 1
break
end
#println("ASD")
ind = ind + 100
lines += 1
end
end
return lines
end
function data_trunc(train,trunc)
space_ind = findall(x->x==1,train[1][1])
space_ind = filter(x->x<=trunc,space_ind)
chosen_ind = maximum(space_ind)
length_of_data = returned_lines(train,trunc)
train_x = ones(Int,trunc,length_of_data)
train_y = zeros(Int,17,trunc,length_of_data)
N_ind = 1
for i in 1:length(train[1])
curr_ind = 1
while curr_ind < length(train[1][i])
if length(train[1][i])<=trunc
train_x[1:length(train[1][i]),N_ind] = train[1][i]
train_y[:,1:length(train[2][i]),N_ind] = onehotbatch(train[2][i][1:end],0:16)
N_ind += 1
break
end
space_ind = findall(x->x==1,train[1][i])
space_ind = append!(space_ind,length(train[1][i]))
space_ind = filter(x->x>=curr_ind,space_ind)
#display(space_ind)
space_ind = filter(x->x<=(trunc+curr_ind-1),space_ind)
#display(space_ind)
#display(length(train[1][i]))
chosen_ind = maximum(space_ind)
train_x[1:(chosen_ind-curr_ind+1),N_ind] = train[1][i][curr_ind:chosen_ind]
train_y[:,1:(chosen_ind-curr_ind+1),N_ind] = onehotbatch(train[2][i][curr_ind:chosen_ind],0:16)
curr_ind = chosen_ind + 1
N_ind += 1
end
end
return train_x,train_y
end
if !(@isdefined train)
train_s = open("train.txt") do file
read(file, String)
end
train_s = split(train_s, "\n")[1:end-1]
val_s = open("val.txt") do file
read(file, String)
end
val_s = split(val_s, "\n")[1:end-1]
#train_pre = partition_data(train_s[1:1000])
train = data_trunc(partition_data(train_s[1:100]), 300)
#train_pre = partition_data(train_s[1:1000])
#val = data_trunc(partition_data(val_s), 300)
end
train_loader = DataLoader((data=train[1],label=train[2]),batchsize=64,shuffle=true,partial=false)
#val_loader = DataLoader((data=val[1],label=val[2]),batchsize=128,shuffle=true)
display(model)
loss(x,y) = logitcrossentropy(model(x),y)
opt = ADAM(0.005)
function train_model(epochs, model)
#model = model |> gpu
ps = copy(params(model))
loss(x, y) = logitcrossentropy(model(x), y)
opt = ADAM(0.05)
train_error_arr = []
val_error_arr = []
train_error_current = 0.0
#val_error_current =0.
loader_length = length(train_loader)
for epoch = 1:epochs
prev_ps = copy(ps)
counter = 1
for (x, y) in train_loader
x_train = Array{Int}(x')
y_train = permutedims(y, [1, 3, 2])
display(size(model(Array{Int}(x'))))
display(size(permutedims(y, [1, 3, 2])))
gs = gradient(() -> loss(x_train, y_train), ps)
Flux.update!(opt, ps, gs)
train_error_current += loss(x_train,y_train)
counter += 1
end
# for (x,y) in val_loader
# x = gpu(x)
# y = gpu(y)
# val_error_current += loss(x,y)
# end
train_error_current /= length(train_loader)
#val_error_current = loss(gpu(val[1]),gpu(val[2]))
@info "Epoch :" epoch
@info "Training_Error :" train_error_current
#@info "Validation Error:" val_error_current
train_error_arr = append!(train_error_arr, train_error_current)
#val_error_arr = append!(val_error_arr,val_error_current)
# if (epoch > 1) && ((val_error_current > val_error_arr[end-1]) || (isapprox(val_error_current,val_error_arr[end-1])))
# @info "Validation error capped"
# #params(model) = prev_ps
# #break
# end
train_error_current = 0.0
#val_error_current =0.
end
return cpu(model), train_error_arr#,val_error_arr
end
mod2, train_err = train_model(2,model)
#train_model(10)
#@load "model2.bson" model
accuracy(x,y) = mean(onecold(model(x)) .== onecold(y))