# Shape of input arrays for an LSTM in Flux.jl? julia 1.0

**URL:** https://discourse.julialang.org/t/shape-of-input-arrays-for-an-lstm-in-flux-jl-julia-1-0/27418
**Category:** Machine Learning
**Tags:** flux
**Created:** [August 11, 2019, 2:32pm UTC](https://discourse.julialang.org/t/shape-of-input-arrays-for-an-lstm-in-flux-jl-julia-1-0/27418 "2019-08-11T14:32:43Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Simon\_Besnard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simon_besnard/32/9730_2.png) [@Simon\_Besnard](https://discourse.julialang.org/u/Simon_Besnard)
#### Post date: [August 11, 2019, 2:32pm UTC](https://discourse.julialang.org/t/shape-of-input-arrays-for-an-lstm-in-flux-jl-julia-1-0/27418/1 "2019-08-11T14:32:43Z")

</div>

I am currently trying to implement an LSTM model for a regression exercise using the flux.jl library. Although it is rather straightforward to build-up the model, I have rather some issues in understanding the right shape of the input arrays to train the model as an error related to the dimensions of the arrays is thrown, so I am not quite sure whether I feed the right input shape to the model, etc… Does somebody have any clues why this is happening?

Here’s the code/error to reproduce the issue for some random data (20 samples, 6 input variables, 1 target variable, sequence length is 100):

> using Flux
> 
> #Create training and validation sets  
> x\_train, y\_train= [rand(6, 100) for i in 1:20], [rand(1, 100) for i in 1:20]  
> x\_valid, y\_valid = [rand(6, 100) for i in 1:20], [rand(1, 100) for i in 1:20]
> 
> #Define loss function  
> function mseLoss(x, y)  
> loss = Flux.mse(model(x), y)  
> Flux.reset!(model)  
> return loss  
> end
> 
> #Create initial model  
> model= Chain(  
> LSTM(6, 20),  
> LSTM(20, 20),  
> LSTM(20, 20),  
> Dense(20, 1))
> 
> #Train model  
> evalcb = () → @show mseLoss(x\_valid, y\_valid)  
> Flux.train!(mseLoss, params(model), zip(x\_train, y\_train), Flux.ADAM(0.01), cb = Flux.throttle(evalcb, 30))  
> ERROR: DimensionMismatch(“matrix A has dimensions (80,6), vector B has length 20”)

However, if I remove the call back function from the training routine, there is no error being thrown.

> Flux.train!(mseLoss, params(model), zip(x\_train, y\_train), Flux.ADAM(0.01))

---

<div class="post-metadata">

### Author: ![mpratland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpratland/32/10796_2.png) [@mpratland](https://discourse.julialang.org/u/mpratland)
#### Post date: [October 13, 2019, 5:39pm UTC](https://discourse.julialang.org/t/shape-of-input-arrays-for-an-lstm-in-flux-jl-julia-1-0/27418/2 "2019-10-13T17:39:34Z")

</div>

The issue is that your `evalcb()` function is trying to apply your loss function (`mseLoss()`) on the entire validation set, instead of a single sample.

For example, if you manually try to call `mseLoss()` on your validation set, it won’t work:

```julia
julia> mseLoss(x_valid, y_valid)
ERROR: DimensionMismatch("matrix A has dimensions (80,6), vector B has length 20")
Stacktrace:

```

But if you use the dot operator it’s all fine:

```julia
julia> mseLoss.(x_valid, y_valid)
20-element Array{Tracker.TrackedReal{Float64},1}:
 0.09329284350031428
 0.08800717849477932
 0.09536389093326372
 0.07287500246038604
 0.07941595462823466
 0.08271627911981938
 0.06494661422789759
 0.090946393431492  
 0.08429430207008291
 0.07520673733085231
 0.08991733401842267
 0.08013483351191553
 0.08518935831116269
 0.08661341277531345
 0.09158514810736933
 0.07756680475821241
 0.10052480072450713
 0.07839699178557202
 0.08355401235047255
 0.08109280394475628

```

So the easy way to get your callback to work is by using the dot operator:

```julia
evalcb = () -> @show mseLoss.(x_valid, y_valid)
Flux.train!(mseLoss, params(model), zip(x_train, y_train), Flux.ADAM(0.01), cb = Flux.throttle(evalcb, 30))

```

Should work like a charm.

Good luck!
