# Flux.jl Training loop where data sequentially depends on model output

**URL:** <https://discourse.julialang.org/t/flux-jl-training-loop-where-data-sequentially-depends-on-model-output/22510>\
**Category:** Machine Learning\
**Created:** [March 29, 2019, 4:50pm UTC](https://discourse.julialang.org/t/flux-jl-training-loop-where-data-sequentially-depends-on-model-output/22510 "2019-03-29T16:50:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ares\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ares_fisher/32/7654_2.png) [@Ares\_Fisher](https://discourse.julialang.org/u/Ares_Fisher)\
**Post date:** [March 29, 2019, 4:50pm UTC](https://discourse.julialang.org/t/flux-jl-training-loop-where-data-sequentially-depends-on-model-output/22510/1 "2019-03-29T16:50:21Z")

</div>

Hey all!

I’m interested in writing some “closed loop” generative models that receive their prediction error for the previous input as their next input. Is there a way to do this in Flux.jl’s Train! loop?

Thanks!

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 29, 2019, 7:22pm UTC](https://discourse.julialang.org/t/flux-jl-training-loop-where-data-sequentially-depends-on-model-output/22510/2 "2019-03-29T19:22:54Z")

</div>

I’m no expert on this, although I have started to look at Flux. I’m not 100% sure whether I understand your question, but it may be related to my own main interest in Flux. My interest is to train models for dynamic systems (typically, inputs are denoted u\_t and outputs are denoted y\_t). If that is what you are interested in, I think there are two ways:

- Use Feedforward Neural Network (FNN, `Dense()`), with at least one hidden layer, say `model = Chain(Dense(Nu,nh,tanh),Dense(Nh,Ny))` mapping u\_t, u\_{t-1}, \ldots, u\_{t-n\_u}, y\_{t-1}, y\_{t-2}, \ldots, y\_{t-n\_y} to y\_t, with N\_u = \dim u\_t\cdot (n\_u+1) + \dim y\_t \cdot n\_y and N\_y = \dim y\_t. This would be some NARX structure. Making the number of hidden nodes (`nh`) large should make it possible to describe any system with sufficient accuracy, I would guess. Here, I have indicated using the `tanh` function as activation function; without specifying activation function, this defaults to identity.
- Combine an FNN with a Recurrent Neural Network (`RNN`). My understanding is that an RNN essentially is a state space model of form y\_t = \sigma(W\_\mathrm{f}y\_{t-1} + Wx\_{t-1} + b) where \sigma is the activation function (e.g. `tanh` or any other suitable function) and x is the input (which is denoted u in control engineering). To allow for more flexibility, one could add an FNN at the input to describe more nonlinearities, and maybe (but I’m not sure) a linear or nonlinear layer at the output. A simple “state space” model could probably be `model = Chain(Dense(nu,nh,tahn),RNN(nh,nx,tanh),Dense(nx,ny))`, where `nh` is the number of hidden nodes and `nh` is the number of states.

As I indicate, I haven’t tested this, so I don’t know whether it works. I’ll test it out the next couple of weeks, when I have time.
