Skip to content

Gradient fails for Dict constructed with a generator or a vector of pairs #1293

Open
@Kolaru

Description

@Kolaru

When taking the gradient of a function that uses a dict constructor using a generator or a list, it fails because there is a try/catch block somewhere.

e.g. for a generator

julia> using Zygote

julia> function f(x)
              a = Dict(c => x for c in 1:3)
              return a[1]
          end
f (generic function with 1 method)

julia> Zygote.gradient(f, 2.0)
ERROR: Compiling Tuple{Type{Dict}, Base.Generator{UnitRange{Int64}, var"#3#4"{Float64}}}: try/catch is not supported.

It works however if I slurp the vector

julia> function f(x)
              a = Dict([c => x for c in 1:3]...)
              return a[1]
          end
f (generic function with 1 method)

julia> Zygote.gradient(f, 2.0)
(1.0,)

Metadata

Metadata

Assignees

No one assigned

    Labels

    dictionaryenhancementNew feature or requestneeds adjointmissing ruleup for grabsanyone is welcome to contribute with a PR to fix the issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions