# ForwardDiff gradient with additional parameters

**URL:** <https://discourse.julialang.org/t/forwarddiff-gradient-with-additional-parameters/3186>\
**Category:** General Usage\
**Created:** [April 12, 2017, 8:35pm UTC](https://discourse.julialang.org/t/forwarddiff-gradient-with-additional-parameters/3186 "2017-04-12T20:35:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)\
**Post date:** [April 12, 2017, 8:35pm UTC](https://discourse.julialang.org/t/forwarddiff-gradient-with-additional-parameters/3186/1 "2017-04-12T20:35:58Z")

</div>

I am trying to use `ForwardDiff.gradient` on a function that takes arguments at which the gradient should be taken, as well as data. For example

```julia
using Distributions, ForwardDiff
srand(1)
D = rand(Normal(5.0, 1.0), 10)

f(x, D) = loglikelihood(Normal(x[1], x[2]), D)

```

I would like to take the gradient of `f` wrt `x`, using data `D`. I can “hack” this by setting `D` as a global parameter e.g.

```julia
using Distributions, ForwardDiff
srand(1)
D = rand(Normal(5.0, 1.0), 10)

f(x) = loglikelihood(Normal(x[1], x[2]), D)
ForwardDiff.gradient(f, [5.0, 1.0])

```

But I don’t like this code for more general usage. What I’d like to do is something along the lines of

```julia
g(x, D) = loglikelihood(Normal(x[1], x[2]), D)
ForwardDiff.gradient(g, [5.0, 1.0], D=D)

```

But I’m not sure what the correct syntax is. Any advice on the correct usage of `ForwardDiff` in this setting?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 12, 2017, 8:41pm UTC](https://discourse.julialang.org/t/forwarddiff-gradient-with-additional-parameters/3186/2 "2017-04-12T20:41:06Z")

</div>

An anonymous function would solve your problem nicely:

```julia
julia> f(x, D) = loglikelihood(Normal(x[1], x[2]), D)
f (generic function with 1 method)

julia> ForwardDiff.gradient(x -> f(x, D), [5.0, 1.0])
2-element Array{Float64,1}:
 0.533077
 2.26674 

```

Note that this is basically the same as your global D “hack” (which is actually a perfectly fine way to solve the problem as well):

```julia
julia> function my_function()
       D = rand(Normal(5.0, 1.0), 10)
       f(x) = loglikelihood(Normal(x[1], x[2]), D)
       ForwardDiff.gradient(f, [5.0, 1.0])
   end
my_function (generic function with 1 method)

julia> my_function()
2-element Array{Float64,1}:
 -1.26298
 -3.8625 

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 12, 2017, 8:42pm UTC](https://discourse.julialang.org/t/forwarddiff-gradient-with-additional-parameters/3186/3 "2017-04-12T20:42:55Z")

</div>

Also, the general term for what I’m suggesting is a [closure](https://en.wikipedia.org/wiki/Closure_(computer_programming)).

---

<div class="post-metadata">

**Author:** ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)\
**Post date:** [April 12, 2017, 8:52pm UTC](https://discourse.julialang.org/t/forwarddiff-gradient-with-additional-parameters/3186/4 "2017-04-12T20:52:27Z")

</div>

Thanks @rdeits ! That works.
