-
Notifications
You must be signed in to change notification settings - Fork 24
reverse glow for MAP #65
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
Open
rafaelorozco
wants to merge
5
commits into
master
Choose a base branch
from
glow-map
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c7ad1e3
reverse glow for MAP w/ tests
25feb2c
fix 3d tests for glow jacobian
905dd71
fix glow test
1397079
add logdet in glow inverse,make sure glow tests pass
f55745e
glow works with logdet=false for direct inverse learning
rafaelorozco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,20 +91,24 @@ end | |
| CouplingLayerGlow3D(args...;kw...) = CouplingLayerGlow(args...; kw..., ndims=3) | ||
|
|
||
| # Forward pass: Input X, Output Y | ||
| function forward(X::AbstractArray{T, N}, L::CouplingLayerGlow) where {T,N} | ||
| function forward(X::AbstractArray{T, N}, L::CouplingLayerGlow; save=false) where {T,N} | ||
|
|
||
| X_ = L.C.forward(X) | ||
| X1, X2 = tensor_split(X_) | ||
|
|
||
| Y2 = copy(X2) | ||
| logS_T = L.RB.forward(X2) | ||
| logSm, Tm = tensor_split(logS_T) | ||
| Sm = L.activation.forward(logSm) | ||
| Y1 = Sm.*X1 + Tm | ||
|
|
||
| Y = tensor_cat(Y1, Y2) | ||
| Y = tensor_cat(Y1, X2) | ||
|
|
||
| if L.logdet | ||
| save ? (return Y, Y1, X2, coupling_logdet_forward(Sm), Sm) : (return Y, coupling_logdet_forward(Sm)) | ||
| else | ||
| save ? (return Y, Y1, X2, Sm) : (return Y) | ||
| end | ||
|
|
||
| L.logdet == true ? (return Y, glow_logdet_forward(Sm)) : (return Y) | ||
| end | ||
|
|
||
| # Inverse pass: Input Y, Output X | ||
|
|
@@ -160,13 +164,38 @@ function backward(ΔY::AbstractArray{T, N}, Y::AbstractArray{T, N}, L::CouplingL | |
| end | ||
| end | ||
|
|
||
| # 2D/3D Reverse backward pass: Input (ΔX, X), Output (ΔY, Y) | ||
| function backward_inv(ΔX::AbstractArray{T, N}, X::AbstractArray{T, N}, L::CouplingLayerGlow; set_grad::Bool=true) where {T, N} | ||
|
|
||
| ## Jacobian-related functions | ||
| ΔX, X = L.C.forward((ΔX, X)) | ||
| X1, X2 = tensor_split(X) | ||
| ΔX1, ΔX2 = tensor_split(ΔX) | ||
|
|
||
| function jacobian(ΔX::AbstractArray{T, N}, Δθ::Array{Parameter, 1}, X, L::CouplingLayerGlow) where {T,N} | ||
| # Recompute forward state | ||
| #Y, Y1, X2, S = forward(X, L; save=true) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leftover |
||
| logS_T = L.RB.forward(X2) | ||
| logSm, Tm = tensor_split(logS_T) | ||
| Sm = L.activation.forward(logSm) | ||
| Y1 = Sm.*X1 + Tm | ||
|
|
||
| # Get dimensions | ||
| k = Int(L.C.k/2) | ||
| # Backpropagate residual | ||
| ΔT = -ΔX1 ./ Sm | ||
| ΔS = X1 .* ΔT | ||
| if L.logdet == true | ||
| ΔS += coupling_logdet_backward(Sm) | ||
| end | ||
|
|
||
| ΔY2 = L.RB.backward(tensor_cat(L.activation.backward(ΔS, Sm), ΔT), X2) + ΔX2 | ||
| ΔY1 = -ΔT | ||
|
|
||
| ΔY = tensor_cat(ΔY1, ΔY2) | ||
| Y = tensor_cat(Y1, X2) | ||
|
|
||
| return ΔY, Y | ||
| end | ||
|
|
||
| ## Jacobian-related functions | ||
| function jacobian(ΔX::AbstractArray{T, N}, Δθ::Array{Parameter, 1}, X, L::CouplingLayerGlow) where {T,N} | ||
|
|
||
| ΔX_, X_ = L.C.jacobian(ΔX, Δθ[1:3], X) | ||
| X1, X2 = tensor_split(X_) | ||
|
|
@@ -175,17 +204,19 @@ function jacobian(ΔX::AbstractArray{T, N}, Δθ::Array{Parameter, 1}, X, L::Cou | |
| Y2 = copy(X2) | ||
| ΔY2 = copy(ΔX2) | ||
| ΔlogS_T, logS_T = L.RB.jacobian(ΔX2, Δθ[4:end], X2) | ||
| Sm = L.activation.forward(logS_T[:,:,1:k,:]) | ||
| ΔS = L.activation.backward(ΔlogS_T[:,:,1:k,:], nothing;x=logS_T[:,:,1:k,:]) | ||
| Tm = logS_T[:, :, k+1:end, :] | ||
| ΔT = ΔlogS_T[:, :, k+1:end, :] | ||
| logSm, Tm = tensor_split(logS_T) | ||
| ΔlogSm, ΔT = tensor_split(ΔlogS_T) | ||
|
|
||
| Sm = L.activation.forward(logSm) | ||
| ΔS = L.activation.backward(ΔlogSm, nothing;x=logSm) | ||
| Y1 = Sm.*X1 + Tm | ||
| ΔY1 = ΔS.*X1 + Sm.*ΔX1 + ΔT | ||
| Y = tensor_cat(Y1, Y2) | ||
| ΔY = tensor_cat(ΔY1, ΔY2) | ||
|
|
||
| # Gauss-Newton approximation of logdet terms | ||
| JΔθ = L.RB.jacobian(cuzeros(ΔX2, size(ΔX2)), Δθ[4:end], X2)[1][:, :, 1:k, :] | ||
| JΔθ = L.RB.jacobian(cuzeros(ΔX2, size(ΔX2)), Δθ[4:end], X2)[1] | ||
| JΔθ = tensor_split(JΔθ)[1] | ||
| GNΔθ = cat(0f0*Δθ[1:3], -L.RB.adjointJacobian(tensor_cat(L.activation.backward(JΔθ, Sm), zeros(Float32, size(Sm))), X2)[2]; dims=1) | ||
|
|
||
| L.logdet ? (return ΔY, Y, glow_logdet_forward(Sm), GNΔθ) : (return ΔY, Y) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.