Flux.jl/src/tracker/numeric.jl
2017-08-27 09:49:42 +01:00

23 lines
478 B
Julia
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function gradient(f, xs::AbstractArray...)
xs = track.(xs)
back!(f(xs...))
grad.(xs)
end
function ngradient(f, xs::AbstractArray...)
y = f(xs...)
grads = zeros.(xs)
for (x, Δ) in zip(xs, grads)
for i in 1:length(x)
δ = sqrt(eps())
tmp, x[i] = x[i], x[i]+δ
y = f(xs...)
x[i] = tmp
Δ[i] = (y-y)/δ
end
end
return grads
end
gradcheck(f, xs...) = all(isapprox.(ngradient(f, xs...), gradient(f, xs...), rtol = 1e-6))