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

Little Bug-fix #1

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/optimize_flow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,29 @@ function negll_flow_loss(flow::F, x::AbstractMatrix{<:Real}, logd_orig::Abstract
end

function negll_flow(flow::F, x::AbstractMatrix{<:Real}, logd_orig::AbstractVector, logpdf::Tuple{Function, Function}) where F<:AbstractFlow
negll, back = Zygote.pullback(negll_flow, flow, x, logd_orig, logpdf[2])
negll, back = Zygote.pullback(negll_flow_loss, flow, x, logd_orig, logpdf[2])
d_flow = back(one(eltype(x)))[1]
return negll, d_flow
end
export negll_flow

function KLDiv_flow_loss(flow::F, x::AbstractMatrix{<:Real}, logd_orig::AbstractVector, logpdfs::Tuple{Function, Function}) where F<:AbstractFlow
function KLDiv_flow_loss(flow::F, x::AbstractMatrix{<:Real}, logd_orig::AbstractVector, logpdf::Function) where F<:AbstractFlow
nsamples = size(x, 2)
flow_corr = fchain(flow,logpdfs[2].f)
logpdf_y = logpdfs[2].logdensity
flow_corr = fchain(flow,logpdf.f)
#logpdf_y = logpdfs[2].logdensity
y, ladj = with_logabsdet_jacobian(flow_corr, x)
KLDiv = sum(exp.(logd_orig - vec(ladj)) .* (logd_orig - vec(ladj) - logpdf_y(y))) / nsamples
KLDiv = sum(exp.(logd_orig - vec(ladj)) .* (logd_orig - vec(ladj) - logpdf.logdensity(y))) / nsamples
return KLDiv
end

function KLDiv_flow(flow::F, x::AbstractMatrix{<:Real}, logd_orig::AbstractVector, logpdfs::Tuple{Function, Function}) where F<:AbstractFlow
KLDiv, back = Zygote.pullback(KLDiv_flow_loss, flow, x, logd_orig, logpdfs)
function KLDiv_flow(flow::F, x::AbstractMatrix{<:Real}, logd_orig::AbstractVector, logpdf::Tuple{Function, Function}) where F<:AbstractFlow
KLDiv, back = Zygote.pullback(KLDiv_flow_loss, flow, x, logd_orig, logpdf[2])
d_flow = back(one(eltype(x)))[1]
return KLDiv, d_flow
end
export KLDiv_flow


function optimize_flow(samples::Union{Matrix, Tuple{Matrix, Matrix}},
initial_flow::F where F<:AbstractFlow,
optimizer;
Expand Down