# How can I differentiate a subset of the outputs of a neural network in Flux or Lux?

**URL:** <https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354>\
**Category:** General Usage\
**Tags:** question, flux, ml, lux\
**Created:** [August 30, 2023, 6:32am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354 "2023-08-30T06:32:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [August 30, 2023, 6:32am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/1 "2023-08-30T06:32:38Z")

</div>

Is there a way in Flux or Lux to differentiate a subset of the outputs of a Neural Network model? The reason why I am asking is that I have a target matrix with 6 outputs and 3000 observations (very little data) but the bad thing is that I have quite a few `missing` here and there in the target matrix. Thus at any given batch I would like to backpropagate the errors from all the known targets while ignoring the missing. Is there a way to do that? Happy to provide some dummy code for it if it helps.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [August 30, 2023, 8:40am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/2 "2023-08-30T08:40:36Z")

</div>

I guess you’re gonna have to do manual autodiff using Zygote, defining the loss function yourself as the sum of errors for all non-missing outputs

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [August 30, 2023, 9:14am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/3 "2023-08-30T09:14:59Z")

</div>

I’ll give that a go. Thanks. 🙂

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [August 31, 2023, 12:03am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/4 "2023-08-31T00:03:37Z")

</div>

Could you share an example if you succeed?  
At first I was thinking that it’s trivial and then I couldn’t find the solution at a glance

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [August 31, 2023, 12:24am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/5 "2023-08-31T00:24:52Z")

</div>

You may be interested in some of the previous discussions on here and GitHub about masking in losses. Those threads should have code examples

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [August 31, 2023, 6:33am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/6 "2023-08-31T06:33:09Z")

</div>

So this seems to work but my loss function is a horrible hack and will for sure run extremely slow on a GPU.

```julia
function loss2(y, ŷ)
    l(x, x̂) = sum((x - x̂) .^ 2)
    totloss = 0
    for j in 1:(size(y)[1])
        for i in 1:(size(y)[2])
            if !ismissing(y[j, i])
                totloss = totloss + l(y[j, i], ŷ[j, i])
            end
        end
    end
    totloss / prod(size(y))
end
model = Chain(Dense(size(X)[1] => 10), Dense(10 => size(Y)[1]))
opt_state = Flux.setup(AdamW(0.005), model)
@info loss2(Y, model(X))
for e in 1:10
    # Calculate the gradient of the objective
    # with respect to the parameters within the model:
    grads = Flux.gradient(model) do m
        result = m(X)
        loss2(Y, result)
    end
    # Update the parameters so as to reduce the objective,
    # according the chosen optimisation rule:
    Flux.update!(opt_state, model, grads[1])
    @info loss2(Y, model(X))
end

```

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [August 31, 2023, 6:48am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/7 "2023-08-31T06:48:30Z")

</div>

Did you try something like  
`Flux.Losses.mse(model(x), y; agg=skipmissing|>mean)`  
?

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [August 31, 2023, 6:49am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/8 "2023-08-31T06:49:35Z")

</div>

No but I tried this:

```julia
loss(y, ŷ) = sum(skipmissing(y - ŷ) .^ 2)

```

which did not work. I will try your suggestion.

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [August 31, 2023, 6:50am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/9 "2023-08-31T06:50:25Z")

</div>

I think they are basically the same.  
Sorry for inconvenience, I just cannot try this right now ☹  
But yeah, give it a try if you don’t mind :\>

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [August 31, 2023, 7:00am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/10 "2023-08-31T07:00:04Z")

</div>

You could also try something with `coalesce`, like

```julia
loss(ŷ, y) = mse(coalesce.(y, ŷ), ŷ)

```

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [August 31, 2023, 7:20am UTC](https://discourse.julialang.org/t/how-can-i-differentiate-a-subset-of-the-outputs-of-a-neural-network-in-flux-or-lux/103354/11 "2023-08-31T07:20:48Z")

</div>

So this was precisely what I hoped to achieve with skipmissing. Did not know about `coalesce`. Very neat. This solves my problem. Thanks guys. 🙂
