# How to build a bidirectional RNN with Flux?

**URL:** <https://discourse.julialang.org/t/how-to-build-a-bidirectional-rnn-with-flux/32021>\
**Category:** Machine Learning\
**Tags:** question, flux, machine-learning\
**Created:** [December 8, 2019, 4:47pm UTC](https://discourse.julialang.org/t/how-to-build-a-bidirectional-rnn-with-flux/32021 "2019-12-08T16:47:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rana\_Ian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rana_ian/32/13554_2.png) [@Rana\_Ian](https://discourse.julialang.org/u/Rana_Ian)\
**Post date:** [December 8, 2019, 4:47pm UTC](https://discourse.julialang.org/t/how-to-build-a-bidirectional-rnn-with-flux/32021/1 "2019-12-08T16:47:46Z")

</div>

Hi,

How would you build a bidirectional RNN with Flux? Drawing from a few examples

1. [Bidirectional LSTM example A](https://github.com/FluxML/Flux.jl/issues/866)
2. [Bidirectional LSTM example B](https://github.com/maetshju/flux-blstm-implementation/blob/master/01-blstm.jl)
3. [Knet bidirectional RNN source](https://github.com/denizyuret/Knet.jl/blob/master/src/rnn.jl#L781)

I’ve written

```Julia
using Pkg; for p in ["Flux"] Pkg.add(p) end
using Flux

# Bidirectional RNN
struct BRNN{L,D}
  forward :: L
  backward :: L
  dense :: D
end

Flux.@functor BRNN

function BRNN(in::Integer, hidden::Integer, out::Integer, σ = relu)
  return BRNN(
    RNN(in, hidden, σ), # forward
    RNN(in, hidden, σ), # backward
    Dense(2hidden, out, σ)
  )
end

function (m::BRNN)(xs)
  m.dense(vcat(m.forward(xs), m.backward(reverse(xs))))
end

inSize = 5
hiddenSize = 3
outSize = 1

trn = [(rand(inSize), rand(outSize)) for _ in 1:8]
@info "trn", summary(trn)

m = BRNN(inSize, hiddenSize, outSize)
loss(x, y) = Flux.mse(m(x), y)
ps = Flux.params(m)
opt = ADAM()

Flux.train!(loss, ps, trn, opt)

```

errors with

> ERROR: LoadError: Mutating arrays is not supported

Flux does not like reversing the input data.

```Julia
m.backward(reverse(xs))

```

Other examples ([A](https://github.com/FluxML/Flux.jl/issues/866), [B](https://github.com/maetshju/flux-blstm-implementation/blob/master/01-blstm.jl)) use

```Julia
Flux.flip(m.backward, xs)

```

but does not produce a compatible output shape for this example.

How would you approach it?

---

<div class="post-metadata">

**Author:** ![Rana\_Ian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rana_ian/32/13554_2.png) [@Rana\_Ian](https://discourse.julialang.org/u/Rana_Ian)\
**Post date:** [December 10, 2019, 6:31am UTC](https://discourse.julialang.org/t/how-to-build-a-bidirectional-rnn-with-flux/32021/2 "2019-12-10T06:31:15Z")

</div>

See Flux issue [Zygote errors building bidirectional RNN](https://github.com/FluxML/Flux.jl/issues/962)
