# Help with gradient

**URL:** https://discourse.julialang.org/t/help-with-gradient/33943
**Category:** Machine Learning
**Tags:** flux
**Created:** [January 29, 2020, 6:42pm UTC](https://discourse.julialang.org/t/help-with-gradient/33943 "2020-01-29T18:42:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ccc1685](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccc1685/32/12548_2.png) [@ccc1685](https://discourse.julialang.org/u/ccc1685)
#### Post date: [January 29, 2020, 6:42pm UTC](https://discourse.julialang.org/t/help-with-gradient/33943/1 "2020-01-29T18:42:01Z")

</div>

Hi,  
I have a loss function that involves a probability density that requires several matrix operations to derive. When I tried running gradient on it through either Flux or Zygote I get this error: Mutating arrays is not supported. If anyone could tell me what that pertains to then perhaps I can adjust the code to comply.

thanks,  
carson

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 30, 2020, 1:27pm UTC](https://discourse.julialang.org/t/help-with-gradient/33943/2 "2020-01-30T13:27:55Z")

</div>

Do what the error message tells you: don’t mutate arrays, use a functional style (`map`, comprehensions, etc).

It is difficult to help more without a concrete, self-contained example.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

Also, see

[https://fluxml.ai/Zygote.jl/latest/utils/#Zygote.Buffer](https://fluxml.ai/Zygote.jl/latest/utils/#Zygote.Buffer)

---

<div class="post-metadata">

### Author: ![ccc1685](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccc1685/32/12548_2.png) [@ccc1685](https://discourse.julialang.org/u/ccc1685)
#### Post date: [January 30, 2020, 7:46pm UTC](https://discourse.julialang.org/t/help-with-gradient/33943/3 "2020-01-30T19:46:53Z")

</div>

Thanks for your prompt reply. Sorry for not providing enough information. I think you have answered my question but here is my code [https://github.com/ccc1685/transcription-model/blob/master/loss.jl](https://github.com/ccc1685/transcription-model/blob/master/loss.jl). I want to find the gradient of function loss(x).

I need compute the nullspace of a matrix and thus use Julia’s linear algebra package. From what I gather, gradient cannot plow through matrix calculations. So I think this means I need to either write my own SVD solver or wait?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 31, 2020, 10:05am UTC](https://discourse.julialang.org/t/help-with-gradient/33943/4 "2020-01-31T10:05:06Z")

</div>

I would suggest providing a [custom adjoint](https://fluxml.ai/Zygote.jl/latest/adjoints/) to the nullspace operation itself. I usuallly find

```nohighlight
@inproceedings{giles2008extended,
  title={An extended collection of matrix derivative results for forward and reverse mode algorithmic differentiation. An extended version of a paper that appeared in the proceedings of AD2008},
  author={Giles, M},
  booktitle={the 5th International Conference on Automatic Differentiation},
  year=2008
}

```

useful

---

<div class="post-metadata">

### Author: ![ccc1685](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccc1685/32/12548_2.png) [@ccc1685](https://discourse.julialang.org/u/ccc1685)
#### Post date: [January 31, 2020, 12:03pm UTC](https://discourse.julialang.org/t/help-with-gradient/33943/5 "2020-01-31T12:03:04Z")

</div>

Thanks for your help

---

<div class="post-metadata">

### Author: ![platawiec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/platawiec/32/31914_2.png) [@platawiec](https://discourse.julialang.org/u/platawiec)
#### Post date: [January 31, 2020, 1:12pm UTC](https://discourse.julialang.org/t/help-with-gradient/33943/6 "2020-01-31T13:12:31Z")

</div>

The other problem you will run into is that you are mutating your matrices in-place. This is easy enough to fix. For instance, the line:

```julia
r ./= rin[reactiondecay]

```

Should read:

```julia
r /= rin[reactiondecay]

```

Likewise, your `transition` function would need to be re-written in the same manner. That might not be straightforward in the case of your `B` and `T` transition matrices, and it may be easier to derive and define the adjoints by hand.

One trick I’ve used is to calculate the matrix construction with `ForwardDiff`, and then use the resulting Jacobian to calculate the vector’-Jacobian product, like so:

```julia
forward(params) = ... # constructs a matrix T from params

Zygote.@adjoint function forward(params)
    T = forward(params)
    return T, function (params_bar)
        J = ForwardDiff.jacobian(forward, params)
        # backward pass is vector-Jacobian product
        vjp = params_bar' * J
        return vjp
    end
end

```

It may not be the most _efficient_ way to calculate things, but I trust it more than doing it by hand.

---

<div class="post-metadata">

### Author: ![ccc1685](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccc1685/32/12548_2.png) [@ccc1685](https://discourse.julialang.org/u/ccc1685)
#### Post date: [January 31, 2020, 1:31pm UTC](https://discourse.julialang.org/t/help-with-gradient/33943/7 "2020-01-31T13:31:10Z")

</div>

Thanks. This concept of mutating matrices is foreign to me so your explanation was very helpful.
