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

testing multilayer mgu #4

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ version = "0.1.0"

[deps]
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[compat]
Flux = "0.14"
Zygote = "0.6.72"
julia = "1.10"

[extras]
Expand Down
2 changes: 2 additions & 0 deletions src/RecurrentLayers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ include("lightru_cell.jl")
include("rhn_cell.jl")
include("nas_cell.jl")

include("utils.jl")

end #module
46 changes: 29 additions & 17 deletions src/mgu_cell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,42 @@ Base.show(io::IO, mgu::MGUCell) =


struct MGU{M}
cell::M
cells::Vector{M}
dropout::Real
end

Flux.@layer :expand MGU

"""
MGU((in, out)::Pair; init = glorot_uniform, bias = true)
MGU((in_size, out_size)::Pair; n_layers = 1, dropout = 0.0, init = glorot_uniform, bias = true)
"""
function MGU((in, out)::Pair; init = glorot_uniform, bias = true)
cell = MGUCell(in => out; init, bias)
return MGU(cell)
function MGU((in_size, out_size)::Pair;
n_layers::Int=1,
dropout::Float64=0.0,
kwargs...)
cells = []
for i in 1:n_layers
tin_size = i == 1 ? in_size : out_size
push!(cells, MGUCell(tin_size => out_size; kwargs...))
end
return MGU(cells, dropout)
end

function (mgu::MGU)(inp)
state = zeros_like(inp, size(mgu.cell.Wh, 2))
return mgu(inp, state)
# Forward pass without initial state
function (mgu::MGU)(input)
batch_size = size(input, 3)
state = [zeros(size(mgu.cells[i].Wh, 2), batch_size) for i in 1:length(mgu.cells)]
return mgu(input, state)
end
function (mgu::MGU)(inp, state)

function (mgu::MGU)(inp, initial_states)
@assert ndims(inp) == 2 || ndims(inp) == 3
new_state = []
for inp_t in eachslice(inp, dims=2)
state = mgu.cell(inp_t, state)
new_state = vcat(new_state, [state])
end
return stack(new_state, dims=2)
num_layers = length(mgu.cells)
foldl((acc, idx) -> begin
(layer_input, states) = acc
cell = mgu.cells[idx]
layer_output, new_state = _process_layer(layer_input, states[idx], cell)
updated_states = vcat(states[1:idx-1], [new_state], states[idx+1:end])
return layer_output, updated_states
end, 1:num_layers, init=(inp, initial_states))[1]
end
9 changes: 9 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function _process_layer(layer_input, state, cell)
new_states = map(eachslice(layer_input, dims=2)) do inp_t
state = cell(inp_t, state)
state
end

layer_output = stack(new_states, dims=2)
return layer_output, state
end
Loading