# Some questions for LSTM

**URL:** https://discourse.julialang.org/t/some-questions-for-lstm/84907
**Category:** General Usage
**Tags:** question
**Created:** [July 28, 2022, 7:29am UTC](https://discourse.julialang.org/t/some-questions-for-lstm/84907 "2022-07-28T07:29:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)
#### Post date: [July 28, 2022, 7:29am UTC](https://discourse.julialang.org/t/some-questions-for-lstm/84907/1 "2022-07-28T07:29:29Z")

</div>

I am trying to do batch training using LSTM for a time series data with multiple features.  
Assuming I have 5000 samples and 5 features for each sample. The input uses 14 days into the past and the output is a single value on the 15th day. (My time step is 14). The size of my data is the following:

```julia
using Flux
using Flux: Flux.Data.DataLoader
X = rand(14,5,5000);
Y = rand(1,1,5000);

model = Chain(
LSTM(5, 20),
Dropout(0.5),
Dense(20, 1,σ)
)

#evaluating prediction for the input sequence
function eval_model(x)
Flux.reset!(model)
inputs = [x[1,t,:] for t in 1:14]
output = model.(inputs)
end

L(x, y) = Flux.mse(eval_model(X), Y)
opt = ADAM(0.001)

#creating batches and training 
train_loader = DataLoader((X, Y), batchsize=30, shuffle=true)
@time Flux.train!(L, params(model), train_loader, opt)

```

But some errors show as follow:  
BoundsError: attempt to access 14×5×5000 Array{Float64,3} at index [1, 6, 1:5000]  
Errors happen in `train_loader = DataLoader((X, Y), batchsize=30, shuffle=true)`  
How can I solve it?  
Another question: How to show the loss during the training process?

---

<div class="post-metadata">

### Author: ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)
#### Post date: [July 28, 2022, 10:03am UTC](https://discourse.julialang.org/t/some-questions-for-lstm/84907/2 "2022-07-28T10:03:24Z")

</div>

> [@zlq178](#):
>
> `inputs = [x[1,t,:] for t in 1:14]`

Answering your first question,  
Problem seems to be here.  
t → varies from 1:14, but at 2nd index of x, you only have 5 elements (x = rand(14,`5`,5000)).

---

<div class="post-metadata">

### Author: ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)
#### Post date: [July 28, 2022, 10:17am UTC](https://discourse.julialang.org/t/some-questions-for-lstm/84907/3 "2022-07-28T10:17:04Z")

</div>

So it should be `inputs = [x[t,:,:] for t in 1:14]` or `inputs = [x[:,:,t] for t in 1:14]`,according to the problem aboved?

---

<div class="post-metadata">

### Author: ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)
#### Post date: [July 28, 2022, 10:21am UTC](https://discourse.julialang.org/t/some-questions-for-lstm/84907/4 "2022-07-28T10:21:15Z")

</div>

I try to turn `inputs = [x[1,t,:] for t in 1:14]` as `inputs = [x[t,:,:] for t in 1:14]`, it also show me  
MethodError: no method matching (::Flux.LSTMCell{Array{Float32,2},Array{Float32,1},Tuple{Array{Float32,2},Array{Float32,2}}})(::Tuple{Array{Float32,2},Array{Float32,2}}, ::Array{Float64,2})  
Closest candidates are:  
Any(::Any, ::Union{AbstractArray{T,2}, AbstractArray{T,1}, Flux.OneHotArray}) where {A, V, T} at C:\Users\zlq.julia\packages\Flux\qp1gc\src\layers\recurrent.jl:137  
The same error happened when change as`inputs = [x[:,:,t] for t in 1:14]`

---

<div class="post-metadata">

### Author: ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)
#### Post date: [July 28, 2022, 11:33am UTC](https://discourse.julialang.org/t/some-questions-for-lstm/84907/5 "2022-07-28T11:33:22Z")

</div>

Sorry, I haven’t used LSTM / RNN from Flux yet, so i don’t have an answer,  
But there is kind of similar post where you might get some lead →

> [@Broadcasting over a Flux.DataLoader](https://discourse.julialang.org/t/broadcasting-over-a-flux-dataloader/70414):
>
> Apologies for this question since I’ve had a lot of trouble producing a MWE (which I don’t have). I’m working on an RNN using text data (each observation is a sentence - each word in each sentence is mapped to a word embedding). The problem is classification into six classes. The issue I am having I think relates to using the Flux.DataLoader type. The basic setup is… train\_data = Flux.DataLoader((data=train, label=tlabels), batchsize=42, shuffle=true) # train\_data.data.data is 1x357, wher…

---

<div class="post-metadata">

### Author: ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)
#### Post date: [July 29, 2022, 11:43am UTC](https://discourse.julialang.org/t/some-questions-for-lstm/84907/6 "2022-07-29T11:43:10Z")

</div>

Sorry, I still do not understand how the method solves my error.

---

<div class="post-metadata">

### Author: ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)
#### Post date: [July 29, 2022, 11:44am UTC](https://discourse.julialang.org/t/some-questions-for-lstm/84907/7 "2022-07-29T11:44:49Z")

</div>

Can anyone solve the error I proposed at first? Thank you very much
