# How to differentiate using Zygote in the presence of missing values

**URL:** https://discourse.julialang.org/t/how-to-differentiate-using-zygote-in-the-presence-of-missing-values/112822
**Category:** Numerics
**Tags:** zygote, missing-values
**Created:** [April 11, 2024, 10:05am UTC](https://discourse.julialang.org/t/how-to-differentiate-using-zygote-in-the-presence-of-missing-values/112822 "2024-04-11T10:05:18Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Nikos\_Gianniotis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nikos_gianniotis/32/11487_2.png) [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)
#### Post date: [April 11, 2024, 10:05am UTC](https://discourse.julialang.org/t/how-to-differentiate-using-zygote-in-the-presence-of-missing-values/112822/1 "2024-04-11T10:05:18Z")

</div>

I have encountered a problem in my code. While I cannot easily write a MWE for my particular case, I thought that perhaps dealing with the following issue would help me further.

Let us say, that we have some data `y` and parameters `x`. Using `Zygote`, we can calculate the gradient of the following:

```julia
using Zygote

x = [1.0;3.0;5.0] # parameters
y = [1.0;3.0;5.0].+ 10 # fake data

# calculate gradient wrt x
Zygote.gradient(x -> sum(abs2.(x-y)), x)

```

Now suppose that `y` contains `missing` values:

```julia
using Zygote

x = [1.0;3.0;5.0] # parameters
y = [1.0;3.0;missing].+ 10 # fake data

# calculate gradient wrt x
Zygote.gradient(x -> sum(abs2.(x-y)), x)

```

The above, as expected, will not work.

I tried using instead:

```julia
Zygote.gradient(x -> sum(abs2.(skipmissing(x-y))), x)

```

which gives me an error. I also tried:

```julia
Zygote.gradient(x -> sum(skipmissing(abs2.(x-y))), x)

```

which also doesn’t work, but, interestingly, gives me a different error.

Any advice appreciated.
