EasyHybrid Example: Synthetic Data Analysis
This example demonstrates how to use EasyHybrid to train a hybrid model on synthetic data for respiration modeling with Q10 temperature sensitivity.
julia
using EasyHybrid
using Metal
# using CUDA
using LuxData Loading and Preprocessing
Load synthetic dataset from GitHub into DataFrame
julia
df = load_timeseries_netcdf("https://github.com/bask0/q10hybrid/raw/master/data/Synthetic4BookChap.nc");Select a subset of data for faster execution
julia
df = df[1:5000, :];
first(df, 5)Define the Physical Model
RbQ10 model: Respiration model with Q10 temperature sensitivity
Parameters:
ta: air temperature [°C]
Q10: temperature sensitivity factor [-]
rb: basal respiration rate [μmol/m²/s]
tref: reference temperature [°C] (default: 15.0)
julia
function RbQ10(; ta, Q10, rb, tref = 15.0f0)
reco = rb .* Q10 .^ (0.1f0 .* (ta .- tref))
return (; reco, Q10, rb)
endDefine Model Parameters
Parameter specification: (default, lower_bound, upper_bound)
julia
parameters = (
# Parameter name | Default | Lower | Upper | Description
rb = (3.0f0, 0.0f0, 13.0f0), # Basal respiration [μmol/m²/s]
Q10 = (2.0f0, 1.0f0, 4.0f0), # Temperature sensitivity factor [-]
)Configure Hybrid Model Components
Define input variables
julia
forcing = [:ta] # Forcing variables (temperature)Target variable
julia
target = [:reco] # Target variable (respiration)Parameter classification
julia
global_param_names = [:Q10] # Global parameters (same for all samples)
neural_param_names = [:rb] # Neural network predicted parametersSingle NN Hybrid Model Training
julia
predictors_single_nn = [:sw_pot, :dsw_pot] # Predictor variables (solar radiation, and its derivative)
small_nn_hybrid_model = constructHybridModel(
predictors_single_nn, # Input features
forcing, # Forcing variables
target, # Target variables
RbQ10, # Process-based model function
parameters, # Parameter definitions
neural_param_names, # NN-predicted parameters
global_param_names, # Global parameters
hidden_layers = [16, 16], # Neural network architecture
activation = sigmoid, # Activation function
scale_nn_outputs = true, # Scale neural network outputs
input_batchnorm = true # Apply batch normalization to inputs
)
large_nn_hybrid_model = constructHybridModel(
predictors_single_nn, # Input features
forcing, # Forcing variables
target, # Target variables
RbQ10, # Process-based model function
parameters, # Parameter definitions
neural_param_names, # NN-predicted parameters
global_param_names, # Global parameters
hidden_layers = [1024, 512, 256, 128, 64], # Neural network architecture
activation = sigmoid, # Activation function
scale_nn_outputs = true, # Scale neural network outputs
input_batchnorm = true # Apply batch normalization to inputs
)train on DataFrame
Train the hybrid model
julia
cfg = EasyHybrid.TrainConfig(
nepochs = 3,
batchsize = 64,
opt = RMSProp(0.01),
loss_types = [:mse, :nse],
show_progress = false,
keep_history = false, # set to true to keep per-epoch history, losses, predictions, etc.
save_training = false, # Set to true to enable saving training history and checkpoints
)
using BenchmarkTools
gpu_small_nn() = tune(
small_nn_hybrid_model, df, cfg;
gdev = gpu_device(), model_name = "small_nn_gpu"
)
cpu_small_nn() = tune(
small_nn_hybrid_model, df, cfg;
gdev = cpu_device(), model_name = "small_nn_cpu"
)
gpu_large_nn() = tune(
large_nn_hybrid_model, df, cfg;
gdev = gpu_device(), model_name = "large_nn_gpu"
)
cpu_large_nn() = tune(
large_nn_hybrid_model, df, cfg;
gdev = cpu_device(), model_name = "large_nn_cpu"
)warm-up to pay compilation once
julia
gpu_small_nn();
cpu_small_nn();
gpu_large_nn();
cpu_large_nn();
nothing # hideWith Large NN CPU is slower than GPU
Large NN on GPU
julia
@benchmark gpu_large_nn() samples = 1 evals = 1Large NN on CPU
julia
@benchmark cpu_large_nn() samples = 1 evals = 1With Small NN CPU and GPU are on par
julia
using BenchmarkToolsSmall NN on GPU
julia
@benchmark gpu_small_nn() samples = 1 evals = 1Small NN on CPU
julia
@benchmark cpu_small_nn() samples = 1 evals = 1