### Import neurodiffeq modules and set up plotting Source: https://neurodiffeq.readthedocs.io/en/latest/advanced.html Imports required libraries (numpy, torch, neurodiffeq modules, matplotlib) and enables notebook plotting. This is the setup for all examples on the page. ```python import numpy as np import torch from neurodiffeq.neurodiffeq import safe_diff as diff from neurodiffeq.ode import solve, solve_system from neurodiffeq.solvers import Solver1D from neurodiffeq.monitors import Monitor1D from neurodiffeq.conditions import IVP import matplotlib.pyplot as plt %matplotlib notebook ``` -------------------------------- ### IBVP1D(x_min, x_max, t_min, t_min_val, x_min_val=None, x_min_prime=None, x_max_val=None, x_max_prime=None) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html An initial & boundary condition on a 1-D range where x in [x0, x1] and time starts at t0. The conditions should have the following parts: u(x,t0)=u0(x), u(x0,t)=g(t) or u'_x(x0,t)=p(t), u(x1,t)=h(t) or u'_x(x1,t)=q(t). ```APIDOC ## IBVP1D(x_min, x_max, t_min, t_min_val, x_min_val=None, x_min_prime=None, x_max_val=None, x_max_prime=None) ### Description An initial & boundary condition on a 1-D range where x in [x0, x1] and time starts at t0. The conditions should have the following parts: u(x,t0)=u0(x), u(x0,t)=g(t) or u'_x(x0,t)=p(t), u(x1,t)=h(t) or u'_x(x1,t)=q(t). ### Parameters - **x_min** (float) - Required - The lower bound of x, the x0. - **x_max** (float) - Required - The upper bound of x, the x1. - **t_min** (float) - Required - The initial time, the t0. - **t_min_val** (callable) - Required - The initial condition, the u0(x). - **x_min_val** (callable, optional) - Optional - The Dirichlet boundary condition when x=x0, the u(x0,t), defaults to None. - **x_min_prime** (callable, optional) - Optional - The Neumann boundary condition when x=x0, the u'_x(x0,t), defaults to None. - **x_max_val** (callable, optional) - Optional - The Dirichlet boundary condition when x=x1, the u(x1,t), defaults to None. - **x_max_prime** (callable, optional) - Optional - The Neumann boundary condition when x=x1, the u'_x(x1,t), defaults to None. ### Raises - **NotImplementedError** - When unimplemented boundary conditions are configured. ### Note This condition cannot be passed to neurodiffeq.conditions.EnsembleCondition unless both boundaries uses Dirichlet conditions (by specifying only x_min_val and x_max_val) and force is set to True in EnsembleCondition's constructor. ``` -------------------------------- ### get_solution(copy=True, best=True) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Gets a callable solution object that can be used to evaluate the solution at given points. The solution can be copied to prevent subsequent training from affecting it. ```APIDOC ## get_solution(copy=True, best=True) ### Description Get a (callable) solution object. See this usage example: ``` solution = solver.get_solution()point_coords = train_generator.get_examples()value_at_points = solution(point_coords) ``` ### Method get_solution ### Parameters - **copy** (bool) - Optional - Whether to make a copy of the networks so that subsequent training doesn't affect the solution; Defaults to True. - **best** (bool) - Optional - Whether to return the solution with lowest loss instead of the solution after the last epoch. Defaults to True. ### Returns - A solution object which can be called. To evaluate the solution on certain points, you should pass the coordinates vector(s) to the returned solution. ### Return Type BaseSolution ``` -------------------------------- ### get_examples() Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Returns the predefined training points. The points are fixed and predefined. ```APIDOC ## get_examples() ### Description Returns the training points. Points are fixed and predefined. ### Method Function ### Endpoint neurodiffeq.generators.get_examples ### Returns - tuple[torch.Tensor] - The predefined training points ### Return Type tuple[torch.Tensor] ``` -------------------------------- ### generator_2dspatial_segment(size, start, end, random=True) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Returns a generator that produces 2D points along a line segment defined by start and end points. The points are either generated randomly or equally spaced along the segment depending on the random flag. ```APIDOC ## generator_2dspatial_segment(size, start, end, random=True) ### Description Returns a generator that generates 2D points in a line segment. ### Parameters - **size** (int) - Required - Number of points to generate when __next__ is invoked. - **start** (tuple[float, float]) - Required - The starting point of the line segment. - **end** (tuple[float, float]) - Required - The ending point of the line segment. - **random** (bool) - Optional - If set to False, return equally spaced points ranging from start to end. If set to True, generate points randomly. Defaults to True. ### Returns A generator that yields 2D points along the line segment. ``` -------------------------------- ### BundleSolver1D(ode_system, conditions, t_min, t_max, theta_min=None, theta_max=None, eq_param_index=(), nets=None, train_generator=None, valid_generator=None, analytic_solutions=None, optimizer=None, loss_fn=None, n_batches_train=1, n_batches_valid=4, metrics=None, n_output_units=1, batch_size=None, shuffle=None) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html A solver class for solving ODEs (single-input differential equations) or a bundle of ODEs for different values of its parameters and/or conditions. It inherits from BaseSolver and provides the same methods as BaseSolver. ```APIDOC ## BundleSolver1D(ode_system, conditions, t_min, t_max, theta_min=None, theta_max=None, eq_param_index=(), nets=None, train_generator=None, valid_generator=None, analytic_solutions=None, optimizer=None, loss_fn=None, n_batches_train=1, n_batches_valid=4, metrics=None, n_output_units=1, batch_size=None, shuffle=None) ### Description A solver class for solving ODEs (single-input differential equations) or a bundle of ODEs for different values of its parameters and/or conditions. It inherits from BaseSolver and provides the same methods as BaseSolver. ### Constructor Parameters - **ode_system** (callable) - Required - The ODE system to solve, which maps a torch.Tensor or a tuple of torch.Tensors, to a tuple of ODE residuals, both the input and output must have shape (n_samples, 1). - **conditions** (list[neurodiffeq.conditions.BaseCondition]) - Required - List of conditions for each target function. - **t_min** (float, optional) - Required - Lower bound of input (start time). Ignored if train_generator and valid_generator are both set. - **t_max** (float, optional) - Required - Upper bound of input (start time). Ignored if train_generator and valid_generator are both set. - **theta_min** (float or tuple, optional) - Optional - Lower bound of input (parameters and/or conditions). If conditions are included in the bundle, the order should match the one inferred by the values of the bundle_param_lookup input in the neurodiffeq.conditions.BundleIVP. Defaults to None. Ignored if train_generator and valid_generator are both set. - **theta_max** (float or tuple, optional) - Optional - Upper bound of input (parameters and/or conditions). If conditions are included in the bundle, the order should match the one inferred by the values of the bundle_param_lookup input in the neurodiffeq.conditions.BundleIVP. Defaults to None. Ignored if train_generator and valid_generator are both set. - **eq_param_index** (int or tuple[int], optional) - Optional - Index (or indices) of bundle parameter that appears in the equation. E.g., if there are 5 bundle parameters generated and the first (index 0) and last (index 4) parameters are used in the equation, then eq_param_index should be (0, 4). The signature of the original equation should have, in addition to functions to solve for and coordinates, 2 more parameters, corresponding to bundle parameters indexed at 0 and 4, in that order. - **nets** (list[torch.nn.Module], optional) - Optional - List of neural networks for parameterized solution. If provided, length of nets must equal that of conditions. - **train_generator** (neurodiffeq.generators.BaseGenerator, optional) - Optional - Generator for sampling training points, which must provide a .get_examples() method and a .size field. train_generator must be specified if t_min and t_max are not set. If provided, the generator must generate bundle parameters too. - **valid_generator** (neurodiffeq.generators.BaseGenerator, optional) - Optional - Generator for sampling validation points, which must provide a .get_examples() method and a .size field. valid_generator must be specified if t_min and t_max are not set. If provided, the generator must generate bundle parameters too. - **analytic_solutions** (callable, optional) - Optional - Analytical solutions to be compared with neural net solutions. It maps a torch.Tensor to a tuple of function values. Output shape should match that of nets. - **optimizer** (torch.nn.optim.Optimizer, optional) - Optional - Optimizer to be used for training. Defaults to a torch.optim.Adam instance that trains on all parameters of nets. - **loss_fn** (str or torch.nn.modules.loss._Loss or callable) - Optional - The loss function used for training. ### Inherited Methods This class inherits from BaseSolver and provides the following methods: get_solution, global_epoch, run_train_epoch, run_valid_epoch. ``` -------------------------------- ### Generator1D Source: https://neurodiffeq.readthedocs.io/en/latest/api.html An example generator for generating 1-D training points. ```APIDOC ## Generator1D ### Description An example generator for generating 1-D training points. ### Class neurodiffeq.generators.Generator1D ### Constructor Parameters - **size** (int) - Required - The number of points to generate each time get_examples is called. - **t_min** (float, optional) - Optional - The lower bound of the 1-D points generated, defaults to 0.0. - **t_max** (float, optional) - Optional - The upper bound of the 1-D points generated, defaults to 1.0. - **method** (str, optional) - Optional - The distribution of the 1-D points generated. Defaults to 'uniform'. Supported values: 'uniform', 'equally-spaced', 'equally-spaced-noisy', 'log-spaced', 'log-spaced-noisy', 'chebyshev1', 'chebyshev', 'chebyshev2'. - **noise_std** (float, optional) - Optional - Standard deviation for noise when method is 'equally-spaced-noisy' or 'log-spaced-noisy'. ### Raises - **ValueError** - When provided with an unknown method. ``` -------------------------------- ### Solve 1-D heat equation with IBVP1D and Solver2D Source: https://neurodiffeq.readthedocs.io/en/latest/getstart.html Set up and solve the 1-D heat equation using IBVP1D conditions and Solver2D. The first block imports IBVP1D and make_animation; the second defines the PDE, conditions, network, solver, and fits it for 200 epochs. ```python from neurodiffeq.conditions import IBVP1D from neurodiffeq.pde import make_animation ``` ```python k, L, T = 0.3, 2, 3 # Define the PDE system # There's only one (heat) equation in the system, so the function maps (u, x, y) to a single entry heat = lambda u, x, t: [ diff(u, t) - k * diff(u, x, order=2) ] # Define the initial and boundary conditions # There's only one function to be solved for, so we only have a single condition object conditions = [ IBVP1D( t_min=0, t_min_val=lambda x: torch.sin(np.pi * x / L), x_min=0, x_min_prime=lambda t: np.pi/L * torch.exp(-k*np.pi**2*t/L**2), x_max=L, x_max_prime=lambda t: -np.pi/L * torch.exp(-k*np.pi**2*t/L**2) ) ] # Define the neural network to be used # Again, there's only one function to be solved for, so we only have a single network nets = [ FCNN(n_input_units=2, hidden_units=(32, 32)) ] # Define the monitor callback monitor=Monitor2D(check_every=10, xy_min=(0, 0), xy_max=(L, T)) monitor_callback = monitor.to_callback() # Instantiate the solver solver = Solver2D( pde_system=heat, conditions=conditions, xy_min=(0, 0), # We can omit xy_min when both train_generator and valid_generator are specified xy_max=(L, T), # We can omit xy_max when both train_generator and valid_generator are specified nets=nets, train_generator=Generator2D((32, 32), (0, 0), (L, T), method='equally-spaced-noisy'), valid_generator=Generator2D((32, 32), (0, 0), (L, T), method='equally-spaced'), ) # Fit the neural network solver.fit(max_epochs=200, callbacks=[monitor_callback]) # Obtain the solution solution_neural_net_heat = solver.get_solution() ``` -------------------------------- ### GeneratorND Source: https://neurodiffeq.readthedocs.io/en/latest/api.html An example generator for generating N-D training points. It supports various sampling methods and optional noise. ```APIDOC ## GeneratorND ### Description An example generator for generating N-D training points. It supports various sampling methods and optional noise. ### Constructor `GeneratorND(grid=(10, 10), r_min=(0.0, 0.0), r_max=(1.0, 1.0), methods=['equally-spaced', 'equally-spaced'], noisy=True, r_noise_std=None, **kwargs)` ### Parameters - **grid** (tuple of int, or int if N=1, optional) - The discretization of the N dimensions. Defaults to (10, 10). - **r_min** (tuple of float, or float if N=1, optional) - The lower bound of N dimensions. Defaults to (0.0, 0.0). - **r_max** (tuple of float, or float if N=1, optional) - The upper bound of N dimensions. Defaults to (1.0, 1.0). - **methods** (list of str, or str if N=1, optional) - The distributions of each 1-D points. Supported methods: 'uniform', 'equally-spaced', 'log-spaced', 'exp-spaced', 'chebyshev', 'chebyshev1', 'chebyshev2'. Defaults to ['equally-spaced', 'equally-spaced']. - **noisy** (bool) - If True, normal noise is added to all points. Defaults to True. - **r_noise_std** (optional) - Standard deviation for noise. ### Raises - **ValueError** - When provided with unknown methods. ``` -------------------------------- ### Solver2D.__init__ Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Constructor for the Solver2D class. Initializes a solver for 2D PDEs with the given PDE system, conditions, and optional configuration parameters. ```APIDOC ## Solver2D.__init__ ### Description Initializes a Solver2D instance for solving PDEs in 2 dimensions. ### Method Constructor ### Signature Solver2D(pde_system, conditions, xy_min=None, xy_max=None, nets=None, train_generator=None, valid_generator=None, analytic_solutions=None, optimizer=None, loss_fn=None, n_batches_train=1, n_batches_valid=4, metrics=None, n_output_units=1, batch_size=None, shuffle=None) ### Parameters - **pde_system** (callable) - Required - The PDE system to solve, which maps two torch.Tensor inputs to PDE residuals (tuple[torch.Tensor]). Both input and output must have shape (n_samples, 1). - **conditions** (list[neurodiffeq.conditions.BaseCondition]) - Required - List of conditions for each target function. - **xy_min** (tuple[float, float], optional) - The lower bound of 2 dimensions. Only needed when train_generator or valid_generator are not specified. Defaults to None. - **xy_max** (tuple[float, float], optional) - The upper bound of 2 dimensions. Only needed when train_generator or valid_generator are not specified. Defaults to None. - **nets** (list[torch.nn.Module], optional) - List of neural networks for parameterized solution. If provided, length must equal that of conditions. - **train_generator** (neurodiffeq.generators.BaseGenerator, optional) - Generator for sampling training points, must provide a .get_examples() method and a .size field. Must be specified if xy_min and xy_max are not set. - **valid_generator** (neurodiffeq.generators.BaseGenerator, optional) - Generator for sampling validation points, must provide a .get_examples() method and a .size field. Must be specified if xy_min and xy_max are not set. - **analytic_solutions** (callable, optional) - Analytical solutions to be compared with neural net solutions. Maps a torch.Tensor to a tuple of function values. Output shape should match that of nets. - **optimizer** (torch.nn.optim.Optimizer, optional) - Optimizer to be used for training. Defaults to a torch.optim.Adam instance that trains on all parameters of nets. - **loss_fn** (str or torch.nn.modules.loss._Loss or callable) - The loss function used for training. If str, must be present in the keys of neurodiffeq.losses._losses. If a torch.nn.modules.loss._Loss instance, pass the instance. If any other callable, it must map A) a residual tensor (shape (n_points, n_equations)), B) a function values tuple (length n_funcs, each element a tensor of shape (n_points, 1)), and C) a coordinate values tuple (length n_coords, each element a tensor of shape (n_coords, 1)) to a tensor of empty shape (i.e. a scalar). The returned tensor must be connected to the computational graph. - **n_batches_train** (int, optional) - Number of batches to train in every epoch, where batch-size equals train_generator.size. Defaults to 1. - **n_batches_valid** (int, optional) - Number of batches to validate in every epoch, where batch-size equals valid_generator.size. Defaults to 4. - **metrics** (dict[str, callable], optional) - Additional metrics to be logged (besides loss). Keys are metric names, values are functions that compute the metric value. These functions must accept the same input as the differential equation pde_system. - **n_output_units** (int, optional) - Number of output units for each neural network. Ignored if nets is specified. Defaults to 1. - **batch_size** (int) - DEPRECATED and IGNORED. Each batch will use all samples generated. Please specify n_batches_train and n_batches_valid instead. - **shuffle** (bool) - DEPRECATED and IGNORED. Shuffling should be performed by generators. ``` -------------------------------- ### SimpleTensorboardCallback Source: https://neurodiffeq.readthedocs.io/en/latest/api.html A callback that writes all metric values to the disk for TensorBoard to plot. Tensorboard must be installed for this callback to work. ```APIDOC ## SimpleTensorboardCallback ### Description A callback that writes all metric values to the disk for TensorBoard to plot. Tensorboard must be installed for this callback to work. ### Constructor `neurodiffeq.callbacks.SimpleTensorboardCallback(writer=None, logger=None)` ### Parameters - **writer** (torch.utils.tensorboard.SummaryWriter) - Optional - The summary writer for writing values to disk. Defaults to a new SummaryWriter instance created with default kwargs. - **logger** (str or logging.Logger) - Optional - The logger (or its name) to be used for this callback. Defaults to the 'root' logger. ``` -------------------------------- ### Import NumPy and Matplotlib Source: https://neurodiffeq.readthedocs.io/en/latest/getstart.html Imports NumPy and Matplotlib and enables inline plotting in a Jupyter notebook. Required for the examples that follow. ```python import numpy as np import matplotlib.pyplot as plt %matplotlib notebook ``` -------------------------------- ### get_residuals(*coords, to_numpy=False, best=True, no_reshape=False) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Get the residuals of the differential equation (or system of differential equations) evaluated at given points. ```APIDOC ## get_residuals(*coords, to_numpy=False, best=True, no_reshape=False) ### Description Get the residuals of the differential equation (or system of differential equations) evaluated at given points. ### Method get_residuals ### Parameters - **coords** (tuple[torch.Tensor] or tuple[np.ndarray]) - Required - The coordinate values where the residual(s) shall be evaluated. If numpy arrays are passed, the method implicitly creates torch tensors with corresponding values. - **to_numpy** (bool) - Optional - Whether to return numpy arrays. Defaults to False. - **best** (bool) - Optional - If set to False, the network from the most recent epoch will be used to evaluate the residuals. If set to True, the network from the epoch with the lowest validation loss (or training loss, if there's no validation) will be used. Defaults to True. - **no_reshape** (bool) - Optional - If set to True, no reshaping will be performed on output. Defaults to False. ### Returns The residuals evaluated at given points. If there is only one equation in the differential equation system, a single torch tensor (or numpy array) will be returned. If there are multiple equations, a list of torch tensors (or numpy arrays) will be returned. The returned shape will be the same as the first input coordinate, unless no_reshape is set to True. Note that the return value will always be torch tensors (even if coords are numpy arrays) unless to_numpy is explicitly set to True. ### Return Type list[torch.Tensor or numpy.array] or torch.Tensor or numpy.array ``` -------------------------------- ### MonitorSpherical.__init__ Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Initializes a MonitorSpherical instance with parameters for the spherical coordinate system and monitoring frequency. ```APIDOC ## MonitorSpherical.__init__ ### Description Initializes a MonitorSpherical instance with parameters for the spherical coordinate system and monitoring frequency. ### Method Constructor ### Parameters - **r_min** (float) - Required - The lower bound of radius, i.e., radius of interior boundary. - **r_max** (float) - Required - The upper bound of radius, i.e., radius of exterior boundary. - **check_every** (int, optional) - Optional - The frequency of checking the neural network represented by the number of epochs between two checks. Defaults to 100. - **var_names** (list[str]) - Optional - Names of dependent variables. If provided, shall be used for plot titles. Defaults to None. - **shape** (tuple[int]) - Optional - Shape of mesh for visualizing the solution. Defaults to (10, 10, 10). - **r_scale** (str) - Optional - 'linear' or 'log'. Controls the grid point in the r direction. Defaults to 'linear'. - **theta_min** (float) - Optional - The lower bound of polar angle. Defaults to 0.0. - **theta_max** (float) - Optional - The upper bound of polar angle. Defaults to pi. - **phi_min** (float) - Optional - The lower bound of azimuthal angle. Defaults to 0.0. - **phi_max** (float) - Optional - The upper bound of azimuthal angle. Defaults to 2*pi. ``` -------------------------------- ### Solver.fit(max_epochs, callbacks=(), tqdm_file=, **kwargs) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Runs multiple epochs of training and validation, updating the best loss at the end of each epoch. If callbacks are provided, they are executed after training, validating, and updating the best model. ```APIDOC ## fit(max_epochs, callbacks=(), tqdm_file=, **kwargs) ### Description Run multiple epochs of training and validation, update best loss at the end of each epoch. If callbacks is passed, callbacks are run, one at a time, after training, validating and updating best model. ### Parameters - **max_epochs** (int) - Required - Number of epochs to run. - **callbacks** (list[callable]) - Optional - A list of callback functions. Each function should accept the solver instance itself as its only argument. - **tqdm_file** (io.StringIO or io.TextIOWrapper) - Optional - File to write tqdm progress bar. If set to None, tqdm is not used at all. Defaults to sys.stderr. ### Notes - This method does not return solution, which is done in the .get_solution() method. - A callback cb(solver) can set solver._stop_training to True to perform early stopping. ``` -------------------------------- ### get_residuals(*coords, to_numpy=False, best=True, no_reshape=False) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Get the residuals of the differential equation (or system of differential equations) evaluated at given points. ```APIDOC ## get_residuals(*coords, to_numpy=False, best=True, no_reshape=False) ### Description Get the residuals of the differential equation (or system of differential equations) evaluated at given points. ### Parameters - **coords** (tuple[torch.Tensor] or tuple[np.ndarray]) - Required - The coordinate values where the residual(s) shall be evaluated. If numpy arrays are passed, the method implicitly creates torch tensors with corresponding values. - **to_numpy** (bool) - Optional - Whether to return numpy arrays. Defaults to False. - **best** (bool) - Optional - If set to False, the network from the most recent epoch will be used to evaluate the residuals. If set to True, the network from the epoch with the lowest validation loss (or training loss, if there's no validation) will be used to evaluate the residuals. Defaults to True. - **no_reshape** (bool) - Optional - If set to True, no reshaping will be performed on output. Defaults to False. ### Returns The residuals evaluated at given points. If there is only one equation in the differential equation system, a single torch tensor (or numpy array) will be returned. If there are multiple equations, a list of torch tensors (or numpy arrays) will be returned. The returned shape will be the same as the first input coordinate, unless no_reshape is set to True. Note that the return value will always be torch tensors (even if coords are numpy arrays) unless to_numpy is explicitly set to True. ### Return Type list[torch.Tensor or numpy.array] or torch.Tensor or numpy.array ``` -------------------------------- ### fit(max_epochs, callbacks=(), tqdm_file=sys.stderr, **kwargs) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Runs multiple epochs of training and validation, updating the best loss at the end of each epoch. Callbacks can be provided to run after each epoch, and early stopping can be triggered by setting solver._stop_training. ```APIDOC ## fit(max_epochs, callbacks=(), tqdm_file=sys.stderr, **kwargs) ### Description Run multiple epochs of training and validation, update best loss at the end of each epoch. If callbacks are passed, they are run one at a time after training, validating, and updating the best model. ### Method fit ### Parameters - **max_epochs** (int) - Required - Number of epochs to run. - **callbacks** (list[callable]) - Optional - A list of callback functions. Each function should accept the solver instance itself as its only argument. - **tqdm_file** (io.StringIO or io.TextIOWrapper) - Optional - File to write tqdm progress bar. If set to None, tqdm is not used at all. Defaults to sys.stderr. ### Notes - This method does not return solution, which is done in the .get_solution() method. - A callback cb(solver) can set solver._stop_training to True to perform early stopping. ``` -------------------------------- ### Get a solution object with get_solution Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Retrieves a callable solution object from a solver. The returned object can be evaluated by passing coordinate vectors obtained from a generator. ```python solution = solver.get_solution() point_coords = train_generator.get_examples() value_at_points = solution(point_coords) ``` -------------------------------- ### make_animation(solution, xs, ts) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Creates an animation of 1-D time-dependent problems. ```APIDOC ## make_animation(solution, xs, ts) ### Description Create animation of 1-D time-dependent problems. ### Method make_animation ### Parameters - **solution** (callable) - Required - Solution function returned by solve2D (for a 1-D time-dependent problem). - **xs** (numpy.array) - Required - The locations to evaluate solution. - **ts** (numpy.array) - Required - The time points to evaluate solution. ### Returns - **matplotlib.animation.FuncAnimation** - The animation. ``` -------------------------------- ### Retrieve Solver1D internals with get_internals() Source: https://neurodiffeq.readthedocs.io/en/latest/advanced.html Set up a Solver1D for the parametric circle ODE system, fit it for 100 epochs, and retrieve the internals dictionary with get_internals(). The displayed output shows the resulting internals, including the trained networks, optimizer state, generators, and loss value. ```python parametric_circle = lambda x1, x2, t: [diff(x1, t) - x2, diff(x2, t) + x1] init_vals_pc = [ IVP(t_0=0.0, u_0=0.0), IVP(t_0=0.0, u_0=1.0), ] solver = Solver1D( ode_system=parametric_circle, conditions=init_vals_pc, t_min=0.0, t_max=2*np.pi, ) solver.fit(max_epochs=100) internals = solver.get_internals() ``` ```python internals ``` ```python {'metrics': {}, 'n_batches': {'train': 1, 'valid': 4}, 'best_nets': [FCNN( (NN): Sequential( (0): Linear(in_features=1, out_features=32, bias=True) (1): Tanh() (2): Linear(in_features=32, out_features=32, bias=True) (3): Tanh() (4): Linear(in_features=32, out_features=1, bias=True) ) ), FCNN( (NN): Sequential( (0): Linear(in_features=1, out_features=32, bias=True) (1): Tanh() (2): Linear(in_features=32, out_features=32, bias=True) (3): Tanh() (4): Linear(in_features=32, out_features=1, bias=True) ) )], 'criterion': .(r)>, 'conditions': [, ], 'global_epoch': 100, 'lowest_loss': 0.023892831479509158, 'n_funcs': 2, 'nets': [FCNN( (NN): Sequential( (0): Linear(in_features=1, out_features=32, bias=True) (1): Tanh() (2): Linear(in_features=32, out_features=32, bias=True) (3): Tanh() (4): Linear(in_features=32, out_features=1, bias=True) ) ), FCNN( (NN): Sequential( (0): Linear(in_features=1, out_features=32, bias=True) (1): Tanh() (2): Linear(in_features=32, out_features=32, bias=True) (3): Tanh() (4): Linear(in_features=32, out_features=1, bias=True) ) )], 'optimizer': Adam ( Parameter Group 0 amsgrad: False betas: (0.9, 0.999) eps: 1e-08 lr: 0.001 weight_decay: 0 ), 'diff_eqs': (x1, x2, t)>, 'generator': {'train': SamplerGenerator(size=32, generator=Generator1D(size=32, t_min=0.0, t_max=6.283185307179586, method='equally-spaced-noisy', noise_std=0.04908738521234052)), 'valid': SamplerGenerator(size=32, generator=Generator1D(size=32, t_min=0.0, t_max=6.283185307179586, method='equally-spaced', noise_std=0.04908738521234052))}, 'train_generator': SamplerGenerator(size=32, generator=Generator1D(size=32, t_min=0.0, t_max=6.283185307179586, method='equally-spaced-noisy', noise_std=0.04908738521234052)), 'valid_generator': SamplerGenerator(size=32, generator=Generator1D(size=32, t_min=0.0, t_max=6.283185307179586, method='equally-spaced', noise_std=0.04908738521234052)), 't_min': 0.0, 't_max': 6.283185307179586} ``` -------------------------------- ### get_solution Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Get a callable solution object that can be used to evaluate the solution at given points. Optionally copies the networks to prevent subsequent training from affecting the solution. ```APIDOC ## get_solution ### Description Get a (callable) solution object. See usage example: solution = solver.get_solution(); point_coords = train_generator.get_examples(); value_at_points = solution(point_coords). ### Method Function call ### Parameters - **copy** (bool) - Optional - Whether to make a copy of the networks so that subsequent training doesn't affect the solution. Defaults to True. - **best** (bool) - Optional - Whether to return the solution with lowest loss instead of the solution after the last epoch. Defaults to True. ### Returns A solution object which can be called. To evaluate the solution on certain points, pass the coordinates vector(s) to the returned solution. Return type: BaseSolution. ``` -------------------------------- ### get_residuals(*coords, to_numpy=False, best=True, no_reshape=False) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Gets the residuals of the differential equation (or system of differential equations) evaluated at given points. Supports both torch tensors and numpy arrays as input coordinates. ```APIDOC ## get_residuals(*coords, to_numpy=False, best=True, no_reshape=False) ### Description Get the residuals of the differential equation (or system of differential equations) evaluated at given points. ### Parameters - **coords** (tuple[torch.Tensor] or tuple[np.ndarray]) - Required - The coordinate values where the residual(s) shall be evaluated. If numpy arrays are passed, the method implicitly creates torch tensors with corresponding values. - **to_numpy** (bool) - Optional - Whether to return numpy arrays. Defaults to False. - **best** (bool) - Optional - If set to False, the network from the most recent epoch will be used to evaluate the residuals. If set to True, the network from the epoch with the lowest validation loss (or training loss, if there's no validation) will be used to evaluate the residuals. Defaults to True. - **no_reshape** (bool) - Optional - If set to True, no reshaping will be performed on output. Defaults to False. ### Returns - The residuals evaluated at given points. If there is only one equation in the differential equation system, a single torch tensor (or numpy array) will be returned. If there are multiple equations, a list of torch tensors (or numpy arrays) will be returned. The returned shape will be the same as the first input coordinate, unless no_reshape is set to True. Note that the return value will always be torch tensors (even if `coords` are numpy arrays) unless to_numpy is explicitly set to True. ### Return Type - list[torch.Tensor or numpy.array] or torch.Tensor or numpy.array ``` -------------------------------- ### Animate heat equation solution with make_animation Source: https://neurodiffeq.readthedocs.io/en/latest/getstart.html Create a grid and animate the neural network solution for the heat equation. The output shows the resulting FuncAnimation object. ```python xs = np.linspace(0, L, 101) ts = np.linspace(0, T, 101) xx, tt = np.meshgrid(xs, ts) make_animation(solution_neural_net_heat, xs, ts) ``` ```output ``` -------------------------------- ### fit(max_epochs, callbacks=(), tqdm_file=sys.stderr, **kwargs) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Runs multiple epochs of training and validation, updating best loss at the end of each epoch. If callbacks are passed, they are run after training, validating, and updating the best model. ```APIDOC ## fit(max_epochs, callbacks=(), tqdm_file=sys.stderr, **kwargs) ### Description Run multiple epochs of training and validation, update best loss at the end of each epoch. If `callbacks` is passed, callbacks are run, one at a time, after training, validating and updating best model. ### Parameters - **max_epochs** (int) - Required - Number of epochs to run. - **callbacks** (list[callable]) - Optional - A list of callback functions. Each function should accept the `solver` instance itself as its **only** argument. - **tqdm_file** (io.StringIO or io.TextIOWrapper) - Optional - File to write tqdm progress bar. If set to None, tqdm is not used at all. Defaults to `sys.stderr`. ### Notes - This method does not return solution, which is done in the `.get_solution()` method. - A callback `cb(solver)` can set `solver._stop_training` to True to perform early stopping. ``` -------------------------------- ### get_residuals(*coords, to_numpy=False, best=True, no_reshape=False) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Gets the residuals of the differential equation (or system of differential equations) evaluated at given points. Supports numpy arrays and returns torch tensors by default. ```APIDOC ## get_residuals(*coords, to_numpy=False, best=True, no_reshape=False) ### Description Get the residuals of the differential equation (or system of differential equations) evaluated at given points. ### Method get_residuals ### Parameters - **coords** (tuple[torch.Tensor] or tuple[np.ndarray]) - Required - The coordinate values where the residual(s) shall be evaluated. If numpy arrays are passed, the method implicitly creates torch tensors with corresponding values. - **to_numpy** (bool) - Optional - Whether to return numpy arrays. Defaults to False. - **best** (bool) - Optional - If set to False, the network from the most recent epoch will be used. If set to True, the network from the epoch with the lowest validation loss (or training loss, if there's no validation) will be used. Defaults to True. - **no_reshape** (bool) - Optional - If set to True, no reshaping will be performed on output. Defaults to False. ### Returns - The residuals evaluated at given points. If there is only one equation, a single torch tensor (or numpy array) will be returned. If there are multiple equations, a list of torch tensors (or numpy arrays) will be returned. The returned shape will be the same as the first input coordinate, unless no_reshape is set to True. Note that the return value will always be torch tensors (even if coords are numpy arrays) unless to_numpy is explicitly set to True. ### Return Type list[torch.Tensor or numpy.array] or torch.Tensor or numpy.array ``` -------------------------------- ### get_solution(copy=True, best=True) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Returns a callable solution object. The solution can be evaluated on coordinate vectors to obtain values. The copy parameter controls whether a copy of the networks is made, and best controls whether the solution with the lowest loss is returned. ```APIDOC ## get_solution(copy=True, best=True) ### Description Returns a callable solution object. The solution can be evaluated on coordinate vectors to obtain values. ### Method Function call ### Parameters - **copy** (bool) - Optional - Whether to make a copy of the networks so that subsequent training doesn't affect the solution. Defaults to True. - **best** (bool) - Optional - Whether to return the solution with lowest loss instead of the solution after the last epoch. Defaults to True. ### Returns - **BaseSolution** - A solution object which can be called. To evaluate the solution on certain points, pass the coordinates vector(s) to the returned solution. ### Example ```python solution = solver.get_solution() point_coords = train_generator.get_examples() value_at_points = solution(point_coords) ``` ``` -------------------------------- ### get_residuals Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Get the residuals of the differential equation (or system) evaluated at given points. Accepts coordinates as torch tensors or numpy arrays, with options to return numpy arrays and control which network state to use. ```APIDOC ## get_residuals ### Description Get the residuals of the differential equation (or system of differential equations) evaluated at given points. ### Method Function call ### Parameters - **coords** (tuple[torch.Tensor] or tuple[np.ndarray]) - Required - The coordinate values where the residual(s) shall be evaluated. If numpy arrays are passed, the method implicitly creates torch tensors with corresponding values. - **to_numpy** (bool) - Optional - Whether to return numpy arrays. Defaults to False. - **best** (bool) - Optional - If set to False, the network from the most recent epoch will be used to evaluate the residuals. If set to True, the network from the epoch with the lowest validation loss (or training loss, if there's no validation) will be used. Defaults to True. - **no_reshape** (bool) - Optional - If set to True, no reshaping will be performed on output. Defaults to False. ### Returns The residuals evaluated at given points. If there is only one equation, a single torch tensor (or numpy array) is returned. If multiple equations, a list of torch tensors (or numpy arrays) is returned. The returned shape is the same as the first input coordinate, unless no_reshape is True. Return type: list[torch.Tensor or numpy.array] or torch.Tensor or numpy.array. ``` -------------------------------- ### fit(max_epochs, callbacks=(), tqdm_file=sys.stderr, **kwargs) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Run multiple epochs of training and validation, update best loss at the end of each epoch. If callbacks is passed, callbacks are run, one at a time, after training, validating and updating best model. ```APIDOC ## fit(max_epochs, callbacks=(), tqdm_file=sys.stderr, **kwargs) ### Description Run multiple epochs of training and validation, update best loss at the end of each epoch. If callbacks is passed, callbacks are run, one at a time, after training, validating and updating best model. ### Method fit ### Parameters - **max_epochs** (int) - Required - Number of epochs to run. - **callbacks** (list[callable]) - Optional - A list of callback functions. Each function should accept the solver instance itself as its only argument. - **tqdm_file** (io.StringIO or io.TextIOWrapper) - Optional - File to write tqdm progress bar. If set to None, tqdm is not used at all. Defaults to sys.stderr. ### Notes 1. This method does not return solution, which is done in the .get_solution() method. 2. A callback cb(solver) can set solver._stop_training to True to perform early stopping. ``` -------------------------------- ### Plot ANN-based vs analytical solution for harmonic oscillator Source: https://neurodiffeq.readthedocs.io/en/latest/getstart.html Compare the ANN-based solution from `solution_pc` with the analytical solution for the harmonic oscillator. The solution function is called with `to_numpy=True` to get numpy arrays for u1 and u2. The plot shows both solutions for u1 and u2 over t from 0 to 2π. ```python ts = np.linspace(0, 2*np.pi, 100) u1_net, u2_net = solution_pc(ts, to_numpy=True) u1_ana, u2_ana = np.sin(ts), np.cos(ts) plt.figure() plt.plot(ts, u1_net, label='ANN-based solution of $u_1$') plt.plot(ts, u1_ana, '.', label='Analytical solution of $u_1$') plt.plot(ts, u2_net, label='ANN-based solution of $u_2$') plt.plot(ts, u2_ana, '.', label='Analytical solution of $u_2$') plt.ylabel('u') plt.xlabel('t') plt.title('comparing solutions') plt.legend() plt.show() ``` -------------------------------- ### fit(max_epochs, callbacks=(), tqdm_file=, **kwargs) Source: https://neurodiffeq.readthedocs.io/en/latest/api.html Run multiple epochs of training and validation, update best loss at the end of each epoch. If callbacks is passed, callbacks are run, one at a time, after training, validating and updating best model. ```APIDOC ## fit(max_epochs, callbacks=(), tqdm_file=, **kwargs) ### Description Run multiple epochs of training and validation, update best loss at the end of each epoch. If callbacks is passed, callbacks are run, one at a time, after training, validating and updating best model. ### Parameters - **max_epochs** (int) - Required - Number of epochs to run. - **callbacks** (list[callable]) - Optional - A list of callback functions. Each function should accept the solver instance itself as its only argument. - **tqdm_file** (io.StringIO or io.TextIOWrapper) - Optional - File to write tqdm progress bar. If set to None, tqdm is not used at all. Defaults to sys.stderr. ### Notes 1. This method does not return solution, which is done in the .get_solution() method. 2. A callback cb(solver) can set solver._stop_training to True to perform early stopping. ```