From b0ee299c24380876ada1d7163b228ba1d5a2f53a Mon Sep 17 00:00:00 2001 From: Adarsh Kumar <45385384+AdarshKumar712@users.noreply.github.com> Date: Sat, 18 Jan 2020 13:37:52 +0530 Subject: [PATCH 1/4] Update char-rnn.jl Updated char-rnn.jl for Flux v0.10 as some methods became obsolete. Also added the sample.txt file as an example of the model generation results. --- text/char-rnn/char-rnn.jl | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/text/char-rnn/char-rnn.jl b/text/char-rnn/char-rnn.jl index 0db14463d..5fee9fe32 100644 --- a/text/char-rnn/char-rnn.jl +++ b/text/char-rnn/char-rnn.jl @@ -1,29 +1,37 @@ +#Declaration of Packages used using Flux -using Flux: onehot, chunk, batchseq, throttle, crossentropy +using Flux: onehot, chunk, batchseq, throttle, crossentropy,@epochs using StatsBase: wsample using Base.Iterators: partition cd(@__DIR__) +#Check for the input.txt file and if doesn't exist then downloads the text file isfile("input.txt") || download("https://cs.stanford.edu/people/karpathy/char-rnn/shakespeare_input.txt", "input.txt") +#Text Preprocessing text = collect(String(read("input.txt"))) alphabet = [unique(text)..., '_'] text = map(ch -> onehot(ch, alphabet), text) stop = onehot('_', alphabet) -N = length(alphabet) -seqlen = 50 +N = length(alphabet) # +seqlen = 20 nbatch = 50 +epo = 1 +#Partioning the data into batches of chunks for Training Xs = collect(partition(batchseq(chunk(text, nbatch), stop), seqlen)) Ys = collect(partition(batchseq(chunk(text[2:end], nbatch), stop), seqlen)) +#Defining the model m = Chain( - LSTM(N, 128), - LSTM(128, 128), + #Input of Shape N*M where M is the length of Input batch + LSTM(N, 128,relu), + LSTM(128, 256), + LSTM(256,128,relu), Dense(128, N), softmax) @@ -31,15 +39,16 @@ m = gpu(m) function loss(xs, ys) l = sum(crossentropy.(m.(gpu.(xs)), gpu.(ys))) - Flux.truncate!(m) return l end +#Compilation parameters opt = ADAM(0.01) tx, ty = (Xs[5], Ys[5]) evalcb = () -> @show loss(tx, ty) -Flux.train!(loss, params(m), zip(Xs, Ys), opt, +#Training the model +@epochs epo Flux.train!(loss, params(m), zip(Xs, Ys), opt, cb = throttle(evalcb, 30)) # Sampling @@ -51,9 +60,18 @@ function sample(m, alphabet, len) c = rand(alphabet) for i = 1:len write(buf, c) - c = wsample(alphabet, m(onehot(c, alphabet)).data) + c = wsample(alphabet, m(onehot(c, alphabet))) end return String(take!(buf)) end sample(m, alphabet, 1000) |> println + +#Saving the text generated by our model to Sample.txt file +open("Sample.txt", "w") do f + write(f, sample(m, alphabet, 1000)) +end + + + + From 48e218a9bbbde29b6c219f4cf302306bfcf84670 Mon Sep 17 00:00:00 2001 From: Adarsh Kumar <45385384+AdarshKumar712@users.noreply.github.com> Date: Sat, 18 Jan 2020 13:39:28 +0530 Subject: [PATCH 2/4] Added Sample.txt file --- text/char-rnn/Sample.txt | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 text/char-rnn/Sample.txt diff --git a/text/char-rnn/Sample.txt b/text/char-rnn/Sample.txt new file mode 100644 index 000000000..0901586c7 --- /dev/null +++ b/text/char-rnn/Sample.txt @@ -0,0 +1,29 @@ +kt el! + +nPEDNRONIO: +Plamincay likind, lawak gunse in'd the is +rn? + +SORRCARYINCHNITESNZCY , heenthe ly! srius dne deatshy, looditifn tinfow, inf the rhou gicen.'ldnes'd my nounty, and was yay! +Od; folne his the tict you, I but so up trpllary, +If' my ohour gugooher nendce. + +OTLTINAS: +Howg, comet l tn'Taeld, +Thisss be your. Pilit? + +PROTOCK arit, bayoigabansk upeielss end thmngwe good be ad: st wal patcirpurpeoy Cadvalt sirprupt, +Tiupord +Fed, and of taestlingit stedu't lonid that we dvou wa dore. + +LASIUS: +GiniI, litn! nof lirithid not be eseadlcord: Y: I Illel ookd surl! reenniondnd thint lowg'nW roff: or spothortwith. lold treotled, an,ist, with them tratco; +That! thy mator is wit? +Wece and k nos the and langoS: +If Perme and at sord's sigff, abeied, +Frethese burepul! +Thing; +Harla you st thBIs iuwhich det t! +Oluld, +Nall cimen all gurnus mour to byou coveny sion, ray, the wif, rivennnolavus? soneldemt ned lirwoyit a dohicthe to ove brordut weld of are sordwll of licumencine, alis, +Sto ut, P \ No newline at end of file From c75584220de43115c95f5cd6e993b2c8ecc0d085 Mon Sep 17 00:00:00 2001 From: Adarsh Kumar <45385384+AdarshKumar712@users.noreply.github.com> Date: Mon, 20 Jan 2020 16:44:59 +0530 Subject: [PATCH 3/4] Update char-rnn.jl --- text/char-rnn/char-rnn.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/text/char-rnn/char-rnn.jl b/text/char-rnn/char-rnn.jl index 5fee9fe32..2404930a1 100644 --- a/text/char-rnn/char-rnn.jl +++ b/text/char-rnn/char-rnn.jl @@ -39,6 +39,7 @@ m = gpu(m) function loss(xs, ys) l = sum(crossentropy.(m.(gpu.(xs)), gpu.(ys))) + Flux.reset!(l) return l end From 7d80b9e635d1aedf98355dc92b373535658dfc6f Mon Sep 17 00:00:00 2001 From: Adarsh Kumar <45385384+AdarshKumar712@users.noreply.github.com> Date: Mon, 20 Jan 2020 16:46:54 +0530 Subject: [PATCH 4/4] Update char-rnn.jl --- text/char-rnn/char-rnn.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/char-rnn/char-rnn.jl b/text/char-rnn/char-rnn.jl index 2404930a1..1daf519f6 100644 --- a/text/char-rnn/char-rnn.jl +++ b/text/char-rnn/char-rnn.jl @@ -39,7 +39,7 @@ m = gpu(m) function loss(xs, ys) l = sum(crossentropy.(m.(gpu.(xs)), gpu.(ys))) - Flux.reset!(l) + Flux.reset!(m) return l end