# Automatic differentiation in Julia for gradient computation

**URL:** https://discourse.julialang.org/t/automatic-differentiation-in-julia-for-gradient-computation/103643
**Category:** General Usage
**Tags:** autodiff
**Created:** [September 8, 2023, 3:50am UTC](https://discourse.julialang.org/t/automatic-differentiation-in-julia-for-gradient-computation/103643 "2023-09-08T03:50:54Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Harsh\_Choudhary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harsh_choudhary/32/52779_2.png) [@Harsh\_Choudhary](https://discourse.julialang.org/u/Harsh_Choudhary)
#### Post date: [September 8, 2023, 3:50am UTC](https://discourse.julialang.org/t/automatic-differentiation-in-julia-for-gradient-computation/103643/1 "2023-09-08T03:50:55Z")

</div>

Hi, I am new to Julia and I am trying to get my head around with the Automatic Differentiation in Julia. I have a decent experience with Pytorch Autograd libraries and I wanted to ask if there is an Analog of loss.backward() in Julia?

My specific use case is as follows:  
I have a variable y which is given as: y = f(W1, W2, x) where W1 and W2 are parameter matrices of some arbitrary dimensions. I want to calculate the gradient of ‘y’ w.r.t. ‘W1’ and ‘W2’ the same way autograd.grad() or loss.bakward() does in pytorch. Is there any way to accomplish that?

---

<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: [September 8, 2023, 4:53am UTC](https://discourse.julialang.org/t/automatic-differentiation-in-julia-for-gradient-computation/103643/2 "2023-09-08T04:53:55Z")

</div>

Hello and welcome!

There are lots of reverse-mode AD libraries in Julia, you could try your hand with [Zygote.jl](https://fluxml.ai/Zygote.jl/dev/) which is the AD used in the DL library Flux.jl

```julia
using Zygote

Zygote.gradient((W1, W2) -> f(W1, W2, x), W1, W2)

```

This creates a closure (anonymous function) `(W1, W2) -> f(W1, W2, x)` that closes over the variable `x`, so that you only compute the gradient w.r.t. `W1,W2`.
