torchphysics.problem.domains.functionsets package
Function sets can be used to sample functions, e.g. in DeepONet.
Submodules
torchphysics.problem.domains.functionsets.custom_functionset module
- class torchphysics.problem.domains.functionsets.custom_functionset.CustomFunctionSet(function_space, parameter_sampler: PointSampler, custom_fn)[source]
Bases:
FunctionSetFunctionSet for an arbitrary function.
- Parameters:
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.
- create_functions(device='cpu')[source]
Creates the functions for the function set and stores them. The created functions can then be retrieved by the get_function method.
- Parameters:
device (str) – The device on which the functions should be stored.
- get_function(idx)[source]
Returns a function from the function set.
- Parameters:
idx (int or list, tensor of int) – The index of the function that should be returned. Multiple functions can be returned at once when a list or tensor is passed in. Note that the index should be in the range of the function set size.
- Returns:
Returns the function. If the function set is discrete, the functions can not be further evaluated and are therefore returned as a tensor. Otherwise a callable is returned that can be evaluated at any point.
- Return type:
callable or torch.tensor
torchphysics.problem.domains.functionsets.data_functionset module
- class torchphysics.problem.domains.functionsets.data_functionset.DataFunctionSet(function_space, data)[source]
Bases:
DiscreteFunctionSetFunctionSet that is created from a given data set. This function set is always a discret set, since the data can not be evaluated at arbitrary points.
- Parameters:
function_space (tp.spaces.FunctionSpace) – The function space of the functions in the set.
data (torch.Tensor) – The data that describes the function values. The shape of the last dimension has to match the dimension of the output space.
- compute_normalization()[source]
Computes the mean and standard deviation over the data contained in this function set.
Notes
The values are not returned but stored internally. Use ‘.mean’ and ‘.std’ to obtain them.
- compute_pca(components, normalize_data=True)[source]
Carries out the principal component analysis for this function set.
- Parameters:
components (int) – The number of components that should be keeped in the PCA.
normalize_data (bool, optional) – If the data of the function set should be normalized before the PCA is computed (recommented). Default is true. Note, the normalization is only applied during this method and not saved afterwards, therefore the underlying data in this function set is not modified!
Notes
The PCA is not returned but instead saved internally for later usage. Use ‘.principal_components’ to obtain the PCA.
Also the data of the function set is flattened over all dimensions expect of the batch dimension. For higher dimensional data (e.g. images) other approaches (like localized PCAs on small patches) may be better suited.
- create_functions(device='cpu')[source]
Creates the functions for the function set and stores them. The created functions can then be retrieved by the get_function method.
- Parameters:
device (str) – The device on which the functions should be stored.
- get_function(idx)[source]
Returns a function from the function set.
- Parameters:
idx (int or list, tensor of int) – The index of the function that should be returned. Multiple functions can be returned at once when a list or tensor is passed in. Note that the index should be in the range of the function set size.
- Returns:
Returns the function. If the function set is discrete, the functions can not be further evaluated and are therefore returned as a tensor. Otherwise a callable is returned that can be evaluated at any point.
- Return type:
callable or torch.tensor
torchphysics.problem.domains.functionsets.functionset module
- class torchphysics.problem.domains.functionsets.functionset.DiscreteFunctionSet(function_space, function_set_size, data_shape)[source]
Bases:
FunctionSetA function set that only returns already discretized functions, which can not be evaluated at arbitrary locations.
- compute_normalization()[source]
Computes the mean and standard deviation over the data contained in this function set.
Notes
The values are not returned but stored internally. Use ‘.mean’ and ‘.std’ to obtain them.
- compute_pca(components: int, normalize_data: bool = True)[source]
Carries out the principal component analysis for this function set.
- Parameters:
components (int) – The number of components that should be keeped in the PCA.
normalize_data (bool, optional) – If the data of the function set should be normalized before the PCA is computed (recommented). Default is true. Note, the normalization is only applied during this method and not saved afterwards, therefore the underlying data in this function set is not modified!
Notes
The PCA is not returned but instead saved internally for later usage. Use ‘.principal_components’ to obtain the PCA.
Also the data of the function set is flattened over all dimensions expect of the batch dimension. For higher dimensional data (e.g. images) other approaches (like localized PCAs on small patches) may be better suited.
- discretize(locations)[source]
Discretizes the function set, to be always evaluated at the provided locations.
- Parameters:
locations (tp.spaces.Points) – The points at which the functions should be evaluated.
- Returns:
The discretized function set.
- Return type:
tp.domains.DiscretizedFunctionSet
- property is_discretized
Returns if the function set is already discretized.
- property mean
- property principal_components
Returns the principal components of this function set. It is requiered to first call ‘compute_pca’ to compute them and set a number n of the used components.
- Returns:
A list of the principal components in the shape of (U, S, V). - U is the matrix of the left singular vectors of shape
(function_set_size, n)
S is a vector containing the first n eigen values of the covariance matrix.
V is the matrix of the principal directions of shape (function_set_dimension, n)
See also: https://pytorch.org/docs/stable/generated/torch.pca_lowrank.html
- Return type:
- property std
- class torchphysics.problem.domains.functionsets.functionset.DiscretizedFunctionSet(function_set: FunctionSet, locations)[source]
Bases:
DiscreteFunctionSetA discretized function set that is always evaluated at the provided locations.
- Parameters:
function_set (tp.domains.FunctionSet) – The function set that should be discretized.
locations (tp.spaces.Points or torch.tensor) – The points at which the functions should be evaluated.
- __mul__(other)[source]
Creates a product of two function sets. Leading to a product in the function spaces and the function outputs are concatenated along the output dimension.
- Parameters:
one. (The other function set that should be multiplied with this)
- Returns:
The product of the two function sets.
- Return type:
tp.domains.FunctionSetProduct
- compute_normalization()[source]
Computes the mean and standard deviation over the data contained in this function set.
Notes
The values are not returned but stored internally. Use ‘.mean’ and ‘.std’ to obtain them.
- compute_pca(components, normalize_data=True)[source]
Carries out the principal component analysis for this function set.
- Parameters:
components (int) – The number of components that should be keeped in the PCA.
normalize_data (bool, optional) – If the data of the function set should be normalized before the PCA is computed (recommented). Default is true. Note, the normalization is only applied during this method and not saved afterwards, therefore the underlying data in this function set is not modified!
Notes
The PCA is not returned but instead saved internally for later usage. Use ‘.principal_components’ to obtain the PCA.
Also the data of the function set is flattened over all dimensions expect of the batch dimension. For higher dimensional data (e.g. images) other approaches (like localized PCAs on small patches) may be better suited.
- create_functions(device='cpu')[source]
Creates the functions for the function set and stores them. The created functions can then be retrieved by the get_function method.
- Parameters:
device (str) – The device on which the functions should be stored.
- get_function(idx)[source]
Returns a function from the function set.
- Parameters:
idx (int or list, tensor of int) – The index of the function that should be returned. Multiple functions can be returned at once when a list or tensor is passed in. Note that the index should be in the range of the function set size.
- Returns:
Returns the function. If the function set is discrete, the functions can not be further evaluated and are therefore returned as a tensor. Otherwise a callable is returned that can be evaluated at any point.
- Return type:
callable or torch.tensor
- class torchphysics.problem.domains.functionsets.functionset.FunctionSet(function_space, function_set_size)[source]
Bases:
objectA function set describes a specfic type of functions that can be used for creating data for training different operator approaches.
- Parameters:
function_space (tp.spaces.FunctionSpace) – The function space that the functions in the set should be part of. This defines what input and output space the functions have.
function_set_size (int) – An integer that defines how many functions are stored in the set. This is used for creating multiple functions at once and then storing them for later use.
Notes
The function_set_size is motivated by the case that creating functions (either by computations or loading from disk) can be computationally expensive if done for each function individually. Therefore, we aim to create multiple functions at once and then store them. When later functions are sampled from this set, they can be quickly returned. But this creation is not only done once but can be repeated after some time to obtain new functions.
- __add__(other)[source]
Performs the “pointwise” addition of two function sets.
- Parameters:
one. (The other function set that should be added to this)
- Returns:
The function sets that computes the sum of the inputs.
- Return type:
tp.domains.FunctionSetAdd
- __mul__(other)[source]
Creates a product of two function sets. Leading to a product in the function spaces and the function outputs are concatenated along the output dimension.
- Parameters:
one. (The other function set that should be multiplied with this)
- Returns:
The product of the two function sets.
- Return type:
tp.domains.FunctionSetProduct
- __sub__(other)[source]
Performs the “pointwise” substraction of two function sets.
- Parameters:
one. (The other function set that should be substracted from this)
- Returns:
The function sets that computes the difference of the inputs.
- Return type:
tp.domains.FunctionSetSubstract
- append(other)[source]
Stacks two function sets together, such that different kind of functions can be combined into one set.
- Parameters:
one. (The other function set that should be connected with this)
- Returns:
The collection of the two function sets.
- Return type:
tp.domains.FunctionSetCollection
- abstract create_functions(device='cpu')[source]
Creates the functions for the function set and stores them. The created functions can then be retrieved by the get_function method.
- Parameters:
device (str) – The device on which the functions should be stored.
- discretize(locations)[source]
Discretizes the function set, to be always evaluated at the provided locations.
- Parameters:
locations (tp.spaces.Points) – The points at which the functions should be evaluated.
- Returns:
The discretized function set.
- Return type:
tp.domains.DiscretizedFunctionSet
- abstract get_function(idx)[source]
Returns a function from the function set.
- Parameters:
idx (int or list, tensor of int) – The index of the function that should be returned. Multiple functions can be returned at once when a list or tensor is passed in. Note that the index should be in the range of the function set size.
- Returns:
Returns the function. If the function set is discrete, the functions can not be further evaluated and are therefore returned as a tensor. Otherwise a callable is returned that can be evaluated at any point.
- Return type:
callable or torch.tensor
- is_discretization_of(function_set)[source]
Returns if the function set is the discretization of another function set.
- Parameters:
function_set (tp.domains.FunctionSet) – The other function set we should compare with.
- property is_discretized
Returns if the function set is already discretized.
torchphysics.problem.domains.functionsets.functionset_operations module
- class torchphysics.problem.domains.functionsets.functionset_operations.FunctionSetAdd(function_space, function_sets)[source]
Bases:
FunctionSetArithmeticsA class handling the pointwise addition of two sets.
- class torchphysics.problem.domains.functionsets.functionset_operations.FunctionSetArithmetics(function_space, function_sets)[source]
Bases:
FunctionSet- create_functions(device='cpu')[source]
Creates the functions for the function set and stores them. The created functions can then be retrieved by the get_function method.
- Parameters:
device (str) – The device on which the functions should be stored.
- abstract discretize(locations)[source]
Discretizes the function set, to be always evaluated at the provided locations.
- Parameters:
locations (tp.spaces.Points) – The points at which the functions should be evaluated.
- Returns:
The discretized function set.
- Return type:
tp.domains.DiscretizedFunctionSet
- get_function(idx)[source]
Returns a function from the function set.
- Parameters:
idx (int or list, tensor of int) – The index of the function that should be returned. Multiple functions can be returned at once when a list or tensor is passed in. Note that the index should be in the range of the function set size.
- Returns:
Returns the function. If the function set is discrete, the functions can not be further evaluated and are therefore returned as a tensor. Otherwise a callable is returned that can be evaluated at any point.
- Return type:
callable or torch.tensor
- is_discretization_of(function_set)[source]
Returns if the function set is the discretization of another function set.
- Parameters:
function_set (tp.domains.FunctionSet) – The other function set we should compare with.
- property is_discretized
Returns if the function set is already discretized.
- class torchphysics.problem.domains.functionsets.functionset_operations.FunctionSetCollection(function_space, function_sets)[source]
Bases:
FunctionSetCollection of multiple FunctionSets. Used for combining different kinds of functions into one single set.
- Parameters:
function_space (tp.spaces.FunctionSpace) – The function space of the set.
function_sets (list) – A list of FunctionSets that should be combined.
- append(other)[source]
Stacks two function sets together, such that different kind of functions can be combined into one set.
- Parameters:
one. (The other function set that should be connected with this)
- Returns:
The collection of the two function sets.
- Return type:
tp.domains.FunctionSetCollection
- create_functions(device='cpu')[source]
Creates the functions for the function set and stores them. The created functions can then be retrieved by the get_function method.
- Parameters:
device (str) – The device on which the functions should be stored.
- discretize(locations)[source]
Discretizes the function set, to be always evaluated at the provided locations.
- Parameters:
locations (tp.spaces.Points) – The points at which the functions should be evaluated.
- Returns:
The discretized function set.
- Return type:
tp.domains.DiscretizedFunctionSet
- get_function(idx)[source]
Returns a function from the function set.
- Parameters:
idx (int or list, tensor of int) – The index of the function that should be returned. Multiple functions can be returned at once when a list or tensor is passed in. Note that the index should be in the range of the function set size.
- Returns:
Returns the function. If the function set is discrete, the functions can not be further evaluated and are therefore returned as a tensor. Otherwise a callable is returned that can be evaluated at any point.
- Return type:
callable or torch.tensor
- is_discretization_of(function_set)[source]
Returns if the function set is the discretization of another function set.
- Parameters:
function_set (tp.domains.FunctionSet) – The other function set we should compare with.
- property is_discretized
Returns if the function set is already discretized.
- class torchphysics.problem.domains.functionsets.functionset_operations.FunctionSetProduct(function_space, function_sets)[source]
Bases:
FunctionSetThe product of multiple function sets.
- Parameters:
function_space (tp.spaces.FunctionSpace) – The function space of the set.
function_sets (list) – A list of FunctionSets that should be multiplied together.
- __mul__(other)[source]
Creates a product of two function sets. Leading to a product in the function spaces and the function outputs are concatenated along the output dimension.
- Parameters:
one. (The other function set that should be multiplied with this)
- Returns:
The product of the two function sets.
- Return type:
tp.domains.FunctionSetProduct
- create_functions(device='cpu')[source]
Creates the functions for the function set and stores them. The created functions can then be retrieved by the get_function method.
- Parameters:
device (str) – The device on which the functions should be stored.
- discretize(locations)[source]
Discretizes the function set, to be always evaluated at the provided locations.
- Parameters:
locations (tp.spaces.Points) – The points at which the functions should be evaluated.
- Returns:
The discretized function set.
- Return type:
tp.domains.DiscretizedFunctionSet
- get_function(idx)[source]
Returns a function from the function set.
- Parameters:
idx (int or list, tensor of int) – The index of the function that should be returned. Multiple functions can be returned at once when a list or tensor is passed in. Note that the index should be in the range of the function set size.
- Returns:
Returns the function. If the function set is discrete, the functions can not be further evaluated and are therefore returned as a tensor. Otherwise a callable is returned that can be evaluated at any point.
- Return type:
callable or torch.tensor
- is_discretization_of(function_set)[source]
Returns if the function set is the discretization of another function set.
- Parameters:
function_set (tp.domains.FunctionSet) – The other function set we should compare with.
- property is_discretized
Returns if the function set is already discretized.
- class torchphysics.problem.domains.functionsets.functionset_operations.FunctionSetSubstract(function_space, function_sets)[source]
Bases:
FunctionSetArithmeticsA class handling the pointwise substraction of two sets.
- class torchphysics.problem.domains.functionsets.functionset_operations.FunctionSetTransform(function_set: FunctionSet, transformation)[source]
Bases:
FunctionSetA class that acts as a wrapper of a different function set to further modify the created functions. E.g., clamping the values produced by a different function set.
- Parameters:
function_set (tp.domains.FunctionSet) – The function set that should be transformed.
transformation (callable) – The function that carries out the transformation. This transformation will be carried out “pointwise”.
- create_functions(device='cpu')[source]
Creates the functions for the function set and stores them. The created functions can then be retrieved by the get_function method.
- Parameters:
device (str) – The device on which the functions should be stored.
- discretize(locations)[source]
Discretizes the function set, to be always evaluated at the provided locations.
- Parameters:
locations (tp.spaces.Points) – The points at which the functions should be evaluated.
- Returns:
The discretized function set.
- Return type:
tp.domains.DiscretizedFunctionSet
- get_function(idx)[source]
Returns a function from the function set.
- Parameters:
idx (int or list, tensor of int) – The index of the function that should be returned. Multiple functions can be returned at once when a list or tensor is passed in. Note that the index should be in the range of the function set size.
- Returns:
Returns the function. If the function set is discrete, the functions can not be further evaluated and are therefore returned as a tensor. Otherwise a callable is returned that can be evaluated at any point.
- Return type:
callable or torch.tensor
- is_discretization_of(function_set)[source]
Returns if the function set is the discretization of another function set.
- Parameters:
function_set (tp.domains.FunctionSet) – The other function set we should compare with.
- property is_discretized
Returns if the function set is already discretized.
torchphysics.problem.domains.functionsets.grf_functionset module
- class torchphysics.problem.domains.functionsets.grf_functionset.GRFFunctionSet(function_space, function_set_size, resolution, auto_cov_fn: callable = <function GRFFunctionSet.<lambda>>, random_generator_fn: callable = <built-in method randn of type object>, normalize: bool = True, sample_noise_in_fourier_space: bool = True, flatten: bool = False)[source]
Bases:
DiscreteFunctionSetA functionset that creates Gaussian Random Fields (GRF).
- Parameters:
resolution (int, tuple, list) – The resolution of the gausian random field. For higher dimensional fields a tuple or list must be passed in setting the resoultion for each dimension. Each resolution needs to be even.
auto_cov_fn (callable, optional) –
The function describing the correlation between the points in the gaussian random field. Is evaluated in the fourier space at the frequencies (k_i - resolution_i/2) for i = 1, …, dimension and k_i = 0,…, resolution_i. Default is a power law given by
lambda x : 1/(1 + sum([i**2 for i in x]))**2
random_generator_fn (callable, optional) –
A function that creates the underlying random variables. As an input obtains a tuple of the shape
(number of functions, resolution_1, …, resolution_n)
and also the keyword argument device. Should output random values in the corresponding shape on the given device (cpu, gpu, etc.) as a PyTorch tensor. Default is torch.randn, a normal distribution with variance 1.
normalize (bool, optional) – Normalizes the GRF such that it has mean zero and standard deviation 1.0. Default is True.
sample_noise_in_fourier_space (bool, optional) – If we can sample the noise directly in the fourier space, so the random_generator_fn is called two times and the noise is constructed as the sum of both function calls, where one output is multiplied by the imaginary unit. Else we will sample the noise and then transfer the data to the fourier space with a fft. Default is True.
flatten (bool, optional) – If the output should be flattened to a 1D tensor along all intermediate dimensions. Default is False.
- compute_normalization()[source]
Computes the mean and standard deviation over the data contained in this function set.
Notes
The values are not returned but stored internally. Use ‘.mean’ and ‘.std’ to obtain them.
- compute_pca(components, normalize_data=True)[source]
Carries out the principal component analysis for this function set.
- Parameters:
components (int) – The number of components that should be keeped in the PCA.
normalize_data (bool, optional) – If the data of the function set should be normalized before the PCA is computed (recommented). Default is true. Note, the normalization is only applied during this method and not saved afterwards, therefore the underlying data in this function set is not modified!
Notes
The PCA is not returned but instead saved internally for later usage. Use ‘.principal_components’ to obtain the PCA.
Also the data of the function set is flattened over all dimensions expect of the batch dimension. For higher dimensional data (e.g. images) other approaches (like localized PCAs on small patches) may be better suited.
- create_functions(device='cpu')[source]
Creates the functions for the function set and stores them. The created functions can then be retrieved by the get_function method.
- Parameters:
device (str) – The device on which the functions should be stored.
- get_function(idx)[source]
Returns a function from the function set.
- Parameters:
idx (int or list, tensor of int) – The index of the function that should be returned. Multiple functions can be returned at once when a list or tensor is passed in. Note that the index should be in the range of the function set size.
- Returns:
Returns the function. If the function set is discrete, the functions can not be further evaluated and are therefore returned as a tensor. Otherwise a callable is returned that can be evaluated at any point.
- Return type:
callable or torch.tensor
torchphysics.problem.domains.functionsets.harmonic_functionset module
- class torchphysics.problem.domains.functionsets.harmonic_functionset.HarmonicFunctionSet1D(function_space, function_set_size, period, max_frequence, random_sample_fn=<built-in method randn of type object>)[source]
Bases:
FunctionSet- A function set that creates harmonic functions in 1D.
- The functions are of the form
- \[\sum_{i=0}^{N} a_i \sin(\]
rac{2pi i x}{L}) + b_i cos( rac{2pi i x}{L})
where N is the maximum frequence, L is the period length and a_i, b_i are the fourier coefficients, which are created randomly.
- function_spacetp.spaces.FunctionSpace
The function space of the functions in the set.
- function_set_sizeint
The number of functions in the set. This sets how many a_i, b_i are created at once.
- periodfloat
The length of the underyling interval.
- max_frequenceint
The maximum frequence of the functions that are created.
- random_sample_fncallable, optional
A function that creates random samples to initialize the fourier coefficients. Default is torch.randn.
- create_functions(device='cpu')[source]
Creates the functions for the function set and stores them. The created functions can then be retrieved by the get_function method.
- Parameters:
device (str) – The device on which the functions should be stored.
- get_function(idx)[source]
Returns a function from the function set.
- Parameters:
idx (int or list, tensor of int) – The index of the function that should be returned. Multiple functions can be returned at once when a list or tensor is passed in. Note that the index should be in the range of the function set size.
- Returns:
Returns the function. If the function set is discrete, the functions can not be further evaluated and are therefore returned as a tensor. Otherwise a callable is returned that can be evaluated at any point.
- Return type:
callable or torch.tensor
- class torchphysics.problem.domains.functionsets.harmonic_functionset.HarmonicFunctionSet2D(function_space, function_set_size, period, max_frequence, random_sample_fn=<built-in method randn of type object>)[source]
Bases:
HarmonicFunctionSet1DA function set that creates harmonic functions in the given dimension. The functions are build from a fourier basis in the given space, see also https://en.wikipedia.org/wiki/Multidimensional_transform for the mathematical background.
- Parameters:
function_space (tp.spaces.FunctionSpace) – The function space of the functions in the set.
function_set_size (int) – The number of functions in the set. This sets how many a_i, b_i are created at once.
period (list or tuple) – The length of the underyling domain in each space direction.
max_frequence (list or tuple) – The maximum frequence of the functions in each space direction.
random_sample_fn (callable, optional) – A function that creates random samples to initialize the fourier coefficients. Default is torch.randn.
- class torchphysics.problem.domains.functionsets.harmonic_functionset.HarmonicFunctionSet3D(function_space, function_set_size, period, max_frequence, random_sample_fn=<built-in method randn of type object>)[source]
Bases:
HarmonicFunctionSet2D