11# Switch To MKL For Faster Computation
2- # using MKL
2+ using MKL
33
44# # Enable Logging
55using Logging, TerminalLoggers
@@ -20,47 +20,63 @@ n_in = nvars + naugs
2020
2121# # Model
2222using ContinuousNormalizingFlows,
23- Lux, OrdinaryDiffEqAdamsBashforthMoulton, ADTypes, Zygote, MLDataDevices
23+ Lux,
24+ OrdinaryDiffEqAdamsBashforthMoulton,
25+ Static,
26+ SciMLSensitivity,
27+ ADTypes,
28+ Zygote,
29+ MLDataDevices
2430
2531# To use gpu, add related packages
26- # using LuxCUDA, CUDA, cuDNN
32+ # using LuxCUDA
2733
28- nn = Chain (Dense (n_in => 3 * n_in, tanh), Dense (3 * n_in => n_in, tanh))
29- icnf = construct (
30- ICNF,
31- nn,
32- nvars, # number of variables
33- naugs; # number of augmented dimensions
34- compute_mode = LuxVecJacMatrixMode (AutoZygote ()), # process data in batches and use Zygote
35- inplace = false , # not using the inplace version of functions
36- device = cpu_device (), # process data by CPU
37- # device = gpu_device(), # process data by GPU
38- tspan = (0.0f0 , 1.0f0 ), # time span
39- steer_rate = 1.0f-1 , # add random noise to end of the time span
34+ nn = Chain (Dense (n_in => (2 * n_in + 1 ), tanh), Dense ((2 * n_in + 1 ) => n_in, tanh))
35+ icnf = ICNF (;
36+ nn = nn,
37+ nvars = nvars, # number of variables
38+ naugmented = naugs, # number of augmented dimensions
4039 λ₁ = 1.0f-2 , # regulate flow
4140 λ₂ = 1.0f-2 , # regulate volume change
4241 λ₃ = 1.0f-2 , # regulate augmented dimensions
43- sol_kwargs = (; save_everystep = false , alg = VCABM ()), # pass to the solver
42+ steer_rate = 1.0f-1 , # add random noise to end of the time span
43+ tspan = (0.0f0 , 1.0f0 ), # time span
44+ device = cpu_device (), # process data by CPU
45+ # device = gpu_device(), # process data by GPU
46+ cond = false , # not conditioning on auxiliary input
47+ inplace = false , # not using the inplace version of functions
48+ compute_mode = LuxVecJacMatrixMode (AutoZygote ()), # process data in batches and use Zygote
49+ sol_kwargs = (;
50+ save_everystep = false ,
51+ maxiters = typemax (Int),
52+ reltol = 1.0f-4 ,
53+ abstol = 1.0f-8 ,
54+ alg = VCABM (; thread = True ()),
55+ sensealg = InterpolatingAdjoint (; checkpointing = true , autodiff = true ),
56+ ), # pass to the solver
4457)
4558
4659# # Fit It
4760using DataFrames, MLJBase, Zygote, ADTypes, OptimizationOptimisers
48- df = DataFrame (transpose (r), :auto )
49- model = ICNFModel (
50- icnf;
51- optimizers = (Adam (),),
52- adtype = AutoZygote (),
53- batchsize = 512 ,
54- sol_kwargs = (; epochs = 300 , progress = true ), # pass to the solver
55- )
56- mach = machine (model, df)
57- fit! (mach)
58- # CUDA.@allowscalar fit!(mach) # needed for gpu
5961
60- # # Store It
6162icnf_mach_fn = " icnf_mach.jls"
62- MLJBase. save (icnf_mach_fn, mach) # save it
63- mach = machine (icnf_mach_fn) # load it
63+ if ispath (icnf_mach_fn)
64+ mach = machine (icnf_mach_fn) # load it
65+ else
66+ df = DataFrame (transpose (r), :auto )
67+ model = ICNFModel (;
68+ icnf,
69+ optimizers = (OptimiserChain (WeightDecay (), Adam ()),),
70+ batchsize = 1024 ,
71+ adtype = AutoZygote (),
72+ sol_kwargs = (; epochs = 300 , progress = true ), # pass to the solver
73+ )
74+ mach = machine (model, df)
75+ fit! (mach)
76+ # CUDA.@allowscalar fit!(mach) # needed for gpu
77+
78+ MLJBase. save (icnf_mach_fn, mach) # save it
79+ end
6480
6581# # Use It
6682d = ICNFDist (mach, TestMode ())
0 commit comments