Skip to content

Make NamedTuple(::ComponentArray) more AD friendly  #122

Closed
@marius311

Description

@marius311

Here's a version of this which doesn't mutate an array, hence works with Zygote:

function _namedtuple(x::ComponentVector)
    NamedTuple{keys(x)}(map(valkeys(x)) do key
        _namedtuple(getproperty(x, key))
    end)
end

And a test which otherwise fails with the latest release version:

@test ComponentArray(x=4,) == Zygote.gradient(ComponentArray(x=2,)) do c
    (;c...,).x^2
end[1]

Wanted to just suggest this here rather than PR since there's some stuff that function is doing (or seemed to before comments) that I wasn't sure about. If this is good, please feel free to commit directly.

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