# In Zygote, why (model)-\>... in gradient

**URL:** <https://discourse.julialang.org/t/in-zygote-why-model-in-gradient/34367>\
**Category:** Machine Learning\
**Created:** [February 9, 2020, 3:42am UTC](https://discourse.julialang.org/t/in-zygote-why-model-in-gradient/34367 "2020-02-09T03:42:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaynick](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jaynick](https://discourse.julialang.org/u/jaynick)\
**Post date:** [February 9, 2020, 3:42am UTC](https://discourse.julialang.org/t/in-zygote-why-model-in-gradient/34367/1 "2020-02-09T03:42:59Z")

</div>

In this (helpful!) post,  
[https://kiranshila.com/index.php/2020/02/04/teaching-myself-machine-learning-with-julia-part-1/](https://kiranshila.com/index.php/2020/02/04/teaching-myself-machine-learning-with-julia-part-1/)

The (zygote) gradient call is

` g = gradient(model -> mse(model.(x),y), model)`

I tried instead doing

` g = gradient(() -> mse(model.(x),y), model)`

and it did not work.

What are the arguments to gradient supposed to be?  
Does not seem to be specified in the documentation.

---

<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:** [February 9, 2020, 5:57am UTC](https://discourse.julialang.org/t/in-zygote-why-model-in-gradient/34367/2 "2020-02-09T05:57:26Z")

</div>

the function `gradient` must know what you would like to take the gradient with respect to. Typically, you’d like to take the gradient wrt the parameters of the model, which Zygote let’s you do by simply passing the entire model.

You could also take the gradient wrt, for instance, the input `x` if you would like.

---

<div class="post-metadata">

**Author:** ![jaynick](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jaynick](https://discourse.julialang.org/u/jaynick)\
**Post date:** [February 9, 2020, 5:11pm UTC](https://discourse.julialang.org/t/in-zygote-why-model-in-gradient/34367/3 "2020-02-09T17:11:01Z")

</div>

restating, your reply clarifies that giving ‘model’ as the explicit argument of the function tells zygote to take the gradient with respect to the parameters in model.

But what then is the purpose and meaning of the ‘model’ parameter that is passed gradient, this one  
g = gradient(model → mse(model.(x),y), **model** )

Probably a different example would be better to explain. Suppose the function has two arguments,  
` h(x,y) = x+y`  
How do you get the gradient with respect to y?

---

<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:** [February 10, 2020, 6:15am UTC](https://discourse.julialang.org/t/in-zygote-why-model-in-gradient/34367/4 "2020-02-10T06:15:04Z")

</div>

What you write means the following  
`g = gradient(z-> mse(z.(x),y), model)`  
You take the gradient of the anonymous function with respect to its input in the point `model`

---

<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:** [February 10, 2020, 6:16am UTC](https://discourse.julialang.org/t/in-zygote-why-model-in-gradient/34367/5 "2020-02-10T06:16:23Z")

</div>

> [@jaynick](#):
>
> Suppose the function has two arguments,  
> ` h(x,y) = x+y`  
> How do you get the gradient with respect to y?

`gradient(y->h(x, y), y) `
