# Zygote - parametrize matrix such that gradient is only performed on selected coefficients

**URL:** <https://discourse.julialang.org/t/zygote-parametrize-matrix-such-that-gradient-is-only-performed-on-selected-coefficients/62393>\
**Category:** Machine Learning\
**Tags:** question, zygote, matrices, natural-gradient, autodiff\
**Created:** [June 4, 2021, 2:50pm UTC](https://discourse.julialang.org/t/zygote-parametrize-matrix-such-that-gradient-is-only-performed-on-selected-coefficients/62393 "2021-06-04T14:50:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgbrasier](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jgbrasier](https://discourse.julialang.org/u/jgbrasier)\
**Post date:** [June 4, 2021, 2:50pm UTC](https://discourse.julialang.org/t/zygote-parametrize-matrix-such-that-gradient-is-only-performed-on-selected-coefficients/62393/1 "2021-06-04T14:50:26Z")

</div>

I was wondering if it was possible to parametrize matrices such that zygote’s gradient would only be performed on the selected coefficients of the matrix.

Some dummy code would look like:

```julia
theta = randn(2)
W = [theta[1] theta[2]; 0.0 theta[1]]
b = randn(100)
x = randn(2, 100)

gs = gradient(w -> sum(w*x+b), W)

```

However the gradient would only be performed on the values of theta, keeping W[2, 1] set to 0.0 for the entire gradient descent. Also this would imply that the gradients for theta[1] and theta[2] be equal at all times.  
Am I completely going at this wrong way? or is it just not possible in julia?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 5, 2021, 5:34am UTC](https://discourse.julialang.org/t/zygote-parametrize-matrix-such-that-gradient-is-only-performed-on-selected-coefficients/62393/2 "2021-06-05T05:34:14Z")

</div>

You have the right idea, but you need to take the gradient w.r.t. theta instead of W, just wrap the function that accepts W in a function that accepts theta instead.
