Skip to content

Commit 6841af9

Browse files
authored
Fix big gradient error (#528)
* remove `ClipNorm` * switch to `Float32` and loose `abstol` * drop old compats * fix * fix 2
1 parent 5b1c6cd commit 6841af9

10 files changed

Lines changed: 27 additions & 39 deletions

File tree

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ MLUtils = "0.4"
5151
NNlib = "0.9"
5252
Optimisers = "0.4"
5353
OptimizationOptimisers = "0.3"
54-
OrdinaryDiffEqAdamsBashforthMoulton = "1, 2"
54+
OrdinaryDiffEqAdamsBashforthMoulton = "2"
5555
Random = "1"
56-
SciMLBase = "2, 3"
57-
SciMLLogging = "1, 2"
56+
SciMLBase = "3"
57+
SciMLLogging = "2"
5858
SciMLSensitivity = "7"
5959
ScientificTypesBase = "3"
6060
Statistics = "1"

benchmark/benchmarks.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import ADTypes,
1010

1111
ndata = 2^10
1212
ndimensions = 1
13-
data_dist = Distributions.Beta(2.0, 4.0)
13+
data_dist = Distributions.Beta(2.0f0, 4.0f0)
1414
r = rand(data_dist, ndimensions, ndata)
1515

1616
nvariables = size(r, 1)

examples/usage.jl

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ global_logger(TerminalLogger())
66
using Distributions
77
ndata = 1024
88
ndimensions = 1
9-
data_dist = Beta(2.0, 4.0)
9+
data_dist = Beta(2.0f0, 4.0f0)
1010
r = rand(data_dist, ndimensions, ndata)
1111

1212
## Parameters
@@ -37,11 +37,11 @@ icnf = ICNF(;
3737
nvariables = nvariables, # number of variables
3838
naugments = naugments, # number of augmented dimensions
3939
nconditions = 0, # number of conditioning inputs
40-
λ₁ = 0.01, # regulate flow
41-
λ₂ = 0.01, # regulate volume change
42-
λ₃ = 0.01, # regulate augmented dimensions
43-
steer_rate = 0.1, # add random noise to end of the time span
44-
tspan = (0.0, 1.0), # time span
40+
λ₁ = 0.01f0, # regulate flow
41+
λ₂ = 0.01f0, # regulate volume change
42+
λ₃ = 0.01f0, # regulate augmented dimensions
43+
steer_rate = 0.1f0, # add random noise to end of the time span
44+
tspan = (0.0f0, 1.0f0), # time span
4545
device = cpu_device(), # process data by CPU
4646
# device = gpu_device(), # process data by GPU
4747
autonomous = false, # using non-autonomous flow
@@ -51,14 +51,14 @@ icnf = ICNF(;
5151
sol_kwargs = (;
5252
save_everystep = false,
5353
maxiters = typemax(Int),
54-
reltol = 1.0e-4,
55-
abstol = 1.0e-8,
54+
reltol = 1.0f-4,
55+
abstol = 1.0f-4,
5656
alg = VCABM(),
5757
sensealg = QuadratureAdjoint(;
5858
autodiff = true,
5959
autojacvec = ZygoteVJP(),
60-
reltol = 1.0e-4,
61-
abstol = 1.0e-8,
60+
reltol = 1.0f-4,
61+
abstol = 1.0f-4,
6262
),
6363
progress = false,
6464
verbose = Detailed(),
@@ -86,9 +86,8 @@ if !isfile(icnf_mach_fn)
8686
epochs = 300,
8787
callback = opt_callback,
8888
alg = OptimiserChain(
89-
WeightDecay(; lambda = 1.0e-4),
90-
ClipNorm(10.0, 2.0; throw = true),
91-
Adam(; eta = 0.001, beta = (0.9, 0.999), epsilon = 1.0e-8),
89+
WeightDecay(; lambda = 1.0f-4),
90+
Adam(; eta = 0.001f0, beta = (0.9f0, 0.999f0), epsilon = 1.0f-8),
9291
),
9392
progress = true,
9493
verbose = Detailed(),
@@ -120,8 +119,8 @@ display(res_df)
120119
using CairoMakie
121120
f = Figure()
122121
ax = Axis(f[1, 1]; title = "Result")
123-
lines!(ax, 0.0 .. 1.0, x -> pdf(data_dist, x); label = "Actual")
124-
lines!(ax, 0.0 .. 1.0, x -> pdf(d, vcat(x)); label = "Estimated")
122+
lines!(ax, 0.0f0 .. 1.0f0, x -> pdf(data_dist, x); label = "Actual")
123+
lines!(ax, 0.0f0 .. 1.0f0, x -> pdf(d, vcat(x)); label = "Estimated")
125124
axislegend(ax)
126125
save("result-figure.svg", f)
127126
save("result-figure.png", f)

src/core/icnf.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct ICNF{
5151
end
5252

5353
function ICNF(;
54-
data_type::Type{<:AbstractFloat} = Float64,
54+
data_type::Type{<:AbstractFloat} = Float32,
5555
compute_mode::ComputeMode = LuxVecJacMatrixMode(ADTypes.AutoZygote()),
5656
inplace::Bool = false,
5757
autonomous::Bool = false,
@@ -85,7 +85,7 @@ function ICNF(;
8585
save_everystep = false,
8686
maxiters = typemax(Int),
8787
reltol = convert(data_type, 1.0e-4),
88-
abstol = convert(data_type, 1.0e-8),
88+
abstol = convert(data_type, 1.0e-4),
8989
alg = OrdinaryDiffEqAdamsBashforthMoulton.VCABM(),
9090
sensealg = SciMLSensitivity.QuadratureAdjoint(;
9191
autodiff = true,
@@ -95,7 +95,7 @@ function ICNF(;
9595
true,
9696
),
9797
reltol = convert(data_type, 1.0e-4),
98-
abstol = convert(data_type, 1.0e-8),
98+
abstol = convert(data_type, 1.0e-4),
9999
),
100100
progress = false,
101101
verbose = SciMLLogging.Detailed(),

src/exts/mlj_ext/core_cond_icnf.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ function CondICNFModel(;
1616
callback = make_opt_callback(64),
1717
alg = Optimisers.OptimiserChain(
1818
Optimisers.WeightDecay(; lambda = convert(eltype(icnf), 1.0e-4)),
19-
Optimisers.ClipNorm(
20-
convert(eltype(icnf), 10.0),
21-
convert(eltype(icnf), 2.0);
22-
throw = true,
23-
),
2419
Optimisers.Adam(;
2520
eta = convert(eltype(icnf), 0.001),
2621
beta = (convert(eltype(icnf), 0.9), convert(eltype(icnf), 0.999)),

src/exts/mlj_ext/core_icnf.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ function ICNFModel(;
1616
callback = make_opt_callback(64),
1717
alg = Optimisers.OptimiserChain(
1818
Optimisers.WeightDecay(; lambda = convert(eltype(icnf), 1.0e-4)),
19-
Optimisers.ClipNorm(
20-
convert(eltype(icnf), 10.0),
21-
convert(eltype(icnf), 2.0);
22-
throw = true,
23-
),
2419
Optimisers.Adam(;
2520
eta = convert(eltype(icnf), 0.001),
2621
beta = (convert(eltype(icnf), 0.9), convert(eltype(icnf), 0.999)),

src/layers/planar_layer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function PlanarLayer(
1515
mapping::Pair{<:Int, <:Int},
1616
activation::Any = identity;
1717
init_weight::Any = WeightInitializers.glorot_uniform,
18-
init_bias::Any = WeightInitializers.zeros64,
18+
init_bias::Any = WeightInitializers.zeros32,
1919
use_bias::Bool = true,
2020
)
2121
return PlanarLayer{

test/ci_tests/regression_tests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Test.@testset verbose = true showtiming = true failfast = false "Regression Tests" begin
22
ndata = 2^10
33
ndimensions = 1
4-
data_dist = Distributions.Beta(2.0, 4.0)
4+
data_dist = Distributions.Beta(2.0f0, 4.0f0)
55
r = rand(data_dist, ndimensions, ndata)
66

77
nvariables = size(r, 1)

test/ci_tests/smoke_tests.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Test.@testset verbose = true showtiming = true failfast = false "Smoke Tests" be
99

1010
ndata = 4
1111
ndimensions = 2
12-
data_dist = Distributions.Beta(2.0, 4.0)
13-
data_dist2 = Distributions.Beta(2.0, 4.0)
12+
data_dist = Distributions.Beta(2.0f0, 4.0f0)
13+
data_dist2 = Distributions.Beta(2.0f0, 4.0f0)
1414
if compute_mode isa ContinuousNormalizingFlows.VectorMode
1515
r = rand(data_dist, ndimensions)
1616
r2 = rand(data_dist2, ndimensions)
@@ -124,7 +124,6 @@ Test.@testset verbose = true showtiming = true failfast = false "Smoke Tests" be
124124

125125
Test.@testset verbose = true showtiming = true failfast = false "$adtype on loss" for adtype in
126126
adtypes
127-
128127
Test.@test !isnothing(DifferentiationInterface.gradient(diff_loss, adtype, ps))
129128
Test.@test !isnothing(DifferentiationInterface.gradient(diff2_loss, adtype, r))
130129

test/quality_tests/checkby_JET_tests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Test.@testset verbose = true showtiming = true failfast = false "CheckByJET" beg
1313

1414
ndata = 4
1515
ndimensions = 2
16-
data_dist = Distributions.Beta(2.0, 4.0)
17-
data_dist2 = Distributions.Beta(2.0, 4.0)
16+
data_dist = Distributions.Beta(2.0f0, 4.0f0)
17+
data_dist2 = Distributions.Beta(2.0f0, 4.0f0)
1818
if compute_mode isa ContinuousNormalizingFlows.VectorMode
1919
r = rand(data_dist, ndimensions)
2020
r2 = rand(data_dist2, ndimensions)

0 commit comments

Comments
 (0)