Skip to content

Support for gradients w.r.t. data #60

@lxvm

Description

@lxvm

Hi,

Thank you for developing this very capable package. It would be a very useful feature in applications such as topology optimization to be able to take derivatives w.r.t. the data for an interpolant. I made this example trying to use Zygote.jl to take such a derivative:

using FastInterpolations
import Zygote
using Random

Random.seed!(0)

Nx = Ny = 5
data = rand(Nx, Ny)
x = range(0, 1, length=Nx)
y = range(0, 1, length=Ny)

x0 = (0.2, 0.3)

function f0(data, x0)
    itp = cubic_interp((x, y), data)
    return itp(x0)
end

@show f0(data, x0) # 0.5931453845794765
Zygote.withgradient(f0, data, x0)

However, the autodiff fails with the following error:

ERROR: LoadError: Compiling Tuple{typeof(FastInterpolations._get_bank), FastInterpolations.GlobalRegistry, Type{FastInterpolations.CacheBank{FastInterpolations.CacheEntry{Float64, Deriv1{Float64}, Deriv1{Float64}, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, FastInterpolations.ScalarSpacing{Float64}}}}}: ArgumentError: array must be non-empty

In addition, some topology optimization algorithms make use of gradients and hessians in which case it would be helpful to get gradients of the data with respect to those, like in this code:

function f1(data, x0)
    itp = cubic_interp((x, y), data)
    return sum(abs2, gradient(itp, x0))
end

f1(data, x0)
Zygote.withgradient(f1, data, x0)

function f2(data, x0)
    itp = cubic_interp((x, y), data)
    return sum(abs2, hessian(itp, x0))
end

f2(data, x0)
Zygote.withgradient(f2, data, x0)

Would supporting these kinds of gradients be possible? I would be interested in helping to write rrules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions