Skip to content

Commit

Permalink
example: fix parametric NDArray in lstm example (#370)
Browse files Browse the repository at this point in the history
and fix some depwarns

fix #368
  • Loading branch information
iblislin authored and pluskid committed Dec 10, 2017
1 parent 2d7cdc6 commit db09528
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
43 changes: 21 additions & 22 deletions examples/char-lstm/config.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
const DROPOUT = 0
const BATCH_SIZE = 32
const SEQ_LENGTH = 32
const DIM_HIDDEN = 256
const DIM_EMBED = 256
const LSTM_N_LAYER = 2
const N_EPOCH = 21
const BASE_LR = 0.01
const WEIGHT_DECAY = 0.00001
const CLIP_GRADIENT = 1
const NAME = :ptb
const N_GPU = 4
const USE_GPU = true
const DATA_TR_RATIO = 0.9
const CKPOINT_PREFIX = joinpath(dirname(@__FILE__), "checkpoints/$NAME")
const DROPOUT = 0
const BATCH_SIZE = 32
const SEQ_LENGTH = 32
const DIM_HIDDEN = 256
const DIM_EMBED = 256
const LSTM_N_LAYER = 2
const N_EPOCH = 21
const BASE_LR = 0.01
const WEIGHT_DECAY = 0.00001
const CLIP_GRADIENT = 1
const NAME = :ptb
const N_GPU = 1
const USE_GPU = true
const DATA_TR_RATIO = 0.9
const CKPOINT_PREFIX = joinpath(@__DIR__, "checkpoints/$NAME")

const BATCH_SIZE_SMP= 10
const SAMPLE_LENGTH = 100
const SAMPLE_START = 'a'

const UNKNOWN_CHAR = Char(0)
const INPUT_FILE = joinpath(dirname(@__FILE__), "input.txt")
const VOCAB_FILE = joinpath(dirname(@__FILE__), "vocab.dat")
const BATCH_SIZE_SMP = 10
const SAMPLE_LENGTH = 100
const SAMPLE_START = 'a'

const UNKNOWN_CHAR = Char(0)
const INPUT_FILE = joinpath(@__DIR__, "input.txt")
const VOCAB_FILE = joinpath(@__DIR__, "vocab.dat")
4 changes: 2 additions & 2 deletions examples/char-lstm/lstm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ mutable struct NLL <: mx.AbstractEvalMetric
NLL() = new(0.0, 0)
end

function mx.update!(metric :: NLL, labels :: Vector{mx.NDArray}, preds :: Vector{mx.NDArray})
function mx.update!(metric::NLL, labels::Vector{<:mx.NDArray}, preds::Vector{<:mx.NDArray})
@assert length(labels) == length(preds)
nll = 0.0
for (label, pred) in zip(labels, preds)
@mx.nd_as_jl ro=(label, pred) begin
nll -= sum(log(max(broadcast_getindex(pred, round(Int,label+1), 1:length(label)), 1e-20)))
nll -= sum(log.(max.(broadcast_getindex(pred, round.(Int,label+1), 1:length(label)), 1e-20)))
end
end

Expand Down
6 changes: 3 additions & 3 deletions examples/char-lstm/sampler.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include(joinpath(dirname(@__FILE__), "config.jl"))
include(joinpath(dirname(@__FILE__), "lstm.jl"))
include(joinpath(dirname(@__FILE__), "seq-data.jl"))
include(joinpath(@__DIR__, "config.jl"))
include(joinpath(@__DIR__, "lstm.jl"))
include(joinpath(@__DIR__, "seq-data.jl"))

using StatsBase
using MXNet
Expand Down
8 changes: 4 additions & 4 deletions examples/char-lstm/seq-data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ end
#--/provide

#--eachbatch-part1
function mx.eachbatch(p :: CharSeqProvider)
function mx.eachbatch(p::CharSeqProvider)
data_all = [mx.zeros(shape) for (name, shape) in mx.provide_data(p)]
label_all = [mx.zeros(shape) for (name, shape) in mx.provide_label(p)]

Expand All @@ -73,7 +73,7 @@ function mx.eachbatch(p :: CharSeqProvider)

#--eachbatch-part2
#...
function _text_iter()
function _text_iter(c::Channel)
text = p.text

n_batch = floor(Int, length(text) / p.batch_size / p.seq_len)
Expand All @@ -100,11 +100,11 @@ function mx.eachbatch(p :: CharSeqProvider)
copy!(label_all[i], label_jl[i])
end

produce(batch)
put!(c, batch)
end
end

return Task(_text_iter)
return Channel(_text_iter)
end
#--/eachbatch-part2

Expand Down
8 changes: 4 additions & 4 deletions examples/char-lstm/train.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include(joinpath(dirname(@__FILE__), "config.jl"))
include(joinpath(dirname(@__FILE__), "lstm.jl"))
include(joinpath(dirname(@__FILE__), "seq-data.jl"))
include(joinpath(@__DIR__, "config.jl"))
include(joinpath(@__DIR__, "lstm.jl"))
include(joinpath(@__DIR__, "seq-data.jl"))

# build vocabulary
vocab = build_vocabulary(INPUT_FILE, VOCAB_FILE)
Expand Down Expand Up @@ -29,7 +29,7 @@ data_val = CharSeqProvider(text_val, BATCH_SIZE, SEQ_LENGTH, vocab, NAME,
if USE_GPU
context = [mx.gpu(i) for i = 0:N_GPU-1]
else
context = [mx.cpu()]
context = mx.cpu()
end

#--train
Expand Down

0 comments on commit db09528

Please sign in to comment.