Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated char rnn #194

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions text/char-rnn/Sample.txt
Original file line number Diff line number Diff line change
@@ -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
35 changes: 27 additions & 8 deletions text/char-rnn/char-rnn.jl
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@
#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)

m = gpu(m)

function loss(xs, ys)
l = sum(crossentropy.(m.(gpu.(xs)), gpu.(ys)))
Flux.truncate!(m)
Flux.reset!(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
Expand All @@ -51,9 +61,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