# Interpolations, between Lux Dense layers, supported?

**URL:** <https://discourse.julialang.org/t/interpolations-between-lux-dense-layers-supported/104938>\
**Category:** General Usage\
**Tags:** flux, sciml, autodiff, lux\
**Created:** [October 13, 2023, 7:31pm UTC](https://discourse.julialang.org/t/interpolations-between-lux-dense-layers-supported/104938 "2023-10-13T19:31:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rohan\_Rajagopal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohan_rajagopal/32/34183_2.png) [@Rohan\_Rajagopal](https://discourse.julialang.org/u/Rohan_Rajagopal)\
**Post date:** [October 13, 2023, 7:31pm UTC](https://discourse.julialang.org/t/interpolations-between-lux-dense-layers-supported/104938/1 "2023-10-13T19:31:20Z")

</div>

Greetings, one of the custom Lux models I am trying to develop requires interpolation in between, something like the following(?) which errs

```julia
using DataInterpolations, Lux, Random, Zygote, Enzyme

function convert_interpolation_then_vector(M :: AbstractMatrix)
    vectorized_data = collect.(eachslice(M,dims=1))
    vec = LagrangeInterpolation(vectorized_data[1], time)
    return vec'
end

temp_model = Chain(Dense(1, 1, use_bias = false), convert_interpolation_then_vector, Dense(1,1))

ERROR: Need an adjoint for constructor QuadraticInterpolation{Vector{Float64}, Vector{Float64}, true, Float64}. Gradient is of type Vector{Float64}

```

And gradient calculations for some of the interpolations work on Enzyme but not on Zygote.

```julia
time = collect(0:0.01:1)
x = LinearInterpolation(sin.(time), time)
Enzyme.gradient(Forward, time -> sum(x(time)), time) #works

Zygote.gradient(time -> sum(x(time)), time) # Mutating Array error

```

But I am not sure if Enzyme will work on Lux models currently for gradient calculations? Can anyone suggest what I am missing? Thank you very much for your help.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [October 13, 2023, 8:46pm UTC](https://discourse.julialang.org/t/interpolations-between-lux-dense-layers-supported/104938/2 "2023-10-13T20:46:26Z")

</div>

You can always make a custom layer for Lux, then write an rrule that uses Enzyme which will hide the Enzyme use from Zygote.

If it’s not clear what I mean by that - look at ChainRules doc, but then instead of manually implementing the rrule you can call Enzyme.

---

<div class="post-metadata">

**Author:** ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)\
**Post date:** [October 14, 2023, 12:37am UTC](https://discourse.julialang.org/t/interpolations-between-lux-dense-layers-supported/104938/3 "2023-10-14T00:37:31Z")

</div>

@cortner’s solution is the best in the short term.

However, for most CPU Lux models, Enzyme should be fine if your code is type-stable and such. If it doesn’t work, open an issue.
