# Using ReverseSourceDiff for a function with matrices

**URL:** <https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773>\
**Category:** New to Julia\
**Tags:** differentiation\
**Created:** [June 19, 2018, 2:19am UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773 "2018-06-19T02:19:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [June 19, 2018, 2:19am UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/1 "2018-06-19T02:19:57Z")

</div>

I was wondering if anyone could help me with why my code will not work:

```julia
using ReverseDiffSource
X_=randn(2,2)
Y_=randn(2,2)
DerivativeE_c=m.rdiff(E_c, (Matrix{Float64}, Matrix{Float64}))
DerivativeE_c(X_,Y_)

```

Where E\_c is my function. I’ve modeled my code after the third example on this page:  
[https://github.com/JuliaDiff/ReverseDiffSource.jl](https://github.com/JuliaDiff/ReverseDiffSource.jl)  
But I cannot get the package to work at all. I get that m is not defined or rdiff is not. Have I modeled this function correctly?

---

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [June 19, 2018, 2:23am UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/2 "2018-06-19T02:23:50Z")

</div>

To clarify, there were no errors in adding the package:

```julia
Pkg.add("ReverseDiffSource")

```

---

<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:** [June 19, 2018, 6:12am UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/3 "2018-06-19T06:12:53Z")

</div>

AFAICT this package is abandonned, and was not even updated for 0.6.

[https://github.com/JuliaDiff/ReverseDiffSource.jl/issues/52](https://github.com/JuliaDiff/ReverseDiffSource.jl/issues/52)

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [June 19, 2018, 6:45am UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/4 "2018-06-19T06:45:05Z")

</div>

I think XGrad.jl is in some ways the successor of ReverseDiffSource.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [June 19, 2018, 10:03am UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/5 "2018-06-19T10:03:16Z")

</div>

```julia
m.rdiff

```

This looks like a typo in the original README, function for differentiation in ReverseDiffSource is just `rdiff` as it’s shown in all other examples.

Anyway, as mentioned above, ReverseDiffSource.jl haven’t seen any updates for 2 years, but I maintain a similar package - [XGrad.jl](https://github.com/dfdx/XGrad.jl). You can use it as follows:

```julia
using XGrad

E_c(x, y) = sum(x * y)

DerivativeE_c = xdiff(E_c; x=rand(2,2), y=rand(2,2))

X_ = randn(2,2)
Y_ = randn(2,2)

DerivativeE_c(X_,Y_)

```

Note that latest version already targets Julia 0.7, so if you are on Julia 0.6 you need to checkout branch `julia-0.6` of XGrad AND a branch with the same name in [Espresso](https://github.com/dfdx/Espresso.jl).

---

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [June 19, 2018, 10:06pm UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/6 "2018-06-19T22:06:37Z")

</div>

Thank you so much that looks like it will work nicely for me. I am new to julia, would you be able to explain how to install just a branch? I learned how to install a package from a website but I cannot find the exact .jl webpage for the branch, just this one  
[https://github.com/dfdx/XGrad.jl/tree/julia-0.6](https://github.com/dfdx/XGrad.jl/tree/julia-0.6)

It looks like the correct Espresso is  
[https://github.com/dfdx/Espresso.jl/releases/tag/v0.4.0](https://github.com/dfdx/Espresso.jl/releases/tag/v0.4.0)  
I believe

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [June 19, 2018, 11:44pm UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/7 "2018-06-19T23:44:45Z")

</div>

In Julia 0.6 (the current stable version that you most likely are using), you can add registered package such as XGrad just as:

```julia
Pkg.add("XGrad")

```

or clone the master branch using (also works for unregistered packages):

```julia
Pkg.clone("https://github.com/dfdx/XGrad.jl")

```

This puts the whole git repository of the package somewhere on your disk, e.g. on Linux the default location is `~/.julia/v0.6/XGrad`.

Espresso.jl is a dependency of XGrad.jl and is installed automatically into similar location, e.g. `~/.julia/v0.6/Espresso`.

Once you get both of the packages, you can enter the directory of the project and, since it’s still a git repo, switch to the branch you want. Again, in Linux you can do it as:

```julia
cd ~/.julia/v0.6/XGrad
git checkout julia-0.6

cd ~/.julia/v0.6/Espresso
git checkout julia-0.6

```

Alternatively, you can switch a branch right from julia console as follows:

```julia
Pkg.checkout("XGrad", "julia-0.6")
Pkg.checkout("Espresso", "julia-0.6")

```

which should also work on Windows.

In Julia 0.7 things are a bit different, but I won’t describe it for now to not overload you with too much information.

---

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [June 20, 2018, 12:35pm UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/8 "2018-06-20T12:35:24Z")

</div>

Okay, thank you I understand now and I’ve gotten it working for me!

---

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [June 22, 2018, 7:13pm UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/9 "2018-06-22T19:13:14Z")

</div>

Does XGrad support linear algebra functions such as kron, trace, conj, and exponentials of matrices? My function seems too complicated for XGrad as I get the error:  
`Can't understand module of Main`  
But I also may be wrongly setting it up.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [June 22, 2018, 8:08pm UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/10 "2018-06-22T20:08:45Z")

</div>

XGrad uses simple chain rule over a set of functions with known derivatives. The default list of such functions and derivatives is defined in [rules.jl](https://github.com/dfdx/XGrad.jl/blob/master/src/rules.jl) and can be extended using `@diffrule` macro. However, there’s no derivatives for functions you mentioned, nor I can find them in [DiffRules.jl](https://github.com/JuliaDiff/DiffRules.jl/blob/master/src/rules.jl) (another repository with similar goal). So if you find derivatives for them, I’ll add these to the list.

> Can’t understand module of Main

This seems unrelated, can you please post the whole example as an issue on GitHub?

---

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [June 23, 2018, 5:12pm UTC](https://discourse.julialang.org/t/using-reversesourcediff-for-a-function-with-matrices/11773/11 "2018-06-23T17:12:45Z")

</div>

Okay, for the moment I need to clear up and refine my code, I’ll post again when I have done this. Thank you
