# Cannot take gradient of a function involves \`eachrow\` with Zygote?

**URL:** <https://discourse.julialang.org/t/cannot-take-gradient-of-a-function-involves-eachrow-with-zygote/58098>\
**Category:** General Usage\
**Tags:** zygote\
**Created:** [March 27, 2021, 11:13pm UTC](https://discourse.julialang.org/t/cannot-take-gradient-of-a-function-involves-eachrow-with-zygote/58098 "2021-03-27T23:13:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qft](https://avatars.discourse-cdn.com/v4/letter/q/b19c9b/32.png) [@qft](https://discourse.julialang.org/u/qft)\
**Post date:** [March 27, 2021, 11:13pm UTC](https://discourse.julialang.org/t/cannot-take-gradient-of-a-function-involves-eachrow-with-zygote/58098/1 "2021-03-27T23:13:56Z")

</div>

The following code

```julia
using Zygote
gradient(x -> sum(map(sum, eachcol(x))), rand(3, 4))

```

gives the error message

> ERROR: Need an adjoint for constructor Base.Generator{Base.OneTo{Int64}, Base.var"#193#194"{Matrix{Float64}}}. Gradient is of type Vector{FillArrays.Fill{Float64, 1, Tuple{Base.OneTo{Int64}}}}

However if I add a `collect` function to the results of `eachcol`, I can get the correct gradient.

```julia
gradient(x -> sum(map(sum, collect(eachcol(x)))), rand(3, 4))

```

Is this a limitation of Zygote, or did I use it wrong? Thanks!

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [March 28, 2021, 1:08am UTC](https://discourse.julialang.org/t/cannot-take-gradient-of-a-function-involves-eachrow-with-zygote/58098/2 "2021-03-28T01:08:17Z")

</div>

You’re code is fine.  
That error message means that  
Zygote is missing the rule for that constructor.

---

<div class="post-metadata">

**Author:** ![qft](https://avatars.discourse-cdn.com/v4/letter/q/b19c9b/32.png) [@qft](https://discourse.julialang.org/u/qft)\
**Post date:** [March 28, 2021, 1:30am UTC](https://discourse.julialang.org/t/cannot-take-gradient-of-a-function-involves-eachrow-with-zygote/58098/3 "2021-03-28T01:30:40Z")

</div>

Thanks! I see someone has made a PR related to this issue ([https://github.com/FluxML/Zygote.jl/pull/896](https://github.com/FluxML/Zygote.jl/pull/896)), if add the adjoints in this PR, my original code works…

---

<div class="post-metadata">

**Author:** ![rlm](https://avatars.discourse-cdn.com/v4/letter/r/b19c9b/32.png) [@rlm](https://discourse.julialang.org/u/rlm)\
**Post date:** [March 30, 2021, 3:48pm UTC](https://discourse.julialang.org/t/cannot-take-gradient-of-a-function-involves-eachrow-with-zygote/58098/4 "2021-03-30T15:48:04Z")

</div>

> [@qft](#):
>
> `collect(eachcol(x))))`

Thanks for this tip, it helped me work around this issue.
