Skip to content

DiffCache/GeneralLazyBufferCache for an array with mixed types #71

@lazarusA

Description

@lazarusA

Is there a way to make this work? The important part is the preallocation of the mixed tuples in the array.

  • DiffCache doesn't work, and
  • GeneralLazyBufferCache [don't know if it is the right approach instead]
using ForwardDiff
using PreallocationTools
using StaticArrays

ar_alloc = [(; a=1.0, b=2.0, c=SVector(0.0)), (; a=1.0, b=2.0, c=SVector(0.0))]

function fxy(x, ar_alloc, y)
    for i in 1:2
        a = x + 1
        b = x * x + 2
        c = SVector(x * x * x + 3 + a)
        ar_alloc[i] = (; a, b, c)
    end
    ŷ = [ar_alloc[1].a + ar_alloc[1].c[1], ar_alloc[2].a + +ar_alloc[2].c[1]]
    return sum(abs.(ŷ .- y))
end

y = [2.3, 5.0]
loss(x) = fxy(x, ar_alloc, y)
loss(0.5)

ForwardDiff.gradient(loss, [0.2, 0.5, 0.6])

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