https://github.com/impicnf/continuousnormalizingflows.jl
Implementations of Infinitesimal Continuous Normalizing Flows Algorithms in Julia
https://github.com/impicnf/continuousnormalizingflows.jl
continuous-normalizing-flows flows infinitesimal-continuous-normalizing-flows infinitesimal-normalizing-flows julia julia-language julialang normalizing-flows
Last synced: 2 months ago
JSON representation
Implementations of Infinitesimal Continuous Normalizing Flows Algorithms in Julia
- Host: GitHub
- URL: https://github.com/impicnf/continuousnormalizingflows.jl
- Owner: impICNF
- License: mit
- Created: 2021-11-10T14:53:12.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-05-04T01:23:39.000Z (2 months ago)
- Last Synced: 2025-05-07T04:02:11.001Z (2 months ago)
- Topics: continuous-normalizing-flows, flows, infinitesimal-continuous-normalizing-flows, infinitesimal-normalizing-flows, julia, julia-language, julialang, normalizing-flows
- Language: Julia
- Homepage: https://impicnf.github.io/ContinuousNormalizingFlows.jl/
- Size: 1.91 MB
- Stars: 27
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Citation: CITATION.bib
Awesome Lists containing this project
README
# ContinuousNormalizingFlows.jl
[](https://impICNF.github.io/ContinuousNormalizingFlows.jl/stable)
[](https://impICNF.github.io/ContinuousNormalizingFlows.jl/dev)
[](https://juliahub.com/ui/Packages/General/ContinuousNormalizingFlows)
[](https://juliahub.com/ui/Packages/General/ContinuousNormalizingFlows?t=2)
[](https://github.com/impICNF/ContinuousNormalizingFlows.jl/actions/workflows/CI.yml?query=branch%3Amain)
[](https://codecov.io/gh/impICNF/ContinuousNormalizingFlows.jl)
[](https://coveralls.io/github/impICNF/ContinuousNormalizingFlows.jl?branch=main)
[](https://juliahub.com/ui/Packages/General/ContinuousNormalizingFlows)
[](https://JuliaCI.github.io/NanosoldierReports/pkgeval_badges/C/ContinuousNormalizingFlows.html)
[](https://juliapkgstats.com/pkg/ContinuousNormalizingFlows)
[](https://juliapkgstats.com/pkg/ContinuousNormalizingFlows)
[](https://github.com/JuliaTesting/Aqua.jl)
[](https://github.com/aviatesk/JET.jl)
[](https://github.com/SciML/ColPrac)Implementations of Infinitesimal Continuous Normalizing Flows Algorithms in Julia
## Citing
See [`CITATION.bib`](CITATION.bib) for the relevant reference(s).
## Installation
```julia
using Pkg
Pkg.add("ContinuousNormalizingFlows")
```## Usage
```julia
# Enable Logging
using Logging, TerminalLoggers
global_logger(TerminalLogger())# Parameters
nvars = 1
naugs = nvars
# n_in = nvars # without augmentation
n_in = nvars + naugs # with augmentation
n = 1024# Model
using ContinuousNormalizingFlows, Lux #, ADTypes, OrdinaryDiffEqDefault, Zygote, CUDA, MLDataDevices
nn = Chain(Dense(n_in => 3 * n_in, tanh), Dense(3 * n_in => n_in, tanh))
icnf = construct(
RNODE,
nn,
nvars, # number of variables
naugs; # number of augmented dimensions
# compute_mode = LuxVecJacMatrixMode(AutoZygote()), # process data in batches and use Zygote
# inplace = true, # use the inplace version of functions
# device = gpu_device(), # process data by GPU
tspan = (0.0f0, 13.0f0), # have bigger time span
steer_rate = 1.0f-1, # add random noise to end of the time span
λ₁ = 1.0f-2, # regulate flow
λ₂ = 1.0f-2, # regulate volume change
λ₃ = 1.0f-2, # regulate augmented dimensions
# sol_kwargs = (;
# progress = true,
# save_everystep = false,
# reltol = sqrt(eps(one(Float32))),
# abstol = eps(one(Float32)),
# maxiters = typemax(Int32),
# alg = DefaultODEAlgorithm(),
# ), # pass to the solver
)# Data
using Distributions
data_dist = Beta{Float32}(2.0f0, 4.0f0)
r = rand(data_dist, nvars, n)
r = convert.(Float32, r)# Fit It
using DataFrames, MLJBase #, Zygote, ADTypes, OptimizationOptimisers
df = DataFrame(transpose(r), :auto)
model = ICNFModel(
icnf;
# optimizers = (Lion(),),
# n_epochs = 300,
# adtype = AutoZygote(),
# use_batch = true,
# batch_size = 32,
# sol_kwargs = (; progress = true,), # pass to the solver
)
mach = machine(model, df)
fit!(mach)
ps, st = fitted_params(mach)# Store It
using JLD2, UnPack
jldsave("fitted.jld2"; ps, st) # save
@unpack ps, st = load("fitted.jld2") # load# Use It
d = ICNFDist(icnf, TestMode(), ps, st) # direct way
# d = ICNFDist(mach, TestMode()) # alternative way
actual_pdf = pdf.(data_dist, vec(r))
estimated_pdf = pdf(d, r)
new_data = rand(d, n)# Evaluate It
using Distances
mad_ = meanad(estimated_pdf, actual_pdf)
msd_ = msd(estimated_pdf, actual_pdf)
tv_dis = totalvariation(estimated_pdf, actual_pdf) / n
res_df = DataFrame(; mad_, msd_, tv_dis)
display(res_df)# Plot It
using CairoMakie
f = Figure()
ax = Makie.Axis(f[1, 1]; title = "Result")
lines!(ax, 0.0f0 .. 1.0f0, x -> pdf(data_dist, x); label = "actual")
lines!(ax, 0.0f0 .. 1.0f0, x -> pdf(d, vcat(x)); label = "estimated")
axislegend(ax)
save("result-fig.svg", f)
save("result-fig.png", f)
```