-
-
Notifications
You must be signed in to change notification settings - Fork 71
Gradient definitions & supertypes for Zygote, continued #169
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
Changes from 3 commits
5c6d7fd
6af53d5
b554abc
fe2db07
c84842a
b3ed973
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| function ChainRulesCore.rrule(::typeof(getindex),VA::AbstractVectorOfArray, i::Union{Int,AbstractArray{Int},CartesianIndex,Colon,BitArray,AbstractArray{Bool}}) | ||
| function AbstractVectorOfArray_getindex_adjoint(Δ) | ||
| Δ′ = [ (i == j ? Δ : zero(x)) for (x,j) in zip(VA.u, 1:length(VA))] | ||
| (NoTangent(),Δ′,NoTangent()) | ||
| (NoTangent(),VectorOfArray(Δ′),NoTangent()) | ||
| end | ||
| VA[i],AbstractVectorOfArray_getindex_adjoint | ||
| end | ||
|
|
@@ -10,7 +10,7 @@ function ChainRulesCore.rrule(::typeof(getindex),VA::AbstractVectorOfArray, indi | |
| function AbstractVectorOfArray_getindex_adjoint(Δ) | ||
| Δ′ = zero(VA) | ||
| Δ′[indices...] = Δ | ||
| (NoTangent(), Δ′, indices[1],map(_ -> NoTangent(), indices[2:end])...) | ||
| (NoTangent(), VectorOfArray(Δ′), indices[1],map(_ -> NoTangent(), indices[2:end])...) | ||
| end | ||
| VA[indices...],AbstractVectorOfArray_getindex_adjoint | ||
| end | ||
|
|
@@ -19,7 +19,7 @@ function ChainRulesCore.rrule(::Type{<:ArrayPartition}, x::S, ::Type{Val{copy_x} | |
| function ArrayPartition_adjoint(_y) | ||
| y = Array(_y) | ||
| starts = vcat(0,cumsum(reduce(vcat,length.(x)))) | ||
| NoTangent(), ntuple(i -> reshape(y[starts[i]+1:starts[i+1]], size(x[i])), length(x)), NoTangent() | ||
| NoTangent(), ArrayPartition(ntuple(i -> reshape(y[starts[i]+1:starts[i+1]], size(x[i]))), length(x)), NoTangent() | ||
| end | ||
|
|
||
| ArrayPartition(x, Val{copy_x}), ArrayPartition_adjoint | ||
|
|
@@ -43,10 +43,21 @@ function ChainRulesCore.rrule(::typeof(getproperty),A::ArrayPartition, s::Symbol | |
| A.x,literal_ArrayPartition_x_adjoint | ||
| end | ||
|
|
||
| # Define a new species of projection operator for this type: | ||
| ChainRulesCore.ProjectTo(x::VectorOfArray) = ChainRulesCore.ProjectTo{VectorOfArray}() | ||
|
|
||
| # Gradient from iteration will be e.g. Vector{Vector}, this makes it another AbstractMatrix | ||
| (::ChainRulesCore.ProjectTo{VectorOfArray})(dx::AbstractVector{<:AbstractArray}) = VectorOfArray(dx) | ||
| # Gradient from broadcasting will be another AbstractArray | ||
| (::ChainRulesCore.ProjectTo{VectorOfArray})(dx::AbstractArray) = dx | ||
|
||
|
|
||
| # These rules duplicate the `rrule` methods above, because Zygote looks for an `@adjoint` | ||
| # definition first, and finds its own before finding those. | ||
|
|
||
| ZygoteRules.@adjoint function getindex(VA::AbstractVectorOfArray, i::Union{Int,AbstractArray{Int},CartesianIndex,Colon,BitArray,AbstractArray{Bool}}) | ||
| function AbstractVectorOfArray_getindex_adjoint(Δ) | ||
| Δ′ = [ (i == j ? Δ : zero(x)) for (x,j) in zip(VA.u, 1:length(VA))] | ||
|
||
| (Δ′,nothing) | ||
| (VectorOfArray(Δ′),nothing) | ||
| end | ||
| VA[i],AbstractVectorOfArray_getindex_adjoint | ||
| end | ||
|
|
@@ -55,11 +66,10 @@ ZygoteRules.@adjoint function getindex(VA::AbstractVectorOfArray, i::Union{Int,A | |
| function AbstractVectorOfArray_getindex_adjoint(Δ) | ||
| Δ′ = zero(VA) | ||
| Δ′[i,j...] = Δ | ||
| (Δ′, i,map(_ -> nothing, j)...) | ||
| (VectorOfArray(Δ′), nothing, map(_ -> nothing, j)...) | ||
| end | ||
| VA[i,j...],AbstractVectorOfArray_getindex_adjoint | ||
| end | ||
|
|
||
| ZygoteRules.@adjoint function ArrayPartition(x::S, ::Type{Val{copy_x}} = Val{false}) where {S<:Tuple,copy_x} | ||
| function ArrayPartition_adjoint(_y) | ||
| y = Array(_y) | ||
|
|
@@ -71,11 +81,11 @@ ZygoteRules.@adjoint function ArrayPartition(x::S, ::Type{Val{copy_x}} = Val{fal | |
| end | ||
|
|
||
| ZygoteRules.@adjoint function VectorOfArray(u) | ||
| VectorOfArray(u),y -> ([y[ntuple(x->Colon(),ndims(y)-1)...,i] for i in 1:size(y)[end]],) | ||
| VectorOfArray(u),y -> (VectorOfArray([y[ntuple(x->Colon(),ndims(y)-1)...,i] for i in 1:size(y)[end]]),) | ||
| end | ||
|
|
||
| ZygoteRules.@adjoint function DiffEqArray(u,t) | ||
| DiffEqArray(u,t),y -> ([y[ntuple(x->Colon(),ndims(y)-1)...,i] for i in 1:size(y)[end]],nothing) | ||
| DiffEqArray(u,t),y -> (DiffEqArray([y[ntuple(x->Colon(),ndims(y)-1)...,i] for i in 1:size(y)[end]],t),nothing) | ||
| end | ||
|
|
||
| ZygoteRules.@adjoint function ZygoteRules.literal_getproperty(A::ArrayPartition, ::Val{:x}) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.