List comprehension in Zygote

Zygote works fine with array comprehensions. I use it frequently. Here’s a simple example:

using Zygote

x = rand(5)
y = rand(5)

f(y) = sum([x[i] * y[i] for i in eachindex(y)])

Then for my random x,y, I have:

julia> Zygote.gradient(f, y)
([0.20735224892728188, 0.023609460904363777, 0.8209142299297378, 0.5328550686359217, 0.11713217978556711],)

You would get better help here in the forum by posting a self contained minimal reproducible example that produces your error. See Please read: make it easier to help you for more details on getting help.

2 Likes