# Optim - provide gradient with fixed parameters

**URL:** https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181
**Category:** Optimization (Mathematical)
**Created:** [January 28, 2019, 2:30pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181 "2019-01-28T14:30:50Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)
#### Post date: [January 28, 2019, 2:30pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/1 "2019-01-28T14:30:50Z")

</div>

I’m using Optim and the BFGS algarithm in order to minimize a function. In order to speed up the minimization I want to provide the gradient of the objective function. However both, the objective function as well as the gradient depends on some constant parameters. I know, how to pass the constant parameters for objective function by

```julia
optimize(x -> mse(x, p), start_guess, BFGS() )

```

How can I do the same with the gradient function?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 28, 2019, 2:33pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/2 "2019-01-28T14:33:30Z")

</div>

Why not in the same way?

---

<div class="post-metadata">

### Author: ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)
#### Post date: [January 28, 2019, 2:36pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/3 "2019-01-28T14:36:33Z")

</div>

I tried already

```julia
optimize(x -> mse(x, p), x-> g(x,p), start_guess, BFGS() )

```

but its not working.

---

<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: [January 28, 2019, 3:01pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/4 "2019-01-28T15:01:09Z")

</div>

Please provide a self-contained example.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)
#### Post date: [January 29, 2019, 7:55am UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/5 "2019-01-29T07:55:46Z")

</div>

I solved the problem partially. If I use the following code

```julia
using Optim
using Plots

function f(x, p)
    return (x[1] - p[1])^2 + (x[2] - p[2])^2
end

function g!(x, G, p)
    G[1] = 2 * (x[1] - p[1])
    G[2] = 2 * (x[2] - p[2])
end

function fit()
    p = [3, 6]
    initial_x = rand(2)
    res = optimize(x -> f(x, p), initial_x, BFGS(), Optim.Options(show_trace = true) )
    return res, res2
end

```

it works fine now. I still can save computation if I would use the the function “only\_fg!(fg!)” ([Optim → only\_fg!](https://github.com/JuliaNLSolvers/Optim.jl/blob/master/docs/src/user/tipsandtricks.md)). (Of course not in this example, but in my calculations later.)

```julia
function fg!(F,G,x,p)
  # common computations not done in this example
  if G != nothing
    G[1] = 2 * (x[1] - p[1])
    G[2] = 2 * (x[2] - p[2])
  end
  if F != nothing
    return (x[1] - p[1])^2 + (x[2] - p[2])^2
  end
end

```

However, calling this function by

```julia
optimize( Optim.only_fg!(x->fg!(x, [3,5])), [0., 0.], LBFGS())

```

is not working.

---

<div class="post-metadata">

### Author: ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)
#### Post date: [January 29, 2019, 1:31pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/6 "2019-01-29T13:31:55Z")

</div>

> [@itzui](#):
>
> However, calling this function by
> 
> ```julia
> optimize( Optim.only_fg!(x->fg!(x, [3,5])), [0., 0.], LBFGS())
> 
> ```
> 
> is not working.

No, because you should write

```julia
(F, G, x)->fg!(F, G, x, [3,5])

```

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 29, 2019, 2:24pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/7 "2019-01-29T14:24:57Z")

</div>

Why don’t you use the (built-in?) automatic differentiation?

---

<div class="post-metadata">

### Author: ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)
#### Post date: [February 5, 2019, 7:00pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/8 "2019-02-05T19:00:38Z")

</div>

Thanks a lot for this clear answer. This of course works.

---

<div class="post-metadata">

### Author: ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)
#### Post date: [February 5, 2019, 7:05pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/9 "2019-02-05T19:05:11Z")

</div>

Unfortunenately, for the objective function I have to calculate eigenvalues and eigenvectors and automatic differentiation cannot be applied.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [February 5, 2019, 7:55pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/10 "2019-02-05T19:55:45Z")

</div>

But you can differentiate them by hand ？

---

<div class="post-metadata">

### Author: ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)
#### Post date: [February 5, 2019, 8:10pm UTC](https://discourse.julialang.org/t/optim-provide-gradient-with-fixed-parameters/20181/11 "2019-02-05T20:10:12Z")

</div>

Unfortunately, I cannot differentiate them by hand. But I can approximate how the objective function is affected by a certain parameter and so, for numerical differentiation, I have to calculate only some parts of the objective function.
