# Problems on Flux.destructure()

**URL:** <https://discourse.julialang.org/t/problems-on-flux-destructure/76398>\
**Category:** Machine Learning\
**Created:** [February 14, 2022, 8:13am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398 "2022-02-14T08:13:48Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Louis612](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Louis612](https://discourse.julialang.org/u/Louis612)\
**Post date:** [February 14, 2022, 8:13am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/1 "2022-02-14T08:13:48Z")

</div>

![截屏2022-02-14 下午4.05.22](https://global.discourse-cdn.com/julialang/original/3X/a/0/a0c148a0ac4aae9984eff47fd7a66a195ff8ed23.jpeg)  
As the figure shown above, I build a LSTM using the code "Chain(LSTM(1,1),LSTM(1,1)) "  
Then I want to build the network with specific parameters p.  
code re(p) but it failed and showed above.  
How to deal with this issue?

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [February 14, 2022, 8:37am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/2 "2022-02-14T08:37:43Z")

</div>

My guess would be that `p4` has element type `Any`?

So to deal with it try to make sure that `p4` has the element type you want (probably `Float32`).

---

<div class="post-metadata">

**Author:** ![Louis612](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Louis612](https://discourse.julialang.org/u/Louis612)\
**Post date:** [February 14, 2022, 9:00am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/3 "2022-02-14T09:00:55Z")

</div>

How to convert from Any to Float32?  
I try to use convert but it showed below:

 ![截屏2022-02-14 下午5.00.41](https://global.discourse-cdn.com/julialang/original/3X/9/3/93e6f4b1388fdebc5c21b10b3d9c2898139a637a.png)

---

<div class="post-metadata">

**Author:** ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Post date:** [February 14, 2022, 9:02am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/4 "2022-02-14T09:02:45Z")

</div>

Hi @Louis612 ,

could you maybe provide a minimum example, so we can reproduce the error?  
That is, in addition to the model (`Chain(LSTM(1,1),LSTM(1,1))`), how you use `destructure` and what kind of object you feed into `re()`.

Please use triple backticks ``` to create copy-pasteable code blocks. Don’t just post screenshots. This makes it easier to help you.

---

<div class="post-metadata">

**Author:** ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Post date:** [February 14, 2022, 9:09am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/5 "2022-02-14T09:09:02Z")

</div>

```julia
convert(Vector{Float32}, b)

```

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [February 14, 2022, 9:16am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/6 "2022-02-14T09:16:35Z")

</div>

The best would probably be to not convert, but to use the proper array from the start. Using an array with `Any` type can be very inefficient.

To make sure you have the correct type from the start you can do something like

```julia
p4 = Float32[...]

```

when initially creating the vector, but if you prefer converting you can either use `convert` as suggested above or what I find easier is to use broadcasting `Float32.(p4)`.

---

<div class="post-metadata">

**Author:** ![Louis612](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Louis612](https://discourse.julialang.org/u/Louis612)\
**Post date:** [February 14, 2022, 9:21am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/8 "2022-02-14T09:21:35Z")

</div>

At first ,the vector’s type is Float64. But when I run the code, it becomes Any, which is very puzzling

---

<div class="post-metadata">

**Author:** ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Post date:** [February 14, 2022, 9:30am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/9 "2022-02-14T09:30:15Z")

</div>

> [@trahflow](#):
>
> Please use triple backticks ``` to create copy-pasteable code blocks.

(you can edit your post)  
Please read through the suggestions here: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

It is hard to search through what is relevant if you just dump all your code like that.  
Please

- properly format your code
- try to provide only as much as is relevant to the problem/error you observe.

Boiling down the problem to what is relevant will make it easiser to help you (and in the process you might even figure it out yourself)

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [February 14, 2022, 9:35am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/12 "2022-02-14T09:35:20Z")

</div>

It might be something with how differentialequations treat parameters? Try doing `ODEProblem(QSIR, u0, tspan, (p3,))` and then do `p3 = p[1]` in QSIR function to see if that keeps the type?

---

<div class="post-metadata">

**Author:** ![Louis612](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Louis612](https://discourse.julialang.org/u/Louis612)\
**Post date:** [February 14, 2022, 9:35am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/13 "2022-02-14T09:35:32Z")

</div>

```nohighlight
using Plots
using Measures
using Flux
using DifferentialEquations
using DiffEqFlux
using LaTeXStrings
#导入数据
vars = matread("/Users/louislee/Desktop/北卡科研/COVID-19-ML-Quarantine-Control-Model-master/Hubei_Track.mat")
Infected = vars["Hubei_Infected_All"]
Recovered = vars["Hubei_Recovered_All"]
Dead = vars["Hubei_Dead_All"]
Time = vars["Hubei_Time"]
#构建网络
n=2
rnn = Chain(LSTM(4,n),LSTM(n,n),LSTM(n,1))
p1,re = Flux.destructure(rnn)
p2 = Float64[0.02, 0.01]
p3 = [p1; p2]
ps = Flux.params(p3)
m=size(p1)[1]
p3
#解方程
function QSIR(du, u, p, t)
    β = abs(p[m+1])
    γ = abs(p[m+2])
    du[1]= - β*u[1]*(u[2])/u0[1]
    du[2] = β*u[1]*(u[2])/u0[1] - γ*u[2] - abs(re(p[1:m])(u)[1])*u[2]/u0[1]
    du[3] = γ*u[2]
    du[4] = + abs(re(p[1:m])(u)[1])*u[2]/u0[1]
end

u0 = Float64[11000000.0, 500 ,10, 10]
tspan = (0, 39.0)
datasize = 39;

prob = ODEProblem(QSIR, u0, tspan, p3)
t = range(tspan[1],tspan[2],length=datasize)

sol = Array(concrete_solve(prob, Rosenbrock23(autodiff = false),u0, p3, saveat=t))
function predict_adjoint() # Our 1-layer neural network
  Array(concrete_solve(prob,Rosenbrock23(autodiff = false),u0,p3,saveat=t))
end

I = Infected[1, :];
R = Recovered[1,:];
D = Dead[1, :];
#误差函数
function loss_adjoint()
 prediction = predict_adjoint()
 loss = sum(abs2, log.(abs.(I[1:39])) .- log.(abs.(prediction[2, :]))) + sum(abs2, log.(abs.(R[1:39] + D[1:39]) .+ 1) .- log.(abs.(prediction[3, :] .+ 1)))
end

Loss = []
P1 = []
P2 = []
P3 = []

using DiffEqFlux
datan = Iterators.repeated((), 1000)
opt = ADAM(1)
cb = function()
  global Loss = append!(Loss, loss_adjoint())
  global P1 = append!(P1, p3[m+1])
  global P2 = append!(P2, p3[m+2])
  global P3 = append!(P3, p3)
end
function predict_adjoint() # Our 1-layer neural network
  Array(concrete_solve(prob,Rosenbrock23(autodiff = false),u0,p3,saveat=t))
end

I = Infected[1, :];
R = Recovered[1,:];
D = Dead[1, :];
#误差函数
function loss_adjoint()
 prediction = predict_adjoint()
 loss = sum(abs2, log.(abs.(I[1:39])) .- log.(abs.(prediction[2, :]))) + sum(abs2, log.(abs.(R[1:39] + D[1:39]) .+ 1) .- log.(abs.(prediction[3, :] .+ 1)))
end

Loss = []
P1 = []
P2 = []
P3 = []

using DiffEqFlux
datan = Iterators.repeated((), 1000)
opt = ADAM(1)
cb = function()
  global Loss = append!(Loss, loss_adjoint())
  global P1 = append!(P1, p3[m+1])
  global P2 = append!(P2, p3[m+2])
  global P3 = append!(P3, p3)
end
Flux.train!(loss_adjoint, ps, datan, opt, cb = cb)
#找误差最小值
L = findmin(Loss)
idx = L[2]
idx1 = (idx-1)*(m+2) +1
idx2 = idx*(m+2)
global p3 = P3[idx1: idx2]
prediction= Array(concrete_solve(prob,Rosenbrock23(autodiff = false),u0,p3,saveat=t))
S_NN_all_loss = prediction[1, :]
I_NN_all_loss = prediction[2, :]
R_NN_all_loss = prediction[3, :]
T_NN_all_loss = prediction[4, :]
#Q(t)
Q_parameter = zeros(Float64, length(S_NN_all_loss), 1)

for i = 1:length(S_NN_all_loss)
  Q_parameter[i] = abs(re(p3[1:m])([S_NN_all_loss[i],I_NN_all_loss[i], R_NN_all_loss[i], T_NN_all_loss[i]])[1])
end

```

---

<div class="post-metadata">

**Author:** ![Louis612](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Louis612](https://discourse.julialang.org/u/Louis612)\
**Post date:** [February 14, 2022, 9:38am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/14 "2022-02-14T09:38:57Z")

</div>

But the solve of ODEs remains Float64 which is the same as p3 at first

---

<div class="post-metadata">

**Author:** ![Louis612](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Louis612](https://discourse.julialang.org/u/Louis612)\
**Post date:** [February 14, 2022, 9:42am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/15 "2022-02-14T09:42:03Z")

</div>

sry, I’m new here.lol

---

<div class="post-metadata">

**Author:** ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Post date:** [February 14, 2022, 9:42am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/16 "2022-02-14T09:42:38Z")

</div>

> [@Louis612](#):
>
> `P3 = []`

This creates an array of type `Any`:

```julia
julia> typeof(P3)
Vector{Any} (alias for Array{Any, 1})

```

Do this instead:

```julia
P3 = Float32[]

```

Hope that solves your problem.

But again

> [@trahflow](#):
>
> Please read through the suggestions here: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

please read these suggestions carefully, they’re worth it. No need to rush it through. Take your time to formulate a question/response 🙂

---

<div class="post-metadata">

**Author:** ![Louis612](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Louis612](https://discourse.julialang.org/u/Louis612)\
**Post date:** [February 14, 2022, 9:43am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/17 "2022-02-14T09:43:34Z")

</div>

The code can run completely if I use ANN( Chain(Dense(,),Dense(,))

---

<div class="post-metadata">

**Author:** ![Louis612](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Louis612](https://discourse.julialang.org/u/Louis612)\
**Post date:** [February 14, 2022, 9:44am UTC](https://discourse.julialang.org/t/problems-on-flux-destructure/76398/18 "2022-02-14T09:44:36Z")

</div>

OK, thank you very much for your help. I’ll try it.
