Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to find the signature of a function in Julia?

Tags:

julia

Just starting to use Julia, but I can't figure out how do you actually find the function signature in any of the docs...

For example, let's say I want to see what parameters the gradient function of the Flux package takes. How can I find that? If I go to flux docs it's mostly english docs and not function signatures. Similar for julia docs.

Any tips appreciated.

like image 570
Andriy Drozdyuk Avatar asked Sep 26 '20 19:09

Andriy Drozdyuk


Video Answer


2 Answers

Use the methods function to get a list of signatures of all methods of a given function. For example:

julia> methods(sin)
# 13 methods for generic function "sin":
[1] sin(x::BigFloat) in Base.MPFR at mpfr.jl:727
[2] sin(::Missing) in Base.Math at math.jl:1197
[3] sin(a::Complex{Float16}) in Base.Math at math.jl:1145
[4] sin(a::Float16) in Base.Math at math.jl:1144
[5] sin(z::Complex{T}) where T in Base at complex.jl:804
[6] sin(x::T) where T<:Union{Float32, Float64} in Base.Math at special/trig.jl:29
[7] sin(x::Real) in Base.Math at special/trig.jl:53
[8] sin(A::LinearAlgebra.Hermitian{var"#s827",S} where S<:(AbstractArray{var"#s828",2} where var"#s828"<:var"#s827") where var"#s827"<:Complex) in LinearAlgebra at /home/bkamins/julia/share/julia/stdlib/v1.5/LinearAlgebra/src/symmetric.jl:922
[9] sin(A::Union{LinearAlgebra.Hermitian{var"#s828",S}, LinearAlgebra.Symmetric{var"#s828",S}} where S where var"#s828"<:Real) in LinearAlgebra at /home/bkamins/julia/share/julia/stdlib/v1.5/LinearAlgebra/src/symmetric.jl:918
[10] sin(D::LinearAlgebra.Diagonal) in LinearAlgebra at /home/bkamins/julia/share/julia/stdlib/v1.5/LinearAlgebra/src/diagonal.jl:576
[11] sin(A::AbstractArray{var"#s828",2} where var"#s828"<:Real) in LinearAlgebra at /home/bkamins/julia/share/julia/stdlib/v1.5/LinearAlgebra/src/dense.jl:836
[12] sin(A::AbstractArray{var"#s828",2} where var"#s828"<:Complex) in LinearAlgebra at /home/bkamins/julia/share/julia/stdlib/v1.5/LinearAlgebra/src/dense.jl:843
[13] sin(J::LinearAlgebra.UniformScaling) in LinearAlgebra at /home/bkamins/julia/share/julia/stdlib/v1.5/LinearAlgebra/src/uniformscaling.jl:139
like image 177
Bogumił Kamiński Avatar answered Oct 06 '22 12:10

Bogumił Kamiński


It seems that this function is lacking proper documentation and the standard approaches described in other answers (? for the help REPL or methods) will not be very useful.

What I normally do in such cases is to type something line:

@edit gradient(1,2)   

This brings me to function definition (actually I can suspect the first parameter is a function but running methods did not indicate anything.

This will open the editor and you will see something like this:

function gradient(f, args...)
  y, back = pullback(f, args...)
  return back(sensitivity(y))
end

So at this stage you know what your function is doing. If not perhaps you can search for pullback typing ?pullback. When you do that you discover that there is no documentation for this neither but it is a part of Zygote.

However, typing into Google Zygote.pullback will forward you to the documentation you need: https://fluxml.ai/Zygote.jl/latest/adjoints/

So this is kind of hackish. Most of Julia libraries are nicely documented but if not I always start the job with @edit macro.

like image 24
Przemyslaw Szufel Avatar answered Oct 06 '22 12:10

Przemyslaw Szufel