JuliaAttic / ReverseDiffSource.jl

Reverse automated differentiation from source
MIT License
47 stars 12 forks source link

invalid ASCII sequence #24

Open baggepinnen opened 9 years ago

baggepinnen commented 9 years ago

I have stumbled upon a bug I can't really explain. I'm trying out autodiff on the pdf for a MvNormal, so I have written a function which takes the parameters of a MvNormal distribution and returns a second function which is the pdf. Autodiff complains at an invalid ASCII-sequence?

using ReverseDiffSource
function fN( m, s)
    k = size(m,1)
    fpdf(x) = begin d = x-m; (sqrt((2*pi)^k*det(s))*exp(-0.5*d'*(inv(s)*d)))[1]; end
end
D = 2
fpdf = fN(zeros(D), eye(D))
dfN = rdiff(fpdf, (zeros(D), ))

julia> dfN = rdiff(fpdf, (zeros(D),)) ERROR: ArgumentError: invalid ASCII sequence in convert at ./ascii.jl:107 in setindex! at array.jl:314 in show at /local/home/fredrikb/.julia/v0.4/ReverseDiffSource/src/graph.jl:55 in print at strings/io.jl:8 in print_to_string at ./strings/io.jl:36 in myeval at /local/home/fredrikb/.julia/v0.4/ReverseDiffSource/src/graph.jl:322 in evaluate at /local/home/fredrikb/.julia/v0.4/ReverseDiffSource/src/graph.jl:354 in calc! at /local/home/fredrikb/.julia/v0.4/ReverseDiffSource/src/graph.jl:410 in rdiff at /local/home/fredrikb/.julia/v0.4/ReverseDiffSource/src/rdiff.jl:34 in rdiff at /local/home/fredrikb/.julia/v0.4/ReverseDiffSource/src/frdiff.jl:23

fredo-dedup commented 9 years ago

I've discovered not one but several issues in ReverseDiffSource with your example ! I'll see if I can solve them.

In the meantime, here's a workaround :

    D = 2
    m, s = zeros(D), eye(D)

    function fpdf2(x) 
        d = x - m
        dt = d'
        k = size(m,1)
        (sqrt((2*pi) ^ k * det(s)) * exp(-0.5 * (dt * (inv(s) * d))) )[1]
    end

        # new derivation rule needed
    @deriv_rule ctranspose(x::AbstractArray) x ctranspose(ds)
    dfN = rdiff(fpdf2, (zeros(D), ))

    dfN(2*ones(2)) # (value, gradient for x)

Variables m and s have to be defined as globals (not function-local varaibles) for derivation to work. This is a limitation of the package as it's designed currently.