Skip to content

Instantly share code, notes, and snippets.

@dermesser
Last active August 22, 2021 15:12
Show Gist options
  • Select an option

  • Save dermesser/ce70f177ff5e17fa06d548d1325f6b5d to your computer and use it in GitHub Desktop.

Select an option

Save dermesser/ce70f177ff5e17fa06d548d1325f6b5d to your computer and use it in GitHub Desktop.
Simple numeric gradients
# Author: Lewin Bormann, 2021
# A simple finite difference algorithm for differentiating a function numerically with respect to multiple dimensions.
function mydiff(func, theta, releps=1e-3, reltol=1e-3)
scale_eps = 1/2
grad = zeros(size(theta))
upper, lower = theta[:], theta[:]
for (i, t) in enumerate(theta)
count = 0
flipflop = 0
this_eps = releps * t
last_diff = 0
while true
upper[i] += this_eps/2
lower[i] -= this_eps/2
diff = (func(upper...)-func(lower...))/this_eps
println("$i $count: $this_eps -> $diff")
if this_eps < 1e-6
grad[i] = diff
break
end
upper[i] -= this_eps/2
lower[i] += this_eps/2
ratio = last_diff / diff
if ratio - 1 >= 0
if abs(ratio-1) < reltol
grad[i] = diff
break
else
last_diff = diff
end
else
flipflop += 1
last_diff = diff
end
if count > 5
grad[i] = diff
break
end
this_eps *= scale_eps
count += 1
end
end
return grad
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment