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

Double return hotfix and adding scan #26

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Changes from 1 commit
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
Next Next commit
starting to add scan to recurrent layers
  • Loading branch information
MartinuzziFrancesco committed Dec 13, 2024
commit 3f2b61d66ef79642ed3a9319734bfc4b9daae520
18 changes: 4 additions & 14 deletions src/fastrnn_cell.jl
Original file line number Diff line number Diff line change
@@ -112,12 +112,7 @@ end

function (fastrnn::FastRNN)(inp, state)
@assert ndims(inp) == 2 || ndims(inp) == 3
new_state = []
for inp_t in eachslice(inp, dims=2)
state = fastrnn.cell(inp_t, state)
new_state = vcat(new_state, [state])
end
return stack(new_state, dims=2)
return scan(fastrnn.cell, inp, state)
end


@@ -173,7 +168,7 @@ function FastGRNNCell((input_size, hidden_size)::Pair, activation=tanh_fast;
zeta = randn(Float32)
nu = randn(Float32)

return FastGRNNCell(Wi, Wh, b, alpha, beta, activation)
return FastGRNNCell(Wi, Wh, b, zeta, nu, activation)
end

function (fastgrnn::FastGRNNCell)(inp::AbstractVecOrMat, state)
@@ -182,7 +177,7 @@ function (fastgrnn::FastGRNNCell)(inp::AbstractVecOrMat, state)

# get variables
Wi, Wh, b = fastgrnn.Wi, fastgrnn.Wh, fastgrnn.bias
alpha, beta = fastgrnn.alpha, fastgrnn.beta
zeta, nu = fastgrnn.zeta, fastgrnn.nu
bh, bz = chunk(b, 2)
partial_gate = Wi * inp .+ Wh * state

@@ -240,10 +235,5 @@ end

function (fastgrnn::FastGRNN)(inp, state)
@assert ndims(inp) == 2 || ndims(inp) == 3
new_state = []
for inp_t in eachslice(inp, dims=2)
state = fastgrnn.cell(inp_t, state)
new_state = vcat(new_state, [state])
end
return stack(new_state, dims=2)
return scan(fastgrnn.call, inp, state)
end
7 changes: 1 addition & 6 deletions src/indrnn_cell.jl
Original file line number Diff line number Diff line change
@@ -92,10 +92,5 @@ end

function (indrnn::IndRNN)(inp, state)
@assert ndims(inp) == 2 || ndims(inp) == 3
new_state = []
for inp_t in eachslice(inp, dims=2)
state = indrnn.cell(inp_t, state)
new_state = vcat(new_state, [state])
end
return stack(new_state, dims=2)
return scan(indrnn.cell, inp, state)
end
7 changes: 1 addition & 6 deletions src/lightru_cell.jl
Original file line number Diff line number Diff line change
@@ -101,10 +101,5 @@ end

function (lightru::LightRU)(inp, state)
@assert ndims(inp) == 2 || ndims(inp) == 3
new_state = []
for inp_t in eachslice(inp, dims=2)
state = lightru.cell(inp_t, state)
new_state = vcat(new_state, [state])
end
return stack(new_state, dims=2)
return scan(lightru.cell, inp, state)
end
7 changes: 1 addition & 6 deletions src/ligru_cell.jl
Original file line number Diff line number Diff line change
@@ -101,12 +101,7 @@ end

function (ligru::LiGRU)(inp, state)
@assert ndims(inp) == 2 || ndims(inp) == 3
new_state = []
for inp_t in eachslice(inp, dims=2)
state = ligru.cell(inp_t, state)
new_state = vcat(new_state, [state])
end
return stack(new_state, dims=2)
return scan(ligru.cell, inp, state)
end


7 changes: 1 addition & 6 deletions src/mgu_cell.jl
Original file line number Diff line number Diff line change
@@ -100,10 +100,5 @@ end

function (mgu::MGU)(inp, state)
@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)
return scan(mgu.cell, inp, state)
end
21 changes: 3 additions & 18 deletions src/mut_cell.jl
Original file line number Diff line number Diff line change
@@ -104,12 +104,7 @@ end

function (mut::MUT1)(inp, state)
@assert ndims(inp) == 2 || ndims(inp) == 3
new_state = []
for inp_t in eachslice(inp, dims=2)
state = mut.cell(inp_t, state)
new_state = vcat(new_state, [state])
end
return stack(new_state, dims=2)
return scan(mut.cell, inp, state)
end


@@ -218,12 +213,7 @@ end

function (mut::MUT2)(inp, state)
@assert ndims(inp) == 2 || ndims(inp) == 3
new_state = []
for inp_t in eachslice(inp, dims=2)
state = mut.cell(inp_t, state)
new_state = vcat(new_state, [state])
end
return stack(new_state, dims=2)
return scan(mut.cell, inp, state)
end


@@ -330,10 +320,5 @@ end

function (mut::MUT3)(inp, state)
@assert ndims(inp) == 2 || ndims(inp) == 3
new_state = []
for inp_t in eachslice(inp, dims=2)
state = mut.cell(inp_t, state)
new_state = vcat(new_state, [state])
end
return stack(new_state, dims=2)
return scan(mut.cell, inp, state)
end