Skip to content

Ternary multiplication Vector'*Diagonal*Vector has NamedTuple gradient #1586

@laggvar

Description

@laggvar

MWE:

using Zygote
using LinearAlgebra

D = (Diagonal(ones(3)))

V = ones(3)

function f(x)
	return V'*D*V + ((V - x)'D*(V - x))
end
gradient(f, V)

gives the error message

MethodError: no method matching +(::LinearAlgebra.Diagonal{Float64, Vector{Float64}}, ::@NamedTuple{diag::Vector{Float64}})

The function `+` exists, but no method is defined for this combination of argument types.

The error is thrown when accumulating from the two occurences of D in f(x). No error is thrown if one occurence is removed. The error can also be removed by adding collect(D) or parantheses around the multiplications to turn them into two binary multiplications.

If I insert @showgrad(D), it shows that the gradient w.r.t. D is a NamedTuple and not a Diagonal.

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