-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add docstring for gradient #62
base: master
Are you sure you want to change the base?
Conversation
true | ||
``` | ||
|
||
Note that `gradient` calculation is disabled for container with integers: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know why this is disabled. Is it possible to return nothing
for it? Otherwise it can very easily be a root of potential bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NiLang assumes gradient type == value type. Integers can be differentiated manually by wrapping GVar. Also, I assume in most cases, people do not want to differentiate them.
Wondering what are the potential bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not very easy to detect and understand that NiLang.AD.gradient(Val(1), i_sum, (0, X))[2]
is zeros (maybe throw a warning for this?)
Another example comes from the Zygote example, and it's quite confusing from the error stack:
julia> norm2'([1,2,3,4])
4-element Vector{Int64}:
2
4
6
8
julia> Zygote.@adjoint function norm2(x::AbstractArray{T}) where T
out = norm2(x)
out, δy -> (grad((~r_norm2)(GVar(out, δy), GVar(x))[2]),)
end
julia> norm2'([1,2,3,4])
ERROR: MethodError: no method matching (::Inv{typeof(r_norm2)})(::GVar{Int64, Int64}, ::Vector{Int64})
Closest candidates are:
(::Inv{typeof(r_norm2)})(::T, ::AbstractArray{T, N} where N) where T at REPL[8]:2
Stacktrace:
[1] (::var"#10#11"{Vector{Int64}, Int64})(δy::Int64)
@ Main ./REPL[11]:3
[2] (::var"#138#back#12"{var"#10#11"{Vector{Int64}, Int64}})(Δ::Int64)
@ Main ~/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:59
[3] (::Zygote.var"#41#42"{var"#138#back#12"{var"#10#11"{Vector{Int64}, Int64}}})(Δ::Int64)
@ Zygote ~/.julia/packages/Zygote/6HN9x/src/compiler/interface.jl:41
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is how you can circumvent this issue:
julia> Zygote.@adjoint function norm2(x::AbstractArray{T}) where T
out = norm2(x)
out, δy -> (grad((~r_norm2)(GVar(out, δy), GVar(x, zero(x)))[2]),)
end
julia> norm2'([1,2,3,4])
4-element Vector{Int64}:
2
4
6
8
Does this make sense?
partially address #53