# ReverseDiff.jl gives zeros when using fill

**URL:** https://discourse.julialang.org/t/reversediff-jl-gives-zeros-when-using-fill/54369
**Category:** Specific Domains
**Tags:** reversediff, ad, automatic
**Created:** [February 1, 2021, 11:49am UTC](https://discourse.julialang.org/t/reversediff-jl-gives-zeros-when-using-fill/54369 "2021-02-01T11:49:50Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)
#### Post date: [February 1, 2021, 11:49am UTC](https://discourse.julialang.org/t/reversediff-jl-gives-zeros-when-using-fill/54369/1 "2021-02-01T11:49:50Z")

</div>

Hey,

the following MWE:

```julia
function g(x)
    out = fill(0.0, size(x))
    for i = 1:size(x)[1]
        out[i, :] .= x[i, :]
    end
    return out
end
ReverseDiff.gradient(x -> sum(g(x)), randn((5, 5)))

```

which returns

```julia
5×5 Array{Float64,2}:
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0

```

Using `out = zeros(eltype(x), size(x))` instead of the `fill`, works as expected:

```julia
function g(x)
    out = zeros(eltype(x), size(x))
    for i = 1:size(x)[1]
        out[i, :] .= x[i, :]
    end
    return out
end
ReverseDiff.gradient(x -> sum(g(x)), randn((5, 5)))

5×5 Array{Float64,2}:
 1.0 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0 1.0

```

Is that something I’m not aware of in ReverseDiff or a bug?

Thanks,

Felix

---

<div class="post-metadata">

### Author: ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)
#### Post date: [February 1, 2021, 11:53am UTC](https://discourse.julialang.org/t/reversediff-jl-gives-zeros-when-using-fill/54369/2 "2021-02-01T11:53:15Z")

</div>

Ah, it’s getting more clear I guess:

```julia
function g(x)
    out = fill(eltype(x)(0), size(x))
    for i = 1:size(x)[1]
        out[i, :] .= x[i, :]
    end
    return out
end
ReverseDiff.gradient(x -> sum(g(x)), randn((5, 5)))

```

This also works, despite that my first `fill` expression and this one have the same type.  
So the reason must be something due to the _connection_ of `x` and `out`, right?
