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

Text generation recipe #258

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Changes from 9 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
8194f3f
Integrate ULMFiT (initial)
Chandu-4444 Jul 19, 2022
7662230
Add Paragraph to train_classifier
Chandu-4444 Jul 19, 2022
1418e50
Add batchseq to pad batch (naive version)
Chandu-4444 Jul 21, 2022
b682105
Remove Project.toml changes.
Chandu-4444 Jul 21, 2022
c1064bc
Add vocab_size to TextClassificationTask
Chandu-4444 Jul 24, 2022
345ced1
Add `vocab_size` to encodings
Chandu-4444 Jul 25, 2022
46b6826
Test `batches` integration with model.
Chandu-4444 Jul 25, 2022
388e8ac
Update load_batchseq function.
Chandu-4444 Jul 25, 2022
8bfe705
Clean up useless code from TextModels.jl.
Chandu-4444 Jul 29, 2022
2482aaa
Update FastText/src/models/pretrain_lm.jl
Chandu-4444 Aug 2, 2022
8bc930d
Update FastText/src/models/dataloader.jl
Chandu-4444 Aug 2, 2022
3882b1d
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 2, 2022
3057989
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 2, 2022
307fde1
Add `reset!` for AWD_LSTM.
Chandu-4444 Aug 2, 2022
075a21e
Add `textlearner`.
Chandu-4444 Aug 8, 2022
f16ec2c
Complere text classification pipeline.
Chandu-4444 Aug 8, 2022
3469630
Upadate `LanguageModel` to use `Flux.reset!`.
Chandu-4444 Aug 18, 2022
974a622
Include models.jl file.
Chandu-4444 Aug 23, 2022
8e9f7aa
Start text generation recipe for `imdb`
Chandu-4444 Aug 23, 2022
0d44bbd
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 23, 2022
4ad9a12
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 23, 2022
080a018
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 23, 2022
922334d
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 23, 2022
ce34be1
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 23, 2022
6cea902
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 23, 2022
5159fb8
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 23, 2022
c6b69f7
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 23, 2022
164fe21
Update FastText/src/models/custom_layers.jl
Chandu-4444 Aug 24, 2022
1ce6df2
Add suggestions and improvements from the call.
Chandu-4444 Aug 26, 2022
712d9c8
Use previous `VarDrop` code for using in colab.
Chandu-4444 Aug 28, 2022
6d6504b
Use NNlib for scalar indexing
Chandu-4444 Aug 29, 2022
5af6edb
Updates to Project.toml
Chandu-4444 Aug 31, 2022
6636393
Merge branch 'textmodel-integration' into text-generation-recipe
Chandu-4444 Aug 31, 2022
6cc9053
Update code to solve `getfield non-differentiable` error.
Chandu-4444 Sep 1, 2022
d0ee3a4
Add `TextGeneration` task
Chandu-4444 Sep 1, 2022
659e6d1
Modify type params for `LanguageModel` and
Chandu-4444 Sep 1, 2022
0a151b2
Update FastText/src/models/train_text_classifier.jl
Chandu-4444 Sep 1, 2022
12bc9ab
Update FastText/src/models/train_text_classifier.jl
Chandu-4444 Sep 1, 2022
35c345f
Update dtypes to avoid CuArray errors.
Chandu-4444 Sep 6, 2022
cceee46
Add callable TextClassifier
Chandu-4444 Sep 6, 2022
f7d51f6
Update FastText/src/models/custom_layers.jl
Chandu-4444 Sep 8, 2022
90c9a79
Update FastText/src/models/custom_layers.jl
Chandu-4444 Sep 8, 2022
7e7de6d
Update `Flux.reset!()`
Chandu-4444 Sep 12, 2022
aae4442
Merge branch 'textmodel-integration' into text-generation-recipe
Chandu-4444 Sep 13, 2022
c1418b3
Update few Flux.dropout functions.
Chandu-4444 Sep 13, 2022
2c04d19
Update code to avoid non-differentiable error
Chandu-4444 Sep 13, 2022
5b96c78
Merge branch 'textmodel-integration' into text-generation-recipe
Chandu-4444 Sep 16, 2022
9c60de6
Add batch generation for generation task.
Chandu-4444 Sep 19, 2022
3903bb2
Push to test on colab
Chandu-4444 Sep 21, 2022
d4aa13c
Add blockmodel for LanguageModel
Chandu-4444 Sep 21, 2022
fb69dc5
Fix `TextClassificationTask`
Chandu-4444 Sep 21, 2022
416a800
Replace `map` with `mapobs`
Chandu-4444 Sep 24, 2022
232f3bf
Update `onehot` encode for NumberVector
Chandu-4444 Sep 26, 2022
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
14 changes: 13 additions & 1 deletion FastText/src/models.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
function blockmodel(inblock::NumberVector, outblock::OneHotTensor, backbone; k = 10)

classifier = TextClassifier(backbone)
return (input) -> model(input, k = k, classifier = classifier)
return classifier
end


function (b::TextClassifier)(input)
k = 10
Zygote.ignore() do
Flux.reset!(b.rnn_layers)
[b.rnn_layers(x) for x in input[1:(end - k)]]
end

# bptt
model = b.linear_layers([b.rnn_layers(x) for x in input[(end - k + 1):end]])
end
20 changes: 10 additions & 10 deletions FastText/src/models/custom_layers.jl
Original file line number Diff line number Diff line change
@@ -27,15 +27,15 @@ Moreover this also follows the Vartional DropOut citeria, that is,
the drop mask is remains same for a whole training pass.
This is done by saving the masks in 'maskWi' and 'maskWh' fields
"""
mutable struct WeightDroppedLSTMCell{A,V,S}
mutable struct WeightDroppedLSTMCell{A,V,S,T}
Wi::A
Wh::A
b::V
h::S
c::S
p::Float32
active::Union{Bool,Nothing}
state0::Tuple{Matrix{Float32},Matrix{Float32}}
state0::T
end

function WeightDroppedLSTMCell(in::Integer, out::Integer, p::Float32 = 0.0f0;
@@ -92,15 +92,15 @@ julia> wd = WeightDroppedLSTM(4, 5, 0.3);
"""
function WeightDroppedLSTM(a...; kw...)
cell = WeightDroppedLSTMCell(a...; kw...)
maskWi = Flux.dropout_mask(Flux.rng_from_array(), cell.Wi, cell.p)
maskWh = Flux.dropout_mask(Flux.rng_from_array(), cell.Wh, cell.p)
maskWi = Flux.dropout_mask(Flux.rng_from_array(cell.Wi), cell.Wi, cell.p)
maskWh = Flux.dropout_mask(Flux.rng_from_array(cell.Wh), cell.Wh, cell.p)
hidden = (cell.state0..., maskWi, maskWh)
return Flux.Recur(cell, hidden)
end

function Flux.reset!(layer::Flux.Recur{<:WeightDroppedLSTMCell})
maskWi = Flux.dropout_mask(Flux.rng_from_array(), layer.cell.Wi, layer.cell.p)
maskWh = Flux.dropout_mask(Flux.rng_from_array(), layer.cell.Wh, layer.cell.p)
maskWi = Flux.dropout_mask(Flux.rng_from_array(layer.cell.Wi), layer.cell.Wi, layer.cell.p)
maskWh = Flux.dropout_mask(Flux.rng_from_array(layer.cell.Wh), layer.cell.Wh, layer.cell.p)
layer.state = (layer.cell.state0..., maskWi, maskWh)
return nothing
end
@@ -136,7 +136,7 @@ testmode!(m::VarDropCell, mode = true) =

function (vd::VarDropCell)((has_mask, mask), x)
if Flux._isactive(vd)
mask = has_mask ? mask : Flux.dropout_mask(Flux.rng_from_array(), x, vd.p)
mask = has_mask ? mask : Flux.dropout_mask(Flux.rng_from_array(x), x, vd.p)
return (true, mask), x .* mask
elseif !has_mask
return (has_mask, mask), x
@@ -180,10 +180,10 @@ To reset mask:
julia> reset_masks!(de)
"""
mutable struct DroppedEmbeddings{A,F}
mutable struct DroppedEmbeddings{A,F,M}
emb::A
p::F
mask::Vector{Float32}
mask::M
active::Union{Bool,Nothing}
end

@@ -264,4 +264,4 @@ function (a::PooledDense)(x)
meanpool = (sum(x, dims = 3) / size(x, 3))[:, :, 1]
hc = cat(x[:, :, 1], maxpool, meanpool, dims = 1)
σ.(W * hc .+ b)
end
end
2 changes: 1 addition & 1 deletion FastText/src/models/pretrain_lm.jl
Original file line number Diff line number Diff line change
@@ -50,4 +50,4 @@ function LanguageModel(load_pretrained::Bool = false, task::Any = Nothing; embed
return lm
end

Flux.@functor LanguageModel
Flux.@functor LanguageModel
5 changes: 3 additions & 2 deletions FastText/src/models/train_text_classifier.jl
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ linear_layers : contains Chain of two Dense layers [PooledDense and Dense] wit
To train create and instance and give it as first argument to 'train_classifier!' function
"""
mutable struct TextClassifier{A,F,Q}
struct TextClassifier{A,F,Q}
vocab::A
rnn_layers::F
linear_layers::Q
@@ -33,6 +33,7 @@ function TextClassifier(lm::LanguageModel = LanguageModel(), clsfr_out_sz::Integ
end

Flux.@functor TextClassifier
Flux.trainable(m::TextClassifier) = (rnn_layers = m.rnn_layers, linear_layers = m.linear_layers)

function loss(m, xs, y; k = 10)
# forward steps
@@ -57,4 +58,4 @@ function train_text_classifier(classifier::TextClassifier = Nothing, batches = N
Flux.Optimise.update!(opt, ps, gs)
end

end
end