# How can I use Optim/BFGS on functions I can't evaluate but whose gradients I can compute?

**URL:** <https://discourse.julialang.org/t/how-can-i-use-optim-bfgs-on-functions-i-cant-evaluate-but-whose-gradients-i-can-compute/35160>\
**Category:** Optimization (Mathematical)\
**Created:** [February 26, 2020, 10:04am UTC](https://discourse.julialang.org/t/how-can-i-use-optim-bfgs-on-functions-i-cant-evaluate-but-whose-gradients-i-can-compute/35160 "2020-02-26T10:04:05Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [February 27, 2020, 11:13pm UTC](https://discourse.julialang.org/t/how-can-i-use-optim-bfgs-on-functions-i-cant-evaluate-but-whose-gradients-i-can-compute/35160/11 "2020-02-27T23:13:54Z")

</div>

Here’s what I checked, minimizing |x|^2 with a fixed step-size of 0.1. There’s no error, but if I just have my target function return e.g. 0, you can see it terminates early even with a negative `f_tol`,

```julia
using Optim, LineSearches, LinearAlgebra

bfgs = BFGS(
    alphaguess = LineSearches.InitialStatic(alpha=0.1), 
    linesearch = LineSearches.Static()
)

# do a proper minimization
optimize(
    x->dot(x,x),
    x->2x,
    ones(10),
    bfgs,
    inplace=false
).iterations # returns 181

# attempt to always return 0 from the target function,
# but with correct gradient
# this doesn't work and will terminate early
optimize(
    x->0,
    x->2x,
    ones(10),
    bfgs,
    Optim.Options(f_tol=-1, allow_f_increases=true),
    inplace=false
).iterations # returns 2

# the only workaround I've found 
f₀ = 0
optimize(
    x->(global f₀-=1),
    x->2x,
    ones(10),
    bfgs,
    inplace=false
).iterations # returns 181

```

---

_[View the full topic](https://discourse.julialang.org/t/how-can-i-use-optim-bfgs-on-functions-i-cant-evaluate-but-whose-gradients-i-can-compute/35160)._
