torchphysics.problem.domains.functionsets package
Function sets can be used to sample functions, e.g. in DeepONet.
Submodules
torchphysics.problem.domains.functionsets.FE_functionset module
- class torchphysics.problem.domains.functionsets.FE_functionset.FEFunctionSet(function_space, order, mesh_vertices, mesh_triangles=None)[source]
Bases:
TestFunctionSet
- class torchphysics.problem.domains.functionsets.FE_functionset.LinearFE1D(mesh_vertices: tensor)[source]
Bases:
object
torchphysics.problem.domains.functionsets.functionset module
- class torchphysics.problem.domains.functionsets.functionset.CustomFunctionSet(function_space, parameter_sampler, custom_fn)[source]
Bases:
FunctionSet
FunctionSet for an arbitrary function.
- Parameters:
function_space (torchphysics.spaces.FunctionSpace) – The space of which this set of functions belongs to. The inputs and outputs of this FunctionSet are defined by the corresponding values inside the function space.
parameter_sampler (torchphysics.samplers.PointSampler) –
A sampler that provides additional parameters that can be used to create different kinds of functions. E.g. our FunctionSet consists of Functions like k*x, x is the input variable and k is given through the sampler.
During each training iteration will call the parameter_sampler to sample new parameters. For each parameter a function will be created and the input batch of functions will be of the same length as the sampled parameters.
custom_fn (callable) – A function that describes the FunctionSet. The input of the functions can include the variables of the function_space.input_space and the parameters from the parameter_sampler.
- class torchphysics.problem.domains.functionsets.functionset.FunctionSet(function_space, parameter_sampler)[source]
Bases:
object
A set of functions that can supply samples from a function space.
- Parameters:
function_space (torchphysics.spaces.FunctionSpace) – The space of which this set of functions belongs to. The inputs and outputs of this FunctionSet are defined by the corresponding values inside the function space.
parameter_sampler (torchphysics.samplers.PointSampler) –
A sampler that provides additional parameters that can be used to create different kinds of functions. E.g. our FunctionSet consists of Functions like k*x, x is the input variable and k is given through the sampler.
During each training iteration will call the parameter_sampler to sample new parameters. For each parameter a function will be created and the input batch of functions will be of the same length as the sampled parameters.
- __add__(other)[source]
Combines two function sets.
Notes
When parameters are sampled, will sample them from both sets. Creates a batch of functions consisting of the batch of each set. (Length of the batches will be added)
- create_function_batch(points)[source]
Evaluates the underlying function object to create a batch of discrete function samples.
- Parameters:
points (torchphysics.spaces.Points) – The input points, where we want to evaluate a set of functions.
- Returns:
The batch of discrete function samples. The underlying tensor is of the shape: [len(self), len(points), self.function_space.output_space.dim]
- Return type:
torchphysics.spaces.Points
- sample_params(device='cpu')[source]
Samples parameters of the function space.
- Parameters:
device (str, optional) – The device, where the parameters should be created. Default is ‘cpu’.
Notes
We save the sampled parameters internally, so that we can use them multiple times. Since given a parameter we still have a continuous representation of the underlying function types. When the functions should be evaluated at some input points, we just have to create the meshgrid of parameters and points.
- class torchphysics.problem.domains.functionsets.functionset.FunctionSetCollection(function_sets)[source]
Bases:
FunctionSet
Collection of multiple FunctionSets. Used for the additions of different FunctionSets.
- __add__(other)[source]
Combines two function sets.
Notes
When parameters are sampled, will sample them from both sets. Creates a batch of functions consisting of the batch of each set. (Length of the batches will be added)
- create_function_batch(points)[source]
Evaluates the underlying function object to create a batch of discrete function samples.
- Parameters:
points (torchphysics.spaces.Points) – The input points, where we want to evaluate a set of functions.
- Returns:
The batch of discrete function samples. The underlying tensor is of the shape: [len(self), len(points), self.function_space.output_space.dim]
- Return type:
torchphysics.spaces.Points
- sample_params(device='cpu')[source]
Samples parameters of the function space.
- Parameters:
device (str, optional) – The device, where the parameters should be created. Default is ‘cpu’.
Notes
We save the sampled parameters internally, so that we can use them multiple times. Since given a parameter we still have a continuous representation of the underlying function types. When the functions should be evaluated at some input points, we just have to create the meshgrid of parameters and points.
- class torchphysics.problem.domains.functionsets.functionset.TestFunctionHelper(*args, **kwargs)[source]
Bases:
Function
- static backward(ctx, grad_output)[source]
Define a formula for differentiating the operation with backward mode automatic differentiation.
This function is to be overridden by all subclasses. (Defining this function is equivalent to defining the
vjp
function.)It must accept a context
ctx
as the first argument, followed by as many outputs as theforward()
returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs toforward()
. Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.The context can be used to retrieve tensors saved during the forward pass. It also has an attribute
ctx.needs_input_grad
as a tuple of booleans representing whether each input needs gradient. E.g.,backward()
will havectx.needs_input_grad[0] = True
if the first input toforward()
needs gradient computed w.r.t. the output.
- static forward(ctx, x, expected_out, grad_out)[source]
Define the forward of the custom autograd Function.
This function is to be overridden by all subclasses. There are two ways to define forward:
Usage 1 (Combined forward and ctx):
@staticmethod def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any: pass
It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).
See combining-forward-context for more details
Usage 2 (Separate forward and ctx):
@staticmethod def forward(*args: Any, **kwargs: Any) -> Any: pass @staticmethod def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None: pass
The forward no longer accepts a ctx argument.
Instead, you must also override the
torch.autograd.Function.setup_context()
staticmethod to handle setting up thectx
object.output
is the output of the forward,inputs
are a Tuple of inputs to the forward.See extending-autograd for more details
The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with
ctx.save_for_backward()
if they are intended to be used inbackward
(equivalently,vjp
) orctx.save_for_forward()
if they are intended to be used for injvp
.
- class torchphysics.problem.domains.functionsets.functionset.TestFunctionSet(function_space)[source]
Bases:
FunctionSet
torchphysics.problem.domains.functionsets.harmonic_functionset module
- class torchphysics.problem.domains.functionsets.harmonic_functionset.HarmonicFunctionSet1D(function_space, interval: Interval, frequence, samples_per_max_frequence: int = 5)[source]
Bases:
TestFunctionSet