-
-
Notifications
You must be signed in to change notification settings - Fork 86
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
PINNErrorVsTime Benchmark Updates #1159
base: master
Are you sure you want to change the base?
Conversation
@ChrisRackauckas I got an error on running the iterations which said that the maxiters are less than 1000 so I set all maxiters to 1100. Actually the decision was a bit arbitrary but is that a good number ?? |
https://docs.sciml.ai/SciMLBenchmarksOutput/v0.5/PINNErrorsVsTime/diffusion_et/ It's supposed to just show the error over time and then get cutoff. I don't see why making it longer would help. |
Yes. Actually I set it to that number just to get rid of that error |
Wait what's the error? |
the error went off on running again |
what error? |
maxiters should be a number greater than 1000 |
can you please just show the error... |
|
I see, that's for the sampling algorithm. You should only need that on Cuhre? |
Yes. But as Cuhre was the first one in the line I thought setting to 1100 just for it would not solve the problem, so I set it to 1100 for all of them |
The CI has passed here. And all the code seems to run perfectly. Can you please review ?? |
@ArnoStrouwen SciML/Integrals.jl#124 can you remind me what the purpose behind this was? |
I don't remember myself, but that PR links to: |
Uninitialized memory in the original C: giordano/Cuba.jl#12 (comment) fantastic stuff numerical community, that's your classic method that everyone says when they say "all of the old stuff is robust" 😅 |
Can you force latest majors and make sure the manifest resolves? |
I bump forced the latest versions and resolved manifests but initially there were a lot of version conflicts. I removed IntegralsCuba and IntegralsCubature for a while to resolve them. The manifest resolved but adding both of them back again poses some more version conflicts |
Can you share the resolution errors? |
@ChrisRackauckas These are the resolution errors that occur |
Oh those were turned into extensions. Change |
Sure !! 🫡 |
|
yes |
@ChrisRackauckas I got a few questions to ask for the diffusion_et.jmd and hamilton_jacobi.jmd in diffusion_et.jmd on running the code, I get this error Where this error comes from, it didn't occur before changing it to Cuba and Cubature |
What is safr_get_device? There is get_device from MLDataDevices which is implemented for all backend including CPU |
It's from you in NeuralPDE: |
Is UnknownDevice new? I don't think I've seen that. |
Oh that one is a stopgap solution for some of the Any issues that were coming up inside NeuralPDE
generally this doesn't get exposed to the user. You can get it if you call |
So, What changes do we need here ?? or is it an issue needed to be addressed in some other repository ?? |
|
I think these are the only 2 errors remaining here |
@ChrisRackauckas any suggested fix for the MLDevices error here ?? It's common in all files and probably coming from NeuralPDE.jl |
I don't know the solution, I'm not sure why MLDevices is involved at all since it's just on the CPU. |
Yeah. I can understand. But just wanted to know if there's some other alternative to this, or what can be done if we didn't figure what's happening |
Make an MWE without the highest level package |
@ChrisRackauckas I implemented an MWE using all the dependencies in codes here except the highest level package. Here's the code: using Integrals, Cubature, Cuba
using ModelingToolkit, Optimization, OptimizationOptimJL
using Lux, Plots
using DelimitedFiles
using OptimizationOptimisers
using QuasiMonteCarlo
import ModelingToolkit: Interval, infimum, supremum
# Create a sample problem
rng = Xoshiro(0)
dim = 2
model = Lux.Chain(
Lux.Dense(dim, 16, relu),
Lux.Dense(16, 8, relu),
Lux.Dense(8, 1)
)
ps, st = Lux.setup(rng, model)
# Objective function to minimize
function objective(x, p)
sum(x.^2)
end
# Bounds for optimization
lower_bounds = [-2.0, -2.0]
upper_bounds = [2.0, 2.0]
# Quasi-Monte Carlo sampling
num_samples = 100
sample_points = QuasiMonteCarlo.sample(num_samples, lower_bounds, upper_bounds, LatticeRuleSample())
# Set up optimization problem
prob = OptimizationProblem(
OptimizationFunction(objective, Optimization.AutoForwardDiff()),
zeros(dim),
ps;
lb = lower_bounds,
ub = upper_bounds
)
# Solve using different optimizers
sol1 = solve(prob, NelderMead())
sol2 = solve(prob, BFGS())
# Demonstration of integration
function test_integral(x)
sin(x[1]) * cos(x[2])
end
# Use different integration methods
int2, = hcubature(test_integral, lower_bounds, upper_bounds)
# Plotting results
p1 = scatter(
sample_points[1,:],
sample_points[2,:],
title="Quasi-Monte Carlo Samples",
xlabel="X1",
ylabel="X2"
)
p2 = plot(
[sol1.minimizer[1]],
[sol1.minimizer[2]],
seriestype = :scatter,
title="Optimization Results",
label="Nelder-Mead"
)
plot!(p2, [sol2.minimizer[1]], [sol2.minimizer[2]],
seriestype = :scatter,
label="BFGS")
# Save results
writedlm("optimization_results.csv",
[sol1.minimizer sol2.minimizer],
',')
println("Nelder-Mead Solution: ", sol1.minimizer)
println("BFGS Solution: ", sol2.minimizer)
println("Integral (hcubature): ", int2) This code does:
|
|
Well that still has neuralpde. Next is to make the MWE without NeuralPDE |
Should I rewrite all the benchmark codes without using NeuralPDE, you mean by this ?? |
The MWE. Find out what the bug is. |
using Integrals, Cubature, Cuba
using ModelingToolkit, Optimization, OptimizationOptimJL
using Lux, Plots
using DelimitedFiles
using Random
using OptimizationOptimisers
using QuasiMonteCarlo
import ModelingToolkit: Interval, infimum, supremum
# Create a sample problem
rng = Xoshiro(0)
dim = 2
model = Lux.Chain(
Lux.Dense(dim, 16, relu),
Lux.Dense(16, 8, relu),
Lux.Dense(8, 1)
)
ps, st = Lux.setup(rng, model)
# Objective function to minimize
function objective(x, p)
sum(x.^2)
end
# Bounds for optimization
lower_bounds = [-2.0, -2.0]
upper_bounds = [2.0, 2.0]
# Quasi-Monte Carlo sampling
num_samples = 100
sample_points = QuasiMonteCarlo.sample(num_samples, lower_bounds, upper_bounds, LatticeRuleSample())
# Set up optimization problem
prob = OptimizationProblem(
OptimizationFunction(objective, Optimization.AutoForwardDiff()),
zeros(dim),
ps;
lb = lower_bounds,
ub = upper_bounds
)
# Solve using different optimizers
sol1 = solve(prob, NelderMead())
sol2 = solve(prob, BFGS())
# Demonstration of integration
function test_integral(x)
sin(x[1]) * cos(x[2])
end
# Use different integration methods
int2, = hcubature(test_integral, lower_bounds, upper_bounds)
# Plotting results
p1 = scatter(
sample_points[1,:],
sample_points[2,:],
title="Quasi-Monte Carlo Samples",
xlabel="X1",
ylabel="X2"
)
p2 = plot(
[sol1.minimizer[1]],
[sol1.minimizer[2]],
seriestype = :scatter,
title="Optimization Results",
label="Nelder-Mead"
)
plot!(p2, [sol2.minimizer[1]], [sol2.minimizer[2]],
seriestype = :scatter,
label="BFGS")
# Save results
writedlm("optimization_results.csv",
[sol1.minimizer sol2.minimizer],
',')
println("Nelder-Mead Solution: ", sol1.minimizer)
println("BFGS Solution: ", sol2.minimizer)
println("Integral (hcubature): ", int2) @ChrisRackauckas The code I previously sent had a bug where Xoshiro wasn't defined basically I wasn't using the Random Package in my code so I added it. Rest this works pretty fine here's the output |
Well is this is fine then it hasn't shown what the source of the bug is then... |
The source of the bug is the NeuralPDE package itself, I think because without using it everything seems to work without any error. I just removed everything related to NeuralPDE and it started working |
NeuralPDE.jl just makes a loss function. You can make a loss function with the same issue without NeuralPDE |
@ChrisRackauckas I made the following changes to the existing allen_cahn code and everything seems to work fine. Below is the code : function allen_cahn(strategy, minimizer, maxIters)
## DECLARATIONS
@parameters t x1 x2 x3 x4
@variables u(..)
Dt = Differential(t)
Dxx1 = Differential(x1)^2
Dxx2 = Differential(x2)^2
Dxx3 = Differential(x3)^2
Dxx4 = Differential(x4)^2
# Discretization
tmax = 1.0
x1width = x2width = x3width = x4width = 1.0
tMeshNum = x1MeshNum = x2MeshNum = x3MeshNum = x4MeshNum = 10
dt = tmax / tMeshNum
dx1 = x1width / x1MeshNum
dx2 = x2width / x2MeshNum
dx3 = x3width / x3MeshNum
dx4 = x4width / x4MeshNum
domains = [t ∈ Interval(0.0, tmax),
x1 ∈ Interval(0.0, x1width),
x2 ∈ Interval(0.0, x2width),
x3 ∈ Interval(0.0, x3width),
x4 ∈ Interval(0.0, x4width)]
# Define the coordinates
ts = 0.0:dt:tmax
x1s = 0.0:dx1:x1width
x2s = 0.0:dx2:x2width
x3s = 0.0:dx3:x3width
x4s = 0.0:dx4:x4width
# Operators
Δu = Dxx1(u(t,x1,x2,x3,x4)) + Dxx2(u(t,x1,x2,x3,x4)) +
Dxx3(u(t,x1,x2,x3,x4)) + Dxx4(u(t,x1,x2,x3,x4))
# Equation
eq = Dt(u(t,x1,x2,x3,x4)) - Δu - u(t,x1,x2,x3,x4) +
u(t,x1,x2,x3,x4)^3 ~ 0
# Initial condition
initialCondition = 1 / (2 + 0.4 * (x1^2 + x2^2 + x3^2 + x4^2))
bcs = [u(0,x1,x2,x3,x4) ~ initialCondition]
# Neural Network setup
n = 10
# Create the neural network with simplified architecture
chain = Lux.Chain(
Lux.Dense(5 => n, tanh),
Lux.Dense(n => n, tanh),
Lux.Dense(n => 1, identity)
)
# Modify training strategy based on type
if strategy isa QuadratureTraining
if strategy.quadrature_alg isa CubaCuhre
# Use modified settings for CubaCuhre
strategy = NeuralPDE.QuadratureTraining(
quadrature_alg = CubaCuhre(),
reltol = 1e-4,
abstol = 1e-4,
maxiters = 1100,
batch = 100 # Enable batch processing
)
elseif strategy.quadrature_alg isa HCubatureJL
# Switch to StochasticTraining for high dimensions
@warn "Switching to StochasticTraining for better performance in high dimensions"
strategy = NeuralPDE.StochasticTraining(
400,
bcs_points = 50
)
end
end
# Create the PINN with modified discretization
discretization = PhysicsInformedNN(
chain,
strategy;
init_params = nothing
)
# Setup PDE system with explicit variable ordering
@named pde_system = PDESystem(
eq,
bcs,
domains,
[t,x1,x2,x3,x4],
[u(t,x1,x2,x3,x4)]
)
# Discretize with fallback options
prob = try
discretize(pde_system, discretization)
catch e
@warn "Initial discretization failed, trying StochasticTraining"
# Fall back to StochasticTraining
strategy = NeuralPDE.StochasticTraining(
400,
bcs_points = 50
)
discretization = PhysicsInformedNN(chain, strategy)
discretize(pde_system, discretization)
end
# Training setup
losses = Float64[]
error = Float64[]
times = Float64[]
timeCounter = 0.0
startTime = time_ns()
# Callback function with error handling
function cb(p, l)
try
deltaT_s = time_ns()
ctime = time_ns() - startTime - timeCounter
push!(times, ctime / 1e9)
push!(losses, l)
push!(error, l)
timeCounter += time_ns() - deltaT_s
return false
catch e
@warn "Callback error: $e"
return false
end
end
# Solve with modified optimization parameters
res = Optimization.solve(
prob,
minimizer;
callback = cb,
maxiters = maxIters
)
# Generate predictions
phi = discretization.phi
domain = [ts, x1s, x2s, x3s, x4s]
u_predict = try
[reshape([first(phi([t,x1,x2,x3,x4], res.minimizer))
for x1 in x1s, x2 in x2s, x3 in x3s, x4 in x4s],
(length(x1s), length(x2s), length(x3s), length(x4s)))
for t in ts]
catch e
@warn "Prediction generation failed: $e"
nothing
end
return [error, res.minimizer, domain, times, losses]
end |
what's the change? |
|
Checklist
contributor guidelines, in particular the SciML Style Guide and
COLPRAC.
Additional context
Add any other context about the problem here.