# Sequence-to-one modelling and Flux.reset!

**URL:** <https://discourse.julialang.org/t/sequence-to-one-modelling-and-flux-reset/70815>\
**Category:** New to Julia\
**Tags:** flux\
**Created:** [November 2, 2021, 3:57pm UTC](https://discourse.julialang.org/t/sequence-to-one-modelling-and-flux-reset/70815 "2021-11-02T15:57:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JLDC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jldc/32/17627_2.png) [@JLDC](https://discourse.julialang.org/u/JLDC)\
**Post date:** [November 2, 2021, 3:57pm UTC](https://discourse.julialang.org/t/sequence-to-one-modelling-and-flux-reset/70815/1 "2021-11-02T15:57:46Z")

</div>

I’m trying to build a sequence-to-one model using Flux and I’m running into an error that I have not been able to solve.

Following: [Building simple sequence-to-one RNN with Flux - New to Julia - JuliaLang](https://discourse.julialang.org/t/building-simple-sequence-to-one-rnn-with-flux/56361/8) it seems that it wasn’t an issue at that time but I’m not sure since I didn’t try it out back then.

The problem is that when I try to use `Flux.reset!` I get an error message.

Here’s a working example to replicate the error:

```julia
using Flux
x = [rand(Float32, 2, 32) for _ ∈ 1:10]
y = rand(Float32, 1, 32)

mutable struct MyModel
    rnn
    fc
end
Flux.@functor MyModel

function (m::MyModel)(x)
    Flux.reset!(m.rnn) # THIS IS THE PROBLEMATIC LINE, IF REMOVED THE CODE RUNS FINE
    [m.rnn(x[i]) for i ∈ 1:length(x)-1]
    m.fc(m.rnn(x[end]))
end
m = MyModel(RNN(2, 5), Dense(5, 1))

loss(x, y) = Flux.mse(m(x), y)
opt = Descent(1e-2)
ps = Flux.params(m)
# This works
loss(x, y)
# This doesn't work
Flux.train!(loss, ps, [(x, y)], opt)

```

Strangely enough, `loss(x, y)` works just fine but when used with `Flux.train!` it throws an error.

`ERROR: LoadError: DimensionMismatch("new dimensions (5, 1) must be consistent with array size 160")`

Am I doing something wrong in the way I’m building my model or is this a bug?

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [November 2, 2021, 5:37pm UTC](https://discourse.julialang.org/t/sequence-to-one-modelling-and-flux-reset/70815/2 "2021-11-02T17:37:23Z")

</div>

Without diagnosing the specific issue, I would strongly recommend calling `reset!` outside of your loss function. That unfortunately means ditching `train!`, but IME `train!` doesn’t really work for RNNs in the first place because of the assumptions it makes around input batching.

---

<div class="post-metadata">

**Author:** ![JLDC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jldc/32/17627_2.png) [@JLDC](https://discourse.julialang.org/u/JLDC)\
**Post date:** [November 2, 2021, 10:42pm UTC](https://discourse.julialang.org/t/sequence-to-one-modelling-and-flux-reset/70815/3 "2021-11-02T22:42:39Z")

</div>

Thank you so much, that does indeed solve the issue!
