Skip to content

Commit

Permalink
move example to a file (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
prbzrg authored Jan 13, 2023
1 parent 854dd5c commit 399d655
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
42 changes: 1 addition & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,4 @@ using Pkg
Pkg.add(url="https://github.com/impICNF/ICNF.jl")
```

To use this package, here is an example:

```julia
using ICNF
using Distributions, Flux
using DifferentialEquations, SciMLSensitivity

# Parameters
nvars = 1
n = 1024

# Data
data_dist = Beta(2.0, 4.0)
r = rand(data_dist, nvars, n)

# Model
nn = Chain(
Dense(nvars => 4*nvars, tanh),
Dense(4*nvars => nvars, tanh),
) |> f64
icnf = RNODE{Float64, Array}(nn, nvars; tspan=(0.0, 4.0))

# Training
using DataFrames, MLJBase
df = DataFrame(transpose(r), :auto)
model = ICNFModel(icnf; opt_app=SciMLOptApp())
mach = machine(model, df)
fit!(mach)

# Use It
d = ICNFDist(icnf)
actual_pdf = pdf.(data_dist, vec(r))
estimated_pdf = pdf(d, r)
new_data = rand(d, n)

# Evaluation
using LinearAlgebra, Distances
n1 = norm(estimated_pdf - actual_pdf, 1) / n
n2 = norm(estimated_pdf - actual_pdf, 2) / n
tv_dis = totalvariation(estimated_pdf, actual_pdf) / n
```
See [`simple_example.jl`](examples/simple_example.jl) for a simple example of using this package.
37 changes: 37 additions & 0 deletions examples/simple_example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using ICNF
using Distributions, Flux
using DifferentialEquations, SciMLSensitivity

# Parameters
nvars = 1
n = 1024

# Data
data_dist = Beta(2.0, 4.0)
r = rand(data_dist, nvars, n)

# Model
nn = Chain(
Dense(nvars => 4*nvars, tanh),
Dense(4*nvars => nvars, tanh),
) |> f64
icnf = RNODE{Float64, Array}(nn, nvars; tspan=(0.0, 4.0))

# Training
using DataFrames, MLJBase
df = DataFrame(transpose(r), :auto)
model = ICNFModel(icnf; opt_app=SciMLOptApp())
mach = machine(model, df)
fit!(mach)

# Use It
d = ICNFDist(icnf)
actual_pdf = pdf.(data_dist, vec(r))
estimated_pdf = pdf(d, r)
new_data = rand(d, n)

# Evaluation
using LinearAlgebra, Distances
n1 = norm(estimated_pdf - actual_pdf, 1) / n
n2 = norm(estimated_pdf - actual_pdf, 2) / n
tv_dis = totalvariation(estimated_pdf, actual_pdf) / n

0 comments on commit 399d655

Please sign in to comment.