Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

johnnychen94
Copy link
Contributor

@johnnychen94 johnnychen94 commented Jun 4, 2021

partially address #53

true
```

Note that `gradient` calculation is disabled for container with integers:
Copy link
Contributor Author

@johnnychen94 johnnychen94 Jun 4, 2021

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.

Copy link
Owner

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?

Copy link
Contributor Author

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

Copy link
Owner

@GiggleLiu GiggleLiu Jun 8, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants