# Gradient of a loss function : struggling to avoid arrays mutation

**URL:** https://discourse.julialang.org/t/gradient-of-a-loss-function-struggling-to-avoid-arrays-mutation/45593
**Category:** New to Julia
**Tags:** zygote, sciml
**Created:** [August 26, 2020, 8:32pm UTC](https://discourse.julialang.org/t/gradient-of-a-loss-function-struggling-to-avoid-arrays-mutation/45593 "2020-08-26T20:32:06Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [August 30, 2020, 7:07am UTC](https://discourse.julialang.org/t/gradient-of-a-loss-function-struggling-to-avoid-arrays-mutation/45593/3 "2020-08-30T07:07:11Z")

</div>

> [@mothmani](#):
>
> ```julia
> function loss_2(p_p)
> diff_pred_obs = map( p_u0 -> ode_p_test_2(p_p, p_u0), eachrow(tab_u0) ) - tab_uT_2
> return sum( vcat( diff_pred_obs... ) .^2 )
> end
> 
> ```

The more natural way to do this would be to use `reduce`:

```julia
function loss_2(p_p)
    diff_pred_obs = map( p_u0 -> ode_p_test_2(p_p, p_u0), eachrow(tab_u0) ) - tab_uT_2
    return sum(reduce(vcat,diff_pred_obs) .^2)
end

```

That will avoid the problems associated with splatting, which is something you rarely want to do on a big array. Additionally, `sum(abs2,reduce(vcat,diff_pred_obs))` is a slightly nicer style. Additionally, instead of using a `map` you can use [Parallel ensembles](https://diffeq.sciml.ai/stable/features/ensemble/) to multithread the solves. This is demonstrated in the SDE parameter estimation tutorial:

[https://diffeqflux.sciml.ai/dev/examples/optimization\_sde/](https://diffeqflux.sciml.ai/dev/examples/optimization_sde/)

So in total, all you need to do to make your code work is `reduce` instead of splat (@dhairyagandhi96 it might be good to try and figure out what’s up with that adjoint anyways), but using the ensembles will have some advantages and will make `sum(abs2,sol-data)` directly work. Cheers!

---

_[View the full topic](https://discourse.julialang.org/t/gradient-of-a-loss-function-struggling-to-avoid-arrays-mutation/45593)._
