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

Summ net #82

Merged
merged 7 commits into from
Jun 9, 2023
Merged

Summ net #82

merged 7 commits into from
Jun 9, 2023

Conversation

rafaelorozco
Copy link
Collaborator

First try at adding a summary network to conditional glow.

I could use some extra eyes on how to implement this cleanly. Although it gets the job done and works with arbitrary flux layers, it feel a little bit clunky.

@codecov
Copy link

codecov bot commented May 9, 2023

Codecov Report

Patch coverage: 96.00% and project coverage change: +0.05 🎉

Comparison is base (671e9c1) 88.47% compared to head (7fb07d9) 88.52%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #82      +/-   ##
==========================================
+ Coverage   88.47%   88.52%   +0.05%     
==========================================
  Files          33       34       +1     
  Lines        2577     2589      +12     
==========================================
+ Hits         2280     2292      +12     
  Misses        297      297              
Impacted Files Coverage Δ
src/InvertibleNetworks.jl 60.00% <ø> (ø)
src/layers/layer_resnet.jl 93.75% <80.00%> (+0.41%) ⬆️
...rc/networks/invertible_network_conditional_glow.jl 96.05% <100.00%> (ø)
src/networks/summarized_net.jl 100.00% <100.00%> (ø)
src/utils/neuralnet.jl 75.47% <100.00%> (+0.96%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

end

@Flux.functor NetworkConditionalGlow

# Constructor
function NetworkConditionalGlow(n_in, n_cond, n_hidden, L, K;freeze_conv=false, split_scales=false, rb_activation::ActivationFunction=ReLUlayer(), k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, ndims=2, squeezer::Squeezer=ShuffleLayer(), activation::ActivationFunction=SigmoidLayer())
function NetworkConditionalGlow(n_in, n_cond, n_hidden, L, K; freeze_conv=false, split_scales=false, summary_net = nothing, rb_activation::ActivationFunction=ReLUlayer(), k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, ndims=2, squeezer::Squeezer=ShuffleLayer(), activation::ActivationFunction=SigmoidLayer())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this different than just the composition sumarry \odot G ?

The whole point of Chain/odot is to easily combine/chain networks and have it work well together, I don't particularly like having networks as input of another like that.

Copy link
Collaborator Author

@rafaelorozco rafaelorozco May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main difference is that composition is not defined for conditional networks.

Practically:
I don't think it will work without some extra work because only one of the inputs to G goes into summary.
(x,y) -> G(x, summary(y))

And summary isnt necessarily invertible so the backward needs to be provide the input to the summary for the gradient calculation (C_save here)

Zx, Zy, lgdet = G.forward(X, Y)
dx, x, dy = G.backward(Zx, Zx, Zy; C_save = Y)

So it will not play nicely with the composition operator that was built for invertible networks.

That said, I agree that it isnt pretty and if these things are easy to solve I am all for it.

@rafaelorozco
Copy link
Collaborator Author

rafaelorozco commented May 31, 2023

How about something like this previous commit @mloubout ?
you make the summarized net like this:

sum_net = ResNet(n_cond, 16, 3; norm=nothing) # make sure it doesnt have any weird normalizati8ons

# Network and input
flow = NetworkConditionalGlow(n_in, n_cond, n_hidden, L, K; ndims=length(N))
G = SummarizedNet(flow, sum_net)

And the main operations are the same as before

Y, ZCond = G.forward(X,Cond)
X_ = G.inverse(Y,ZCond) 
G.backward(Y, Y, ZCond; Y_save = Cond)

@rafaelorozco rafaelorozco merged commit cc8a408 into master Jun 9, 2023
11 checks passed
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.

None yet

2 participants