Training¶
PDETrainer ¶
Trainer for Physics-Informed Neural Networks.
Initialize trainer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
PINN model. |
required |
pde
|
PDEBase
|
PDE instance. |
required |
config
|
Config
|
Full configuration object. Optimizer, scheduler, early
stopping and loss settings all come from |
required |
device
|
device | None
|
Device to train on. |
None
|
bandit_sampler
|
Optional |
None
|
Source code in pinnrl/training/trainer.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
setup_experiment_logging ¶
Log to <experiment_dir>/experiment.log, replacing earlier file handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_dir
|
Directory for the experiment; |
None
|
Source code in pinnrl/training/trainer.py
train_steps ¶
train_steps(
num_steps=None,
time_budget_s=None,
sample_fn=None,
resample_every=1,
checkpoints=(),
on_checkpoint=None,
)
Step-based training loop for benchmarks and studies.
Runs :meth:_step (the same update as :meth:train) until
num_steps steps or until training time reaches time_budget_s.
Training time is the summed wall-clock of sampling plus optimizer steps;
checkpoint callbacks are excluded. There is no validation, early
stopping, LR warm-up or scheduling, progress bar or disk I/O.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_steps
|
int | None
|
Stop after this many steps. |
None
|
time_budget_s
|
float | None
|
Stop once training time reaches this many seconds. |
None
|
sample_fn
|
Callable | None
|
|
None
|
resample_every
|
int
|
Draw new points every N steps and reuse them between. |
1
|
checkpoints
|
Sequence[float]
|
Increasing training-time marks (seconds). |
()
|
on_checkpoint
|
Callable | None
|
Called as |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
Source code in pinnrl/training/trainer.py
train ¶
Train the model.
Each epoch takes max(1, num_points // batch_size) optimizer steps on
freshly sampled batches (one full batch when optimizer is lbfgs). Every
validation_frequency epochs (starting at epoch 0) it validates,
logs, checks early stopping (patience counted in epochs) and, with
experiment_dir, saves history, metadata and the live snapshot.
Each call is planned as its own run: the learning rate restarts at
training.learning_rate and the schedule and the Adam -> L-BFGS
switch are sized to num_epochs. Weights, optimizer moments and
history carry over from earlier calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_epochs
|
int | None
|
Defaults to |
None
|
batch_size
|
int | None
|
Defaults to |
None
|
num_points
|
int | None
|
Collocation points per epoch; defaults to
|
None
|
experiment_dir
|
str | None
|
Run folder for |
None
|
progress
|
bool
|
Show one tqdm progress bar over epochs. |
True
|
Returns:
| Type | Description |
|---|---|
|
The training history dict ( |
Source code in pinnrl/training/trainer.py
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 | |
save_training_metrics ¶
Save training metrics to JSON files.
Writes the same history to metrics.json and history.json. When
metadata is given it is merged into an existing metadata.json and
stamped with last_updated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
history
|
dict[str, Any]
|
Dictionary containing training metrics |
required |
experiment_dir
|
str
|
Directory to save the metrics |
required |
metadata
|
dict[str, Any] | None
|
Optional metadata about the experiment |
None
|
Returns:
| Type | Description |
|---|---|
|
Path of |
Source code in pinnrl/training/trainer.py
AdaptiveLossWeights ¶
Adaptive weights for the [residual, boundary, initial] loss components.
The first :meth:update call returns initial_weights (or ones) and
seeds the running average; later calls return weights that sum to 1.
Initialize adaptive loss weights handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
str
|
Weight adaptation strategy ('lrw' or 'rbw'). |
'rbw'
|
alpha
|
float
|
EMA retention factor: |
0.9
|
eps
|
float
|
Small constant for numerical stability. |
1e-05
|
initial_weights
|
list | None
|
Weights for [residual, boundary, initial]
returned by the first update; |
None
|
Source code in pinnrl/training/adaptive_weights.py
update_weights_lrw ¶
Update weights using Learning Rate based Weighting (LRW).
After the first call (which only seeds g_bar)::
g_bar_i = alpha * g_bar_i + (1 - alpha) * g_i
w_i = (1 / (g_bar_i + eps)) / sum_{j active} (1 / (g_bar_j + eps))
over the active components (g_bar_i > 0), so components with smaller
gradient norms get larger weights. A component whose gradient norm is zero
(a constant loss, e.g. a PDE with no boundary conditions) gets weight 0:
it cannot be minimised, and 1 / eps would otherwise hand it almost all
the weight and switch the other terms off. If no component is active the
weights are uniform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gradients
|
Tensor
|
Gradient norm of each loss component, shape |
required |
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: The weights, shape |
Source code in pinnrl/training/adaptive_weights.py
update_weights_rbw ¶
Update weights using Relative Error based Weighting (RBW).
Gives more weight to terms with bigger losses. After the first call
(which only seeds L_bar)::
L_bar_i = alpha * L_bar_i + (1 - alpha) * L_i
w_i = alpha * w_prev_i + (1 - alpha) * L_bar_i / (sum_j L_bar_j + eps)
where w_prev is the previous RBW result; on the second call there is
none yet and w_i is the normalised term alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
losses
|
Tensor
|
Detached value of each loss component, shape |
required |
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: The weights, shape |
Source code in pinnrl/training/adaptive_weights.py
update ¶
Update weights based on chosen strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
losses
|
Tensor | None
|
Loss component values (required for |
None
|
gradients
|
Tensor | None
|
Gradient norm per component (required for |
None
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Updated loss weights. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input the strategy needs is missing, or the strategy is unknown. |
Source code in pinnrl/training/adaptive_weights.py
build_model ¶
Build the PINNModel described by a build_config_dict result.
Returns:
| Type | Description |
|---|---|
|
|
|
|
class: |
Source code in pinnrl/training/train.py
load_run ¶
Rebuild the PDE and trained model of a finished run.
Reads the run's config.yaml snapshot and final_model.pt and goes
through the same builders as :func:run_training, so the result matches
what was trained. The PDE is the forward problem at the configured (true)
parameters, so its exact solution is the reference even for inverse runs;
observation data is not reloaded (a Well run's snapshot records the time
window its observations covered).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_dir
|
Run folder written by :func: |
required | |
device
|
Torch device; defaults to CPU. |
None
|
Returns:
| Type | Description |
|---|---|
|
|