Class GpuPINNTrainer<T>
- Namespace
- AiDotNet.PhysicsInformed
- Assembly
- AiDotNet.dll
Provides GPU-accelerated training for Physics-Informed Neural Networks.
public class GpuPINNTrainer<T>
Type Parameters
TThe numeric type used for calculations.
- Inheritance
-
GpuPINNTrainer<T>
- Inherited Members
Remarks
For Beginners: This trainer provides GPU-accelerated training methods for PINNs. It can significantly speed up training by:
- Processing large batches of collocation points in parallel
- Performing matrix operations on the GPU
- Using asynchronous data transfers to overlap computation
Usage:
var trainer = new GpuPINNTrainer<double>(myPinn);
var history = trainer.Train(epochs: 10000, options: GpuPINNTrainingOptions.Default);
The trainer automatically falls back to CPU if GPU is not available.
Constructors
GpuPINNTrainer(PhysicsInformedNeuralNetwork<T>, GpuPINNTrainingOptions?)
Initializes a new instance of the GPU PINN trainer.
public GpuPINNTrainer(PhysicsInformedNeuralNetwork<T> pinn, GpuPINNTrainingOptions? options = null)
Parameters
pinnPhysicsInformedNeuralNetwork<T>The Physics-Informed Neural Network to train.
optionsGpuPINNTrainingOptionsOptional GPU training options.
Properties
IsUsingGpu
Gets whether GPU is currently being used for training.
public bool IsUsingGpu { get; }
Property Value
Network
Gets the underlying PINN being trained.
public PhysicsInformedNeuralNetwork<T> Network { get; }
Property Value
Options
Gets the current training options.
public GpuPINNTrainingOptions Options { get; }
Property Value
Methods
EvaluateLoss(Tensor<T>, Tensor<T>?)
Performs a forward pass and computes loss (does not update weights).
public T EvaluateLoss(Tensor<T> batchInputs, Tensor<T>? batchTargets = null)
Parameters
batchInputsTensor<T>Batch of input points.
batchTargetsTensor<T>Optional target values. If provided, computes MSE loss against outputs.
Returns
- T
Loss value for this evaluation step.
Remarks
Note: This method only evaluates loss - it does not perform backpropagation or weight updates. Use the Train method for full training with gradient updates.
Exceptions
- ArgumentNullException
Thrown when batchInputs is null.
- ArgumentException
Thrown when batchTargets shape doesn't match outputs.
GetGpuMemoryInfo()
Gets GPU memory usage statistics.
public PINNGpuMemoryInfo? GetGpuMemoryInfo()
Returns
- PINNGpuMemoryInfo
Memory usage info or null if GPU not in use.
Remarks
Limitations: DirectGpu backends do not provide a standard API to query current memory usage. The returned TotalMemoryBytes reflects the total GPU memory, but UsedMemoryBytes and AvailableMemoryBytes cannot be accurately tracked through the DirectGpu backend.
For accurate memory profiling, use external tools like NVIDIA's nvidia-smi, CUDA's nvml library, or Visual Studio's GPU profiler.
ReleaseGpuResources()
Releases GPU resources.
public void ReleaseGpuResources()
Train(T[,]?, T[,]?, int, double, bool)
Trains the PINN with GPU acceleration.
public GpuTrainingHistory<T> Train(T[,]? dataInputs = null, T[,]? dataOutputs = null, int epochs = 10000, double learningRate = 0.001, bool verbose = true)
Parameters
dataInputsT[,]Optional measured input data.
dataOutputsT[,]Optional measured output data.
epochsintNumber of training epochs.
learningRatedoubleLearning rate for optimization.
verboseboolWhether to print progress.
Returns
- GpuTrainingHistory<T>
Training history with loss values per epoch.
TryInitializeGpu()
Attempts to initialize GPU resources.
public bool TryInitializeGpu()
Returns
- bool
True if GPU initialization was successful.
UpdateOptions(GpuPINNTrainingOptions)
Updates training options.
public void UpdateOptions(GpuPINNTrainingOptions options)
Parameters
optionsGpuPINNTrainingOptionsNew options to apply.