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

unify api #126

Merged
merged 3 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/ICNF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ using Adapt,

include("types.jl")
include("defaults.jl")
include("base_icnf.jl")
include("base_cond_icnf.jl")
include("core_icnf.jl")
include("core_cond_icnf.jl")

Expand Down
77 changes: 77 additions & 0 deletions src/base_cond_icnf.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
export inference, generate, loss

function inference(
icnf::AbstractCondICNF{T, AT},
mode::Mode,
xs::AbstractMatrix,
ys::AbstractMatrix,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::Tuple where {T <: AbstractFloat, AT <: AbstractArray}
n_aug = n_augment(icnf, mode)
zrs = convert(AT, zeros(T, n_aug+1, size(xs, 2)))
f_aug = augmented_f(icnf, mode, size(xs, 2), ys; rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(xs, zrs), icnf.tspan, p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - n_aug - 1), :]
Δlogp = fsol[(end - n_aug), :]
logp̂x = logpdf(icnf.basedist, z) - Δlogp
logp̂x, eachrow(fsol[(end - n_aug + 1):end, :])...
end

function generate(
icnf::AbstractCondICNF{T, AT},
mode::Mode,
ys::AbstractMatrix,
n::Integer,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::AbstractMatrix where {T <: AbstractFloat, AT <: AbstractArray}
n_aug = n_augment(icnf, mode)
new_xs = convert(AT, rand(rng, icnf.basedist, n))
zrs = convert(AT, zeros(T, n_aug+1, size(new_xs, 2)))
f_aug = augmented_f(icnf, mode, size(new_xs, 2), ys; rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(new_xs, zrs), reverse(icnf.tspan), p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - n_aug - 1), :]
z
end

function loss(
icnf::AbstractCondICNF{T, AT},
xs::AbstractMatrix,
ys::AbstractMatrix,
p::AbstractVector = icnf.p;
agg::Function = mean,
rng::AbstractRNG = Random.default_rng(),
)::Real where {T <: AbstractFloat, AT <: AbstractArray}
logp̂x, = inference(icnf, TrainMode(), xs, ys, p; rng)
agg(-logp̂x)
end

function n_augment(
icnf::AbstractCondICNF{T, AT},
mode::Mode,
)::Integer where {T <: AbstractFloat, AT <: AbstractArray}
0
end

# pretty-printing
function Base.show(io::IO, icnf::AbstractCondICNF)
print(
io,
typeof(icnf),
"\n\tNumber of Variables: ",
icnf.nvars,
"\n\tTime Span: ",
icnf.tspan,
)
end
74 changes: 74 additions & 0 deletions src/base_icnf.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
export inference, generate, loss

function inference(
icnf::AbstractICNF{T, AT},
mode::Mode,
xs::AbstractMatrix,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::Tuple where {T <: AbstractFloat, AT <: AbstractArray}
n_aug = n_augment(icnf, mode)
zrs = convert(AT, zeros(T, n_aug+1, size(xs, 2)))
f_aug = augmented_f(icnf, mode, size(xs, 2); rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(xs, zrs), icnf.tspan, p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - n_aug - 1), :]
Δlogp = fsol[(end - n_aug), :]
logp̂x = logpdf(icnf.basedist, z) - Δlogp
logp̂x, eachrow(fsol[(end - n_aug + 1):end, :])...
end

function generate(
icnf::AbstractICNF{T, AT},
mode::Mode,
n::Integer,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::AbstractMatrix where {T <: AbstractFloat, AT <: AbstractArray}
n_aug = n_augment(icnf, mode)
new_xs = convert(AT, rand(rng, icnf.basedist, n))
zrs = convert(AT, zeros(T, n_aug+1, size(new_xs, 2)))
f_aug = augmented_f(icnf, mode, size(new_xs, 2); rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(new_xs, zrs), reverse(icnf.tspan), p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - n_aug - 1), :]
z
end

function loss(
icnf::AbstractICNF{T, AT},
xs::AbstractMatrix,
p::AbstractVector = icnf.p;
agg::Function = mean,
rng::AbstractRNG = Random.default_rng(),
)::Real where {T <: AbstractFloat, AT <: AbstractArray}
logp̂x, = inference(icnf, TrainMode(), xs, p; rng)
agg(-logp̂x)
end

function n_augment(
icnf::AbstractICNF{T, AT},
mode::Mode,
)::Integer where {T <: AbstractFloat, AT <: AbstractArray}
0
end

# pretty-printing
function Base.show(io::IO, icnf::AbstractICNF)
print(
io,
typeof(icnf),
"\n\tNumber of Variables: ",
icnf.nvars,
"\n\tTime Span: ",
icnf.tspan,
)
end
98 changes: 0 additions & 98 deletions src/cond_ffjord.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,102 +75,4 @@ function augmented_f(
f_aug
end

function inference(
icnf::CondFFJORD{T, AT},
mode::TestMode,
xs::AbstractMatrix,
ys::AbstractMatrix,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::AbstractVector where {T <: AbstractFloat, AT <: AbstractArray}
zrs = convert(AT, zeros(T, 1, size(xs, 2)))
f_aug = augmented_f(icnf, mode, size(xs, 2), ys; rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(xs, zrs), icnf.tspan, p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - 1), :]
Δlogp = fsol[end, :]
logp̂x = logpdf(icnf.basedist, z) - Δlogp
logp̂x
end

function inference(
icnf::CondFFJORD{T, AT},
mode::TrainMode,
xs::AbstractMatrix,
ys::AbstractMatrix,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::AbstractVector where {T <: AbstractFloat, AT <: AbstractArray}
zrs = convert(AT, zeros(T, 1, size(xs, 2)))
f_aug = augmented_f(icnf, mode, size(xs, 2), ys; rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(xs, zrs), icnf.tspan, p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - 1), :]
Δlogp = fsol[end, :]
logp̂x = logpdf(icnf.basedist, z) - Δlogp
logp̂x
end

function generate(
icnf::CondFFJORD{T, AT},
mode::TestMode,
ys::AbstractMatrix,
n::Integer,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::AbstractMatrix{T} where {T <: AbstractFloat, AT <: AbstractArray}
new_xs = convert(AT, rand(rng, icnf.basedist, n))
zrs = convert(AT, zeros(T, 1, size(new_xs, 2)))
f_aug = augmented_f(icnf, mode, size(new_xs, 2), ys; rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(new_xs, zrs), reverse(icnf.tspan), p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - 1), :]
z
end

function generate(
icnf::CondFFJORD{T, AT},
mode::TrainMode,
ys::AbstractMatrix,
n::Integer,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::AbstractMatrix{T} where {T <: AbstractFloat, AT <: AbstractArray}
new_xs = convert(AT, rand(rng, icnf.basedist, n))
zrs = convert(AT, zeros(T, 1, size(new_xs, 2)))
f_aug = augmented_f(icnf, mode, size(new_xs, 2), ys; rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(new_xs, zrs), reverse(icnf.tspan), p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - 1), :]
z
end

@functor CondFFJORD (p,)

function loss(
icnf::CondFFJORD{T, AT},
xs::AbstractMatrix,
ys::AbstractMatrix,
p::AbstractVector = icnf.p;
agg::Function = mean,
rng::AbstractRNG = Random.default_rng(),
)::Real where {T <: AbstractFloat, AT <: AbstractArray}
logp̂x = inference(icnf, TrainMode(), xs, ys, p; rng)
agg(-logp̂x)
end
98 changes: 0 additions & 98 deletions src/cond_planar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,102 +75,4 @@ function augmented_f(
f_aug
end

function inference(
icnf::CondPlanar{T, AT},
mode::TestMode,
xs::AbstractMatrix,
ys::AbstractMatrix,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::AbstractVector where {T <: AbstractFloat, AT <: AbstractArray}
zrs = convert(AT, zeros(T, 1, size(xs, 2)))
f_aug = augmented_f(icnf, mode, size(xs, 2), ys; rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(xs, zrs), icnf.tspan, p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - 1), :]
Δlogp = fsol[end, :]
logp̂x = logpdf(icnf.basedist, z) - Δlogp
logp̂x
end

function inference(
icnf::CondPlanar{T, AT},
mode::TrainMode,
xs::AbstractMatrix,
ys::AbstractMatrix,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::AbstractVector where {T <: AbstractFloat, AT <: AbstractArray}
zrs = convert(AT, zeros(T, 1, size(xs, 2)))
f_aug = augmented_f(icnf, mode, size(xs, 2), ys; rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(xs, zrs), icnf.tspan, p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - 1), :]
Δlogp = fsol[end, :]
logp̂x = logpdf(icnf.basedist, z) - Δlogp
logp̂x
end

function generate(
icnf::CondPlanar{T, AT},
mode::TestMode,
ys::AbstractMatrix,
n::Integer,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::AbstractMatrix{T} where {T <: AbstractFloat, AT <: AbstractArray}
new_xs = convert(AT, rand(rng, icnf.basedist, n))
zrs = convert(AT, zeros(T, 1, size(new_xs, 2)))
f_aug = augmented_f(icnf, mode, size(new_xs, 2), ys; rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(new_xs, zrs), reverse(icnf.tspan), p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - 1), :]
z
end

function generate(
icnf::CondPlanar{T, AT},
mode::TrainMode,
ys::AbstractMatrix,
n::Integer,
p::AbstractVector = icnf.p,
args...;
rng::AbstractRNG = Random.default_rng(),
kwargs...,
)::AbstractMatrix{T} where {T <: AbstractFloat, AT <: AbstractArray}
new_xs = convert(AT, rand(rng, icnf.basedist, n))
zrs = convert(AT, zeros(T, 1, size(new_xs, 2)))
f_aug = augmented_f(icnf, mode, size(new_xs, 2), ys; rng)
func = ODEFunction(f_aug)
prob = ODEProblem(func, vcat(new_xs, zrs), reverse(icnf.tspan), p, args...; kwargs...)
sol = solve(prob)
fsol = sol[:, :, end]
z = fsol[1:(end - 1), :]
z
end

@functor CondPlanar (p,)

function loss(
icnf::CondPlanar{T, AT},
xs::AbstractMatrix,
ys::AbstractMatrix,
p::AbstractVector = icnf.p;
agg::Function = mean,
rng::AbstractRNG = Random.default_rng(),
)::Real where {T <: AbstractFloat, AT <: AbstractArray}
logp̂x = inference(icnf, TrainMode(), xs, ys, p; rng)
agg(-logp̂x)
end
Loading