-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
zero
should be type preserving
#29839
Comments
Why? |
a minmal examplemutable struct MyArr{AT}
arr::AT
end
m = randn(2,2)
a = MyArr(m')
a.arr = zero(a.arr) # break what's happening in Flux (simplified version)mutable struct MyTracker{AT}
arr::AT
grad::AT
MyTracker(x) = new{typeof(x)}(x)
end
m = randn(2,2)
a = MyTracker(m')
a.grad = zero(a.arr) # break, no one expect that, right? |
Also, if we believe Base.zero(ad::Adjoint{T, AT}) where {T, AT} = Adjoint{T, AT}(zero(parent(ad))) to make Then the following example will work using LinearAlgebra
Base.zero(a::MyArr) = MyArr(zero(a.arr))
adarr = Adjoint(MyArr);
zero(adarr); |
This could also be handled by having an appropriate convert method. |
There's also an argument to be made that the type of the |
BTW, julia> similar(randn(3,3)') |> typeof
Array{Float64,2}
julia> randn(3,3)'
3×3 LinearAlgebra.Adjoint{Float64,Array{Float64,2}}:
-1.97386 0.537503 -0.306281
-0.187487 -0.669094 -0.154925
-0.407494 0.818063 2.02156 I am not a fan of In the doc, |
Yes, intentionally. Because when you want something similar to an adjoint, you generally don't actually want it to be an adjoint. You just want something that's sufficiently compatible to work. |
Ok, it is really a hard decision between |
I think we can close this issue now, because recently someone fixed it. julia> zero([1,2,3]')
1×3 adjoint(::Vector{Int64}) with eltype Int64:
0 0 0
julia> similar([1,2,3]')
1×3 adjoint(::Vector{Int64}) with eltype Int64:
139677310498048 139677310498496 139677310498832 Cheers! |
The change of type causes trouble in https://github.com/FluxML/Flux.jl/issues/472
Adjoint
,Transpose
wrappers should be keeped.I think this PR #29777 is a proper place to make this change?
The text was updated successfully, but these errors were encountered: