Search code examples
juliaautomatic-differentiation

Automatic differentiation with ForwardDiff in Julia


I am having some trouble using correctly the ForwardDiff package in Julia. I have managed to isolate my problem in the following chunk of code.

In short, I define the function:

using ForwardDiff

function likelihood(mu,X)

  N = size(X,2)

  # Calculate likelihood
  aux = zeros(N)
  for nn=1:N
    aux[nn] = exp(-0.5 * (X[:,nn]-mu)' *  (X[:,nn]-mu))[1]
  end

  # return log-likelihood
  return sum(log(aux))

end

I then check if the function works:

# Check if function works at all
X = randn(2,3) # some random data
mu = [1.0;2.0] # arbitrary mean
@show likelihood(mu,X) # works fine for me

I then try to obtain the gradient using:

ForwardDiff.gradient( ARG -> likelihood(ARG, X), mu)

Unfortunately this fails and I see in my screen:

ERROR: MethodError: convert has no method matching convert(::Type{Float64}, ::ForwardDiff.Dual{2,Float64}) This may have arisen from a call to the constructor Float64(...), since type constructors fall back to convert methods. Closest candidates are:
call{T<:AbstractFloat}(::Type{T<:AbstractFloat}, ::Real, ::RoundingMode{T}) call{T}(::Type{T}, ::Any)
convert(::Type{Float64}, ::Int8) ... in likelihood at none:10 in anonymous at none:1

What am I doing wrong? Thanks, in advance.


Solution

  • I was just informed that this was a careless mistake on my side, though a bit hard to spot to the untrained eye.

    The error is at the call to zeros:

    aux = zeros(N)
    

    Changing this to

    aux = zeros(eltype(mu),N)
    

    solves the problem. Hope this is useful to others.