torchphysics.problem.samplers package

Objects that sample points in a given domain. These objects handle the creation of training and validation points in the underlying geometries. In general they get the following inputs:

  • domain: the domain in which the points should be created. If you want to create points at the boundary of a domain, use domain.boundary as an input argument.

  • n_points or density: to set the number of wanted points. Either a fixed number can be chosen or the density of the points.

  • filter: A function that filters out special points, for example for local boundary conditions.

The default behavior of each sampler is, that in each iteration of the trainings process new points are created and used. If this is not desired, not useful (grid sampling) or not efficient (e.g. really complex domains) one can make every sampler static.

Instead of creating the Cartesian product of different domains it is also possible to create the product of different samplers. For some sampling strategies, this is required, e.g. point grids. If the product of the domains is created, the sampler will create points in the new domain. If the product of samplers is created, first every sampler will create points in its own domain and afterwards the product will create a meshgrid of the points. Therefore, the points of the product of samplers has in general more correlation, than the points of the domain product.

Submodules

torchphysics.problem.samplers.data_samplers module

File with samplers that handle external created data. E.g. measurements or validation data computed with other methods.

class torchphysics.problem.samplers.data_samplers.DataSampler(points)[source]

Bases: PointSampler

A sampler that processes external created data points.

Parameters:

points (torchphysics.spaces.points or dict) – The data points that this data sampler should pass to a condition. Either already a torchphysics.spaces.points object or in form of dictionary like: {‘x’: tensor_for_x, ‘t’: tensor_for_t, …..}. For the dicitionary all tensor need to have the same batch dimension.

sample_points(params=Points: {}, device='cpu')[source]

The method that creates the points. Also implemented in all child classes.

Parameters:
  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points

torchphysics.problem.samplers.grid_samplers module

File with samplers that create points with some kind of ordered structure.

class torchphysics.problem.samplers.grid_samplers.ExponentialIntervalSampler(domain, n_points, exponent)[source]

Bases: PointSampler

Will sample non equdistant grid points in the given interval. This works only on intervals!

Parameters:
  • domain (torchphysics.domain.Interval) – The Interval in which the points should be sampled.

  • n_points (int) – The number of points that should be sampled.

  • exponent (Number) –

    Determines how non equdistant the points are and at which corner they are accumulated. They are computed with a grid in [0, 1] and then transformed with the exponent and later scaled/translated:

    exponent < 1: More points at the upper bound.

    points = 1 - x**(1/exponent)

    exponent > 1: More points at the lower bound.

    points = x**(exponent)

sample_points(params=Points: {}, device='cpu')[source]

The method that creates the points. Also implemented in all child classes.

Parameters:
  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points

class torchphysics.problem.samplers.grid_samplers.GridSampler(domain, n_points=None, density=None, filter_fn=None)[source]

Bases: PointSampler

Will sample a equidistant point grid in the given domain.

Parameters:
  • domain (torchphysics.domain.Domain) – The domain in which the points should be sampled.

  • n_points (int, optional) – The number of points that should be sampled.

  • density (float, optional) – The desiered density of the created points.

  • filter_fn (callable, optional) – A function that restricts the possible positions of sample points. A point that is allowed should return True, therefore a point that should be removed must return false. The filter has to be able to work with a batch of inputs. The Sampler will use a rejection sampling to find the right amount of points.

torchphysics.problem.samplers.plot_samplers module

Samplers for plotting and animations of model outputs.

class torchphysics.problem.samplers.plot_samplers.AnimationSampler(plot_domain, animation_domain, frame_number, n_points=None, density=None, device='cpu', data_for_other_variables={})[source]

Bases: PlotSampler

A sampler that creates points for an animation.

Parameters:
  • plot_domain (Domain) – The domain over which the model/function should later be plotted. Will create points inside and at the boundary of the domain.

  • animation_domain (Interval) – The variable over which the animation should be created, e.g a time-interval.

  • frame_number (int) – The number of frames that should be used for the animation. This equals the number of points that will be created in the animation_domain.

  • n_points (int, optional) – The number of points that should be used for the plot domain.

  • density (float, optional) – The desiered density of the created points, in the plot domain.

  • device (str or torch device, optional) – The device of the model/function.

  • data_for_other_variables (dict, optional) – Since the animation will only evaluate the model at specific points, the values for all other variables are needed. E.g. {‘D’ : [1,2], …}

property animation_key

Retunrs the name of the animation variable

property plot_domain_constant

Returns if the plot domain is a constant domain or changes with respect to other variables.

sample_animation_points()[source]

Samples points out of the animation domain, e.g. time interval.

sample_plot_domain_points(animation_points)[source]

Samples points in the plot domain, e.g. space.

class torchphysics.problem.samplers.plot_samplers.PlotSampler(plot_domain, n_points=None, density=None, device='cpu', data_for_other_variables={})[source]

Bases: PointSampler

A sampler that creates a point grid over a domain (including the boundary). Only used for plotting,

Parameters:
  • plot_domain (Domain) – The domain over which the model/function should later be plotted. Will create points inside and at the boundary of the domain.

  • n_points (int, optional) – The number of points that should be used for the plot.

  • density (float, optional) – The desiered density of the created points.

  • device (str or torch device, optional) – The device of the model/function.

  • data_for_other_variables (dict or torchphysics.spaces.Points, optional) – Since the plot will only evaluate the model at a specific point, the values for all other variables are needed. E.g. {‘t’ : 1, ‘D’ : [1,2], …}

Notes

Can also be used to create your own PlotSampler. By either changing the used sampler after the initialization (self.sampler=…) or by creating your own class that inherits from PlotSampler.

construct_sampler()[source]

Construct the sampler which is used in the plot. Can be overwritten to include your own points structure.

sample_points(params=Points: {}, device='cpu')[source]

Creates the points for the plot. Does not need additional arguments, since they were set in the init.

set_data_for_other_variables(data_for_other_variables)[source]

Sets the data for all other variables. Essentially copies the values into a correct tensor.

transform_data_to_torch(data_for_other_variables)[source]

Transforms all inputs to a torch.tensor.

torchphysics.problem.samplers.random_samplers module

File with samplers that create random distributed points.

class torchphysics.problem.samplers.random_samplers.AdaptiveRandomRejectionSampler(domain, n_points=None, density=None, filter_fn=None)[source]

Bases: AdaptiveSampler

An adaptive sampler that creates more points in regions with high loss. During sampling, points with high loss are more likely to be kept for the next iteration, while points with small loss are regarded and resampled (random) uniformly in the whole domain.

Parameters:
  • domain (torchphysics.domain.Domain) – The domain in which the points should be sampled.

  • n_points (int, optional) – The number of points that should be sampled.

  • density (float, optional) – The desired initial (and average) density of the created points, actual density will change loccally during iterations.

  • filter (callable, optional) – A function that restricts the possible positions of sample points. A point that is allowed should return True, therefore a point that should be removed must return False. The filter has to be able to work with a batch of inputs. The Sampler will use a rejection sampling to find the right amount of points.

sample_points(unreduced_loss=None, params=Points: {}, device='cpu')[source]

Extends the sample methode of the parent class. Also requieres the unreduced loss of the previous iteration to create the new points.

Parameters:
  • unreduced_loss (torch.tensor) – The tensor containing the loss of each training point in the previous iteration.

  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points

class torchphysics.problem.samplers.random_samplers.AdaptiveThresholdRejectionSampler(domain, resample_ratio, n_points=None, density=None, filter_fn=None)[source]

Bases: AdaptiveSampler

An adaptive sampler that creates more points in regions with high loss. During sampling, points with loss larger than ´min(loss)+resample_ratio*(max(loss)-min(loss))´ are kept for the next iteration, while points with small loss are regarded and resampled (random) uniformly in the whole domain.

Parameters:
  • domain (torchphysics.domain.Domain) – The domain in which the points should be sampled.

  • resample_ratio (float) – During sampling, points with loss larger than ´min(loss)+resample_ratio*(max(loss)-min(loss))´ are kept for the next iteration, while points with small loss are regarded and resampled (random) uniformly in the whole domain.

  • n_points (int, optional) – The number of points that should be sampled.

  • density (float, optional) – The desired initial (and average) density of the created points, actual density will change loccally during iterations.

  • filter (callable, optional) – A function that restricts the possible positions of sample points. A point that is allowed should return True, therefore a point that should be removed must return False. The filter has to be able to work with a batch of inputs. The Sampler will use a rejection sampling to find the right amount of points.

sample_points(unreduced_loss=None, params=Points: {}, device='cpu')[source]

Extends the sample methode of the parent class. Also requieres the unreduced loss of the previous iteration to create the new points.

Parameters:
  • unreduced_loss (torch.tensor) – The tensor containing the loss of each training point in the previous iteration.

  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points

class torchphysics.problem.samplers.random_samplers.GaussianSampler(domain, n_points, mean, std)[source]

Bases: PointSampler

Will sample normal/gaussian distributed points in the given domain. Only works for the inner part of a domain, not the boundary!

Parameters:
  • domain (torchphysics.domain.Domain) – The domain in which the points should be sampled.

  • n_points (int) – The number of points that should be sampled.

  • mean (list, array or tensor) – The center/mean of the distribution. Has to fit the dimension of the given domain.

  • std (number) – The standard deviation of the distribution.

class torchphysics.problem.samplers.random_samplers.LHSSampler(domain, n_points)[source]

Bases: PointSampler

Will create a simple latin hypercube sampling [#]_ in the given domain. Only works for the inner part of a domain, not the boundary!

Parameters:
  • domain (torchphysics.domain.Domain) – The domain in which the points should be sampled.

  • n_points (int) – The number of points that should be sampled.

Notes

A bounding box is used tp create the lhs-points in the domain. Points outside will be rejected and additional random uniform points will be added to get a total number of n_points. .. [#] https://en.wikipedia.org/wiki/Latin_hypercube_sampling

class torchphysics.problem.samplers.random_samplers.RandomUniformSampler(domain, n_points=None, density=None, filter_fn=None)[source]

Bases: PointSampler

Will sample random uniform distributed points in the given domain.

Parameters:
  • domain (torchphysics.domain.Domain) – The domain in which the points should be sampled.

  • n_points (int, optional) – The number of points that should be sampled.

  • density (float, optional) – The desiered density of the created points.

  • filter (callable, optional) – A function that restricts the possible positions of sample points. A point that is allowed should return True, therefore a point that should be removed must return False. The filter has to be able to work with a batch of inputs. The Sampler will use a rejection sampling to find the right amount of points.

torchphysics.problem.samplers.sampler_base module

The basic structure of every sampler and all sampler ‘operations’.

class torchphysics.problem.samplers.sampler_base.AdaptiveSampler(n_points=None, density=None, filter_fn=None)[source]

Bases: PointSampler

A sampler that requires a current loss for every point of the last sampled set of points.

sample_points(unreduced_loss, params=Points: {}, device='cpu')[source]

Extends the sample methode of the parent class. Also requieres the unreduced loss of the previous iteration to create the new points.

Parameters:
  • unreduced_loss (torch.tensor) – The tensor containing the loss of each training point in the previous iteration.

  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points

class torchphysics.problem.samplers.sampler_base.AppendSampler(sampler_a, sampler_b)[source]

Bases: PointSampler

A sampler that appends the output of two samplers behind each other. Essentially calling torch.coloumn_stack for the data points.

Parameters:
  • sampler_a (PointSampler) – The two PointSamplers that should be connected. Both Samplers should create the same number of points.

  • sampler_b (PointSampler) – The two PointSamplers that should be connected. Both Samplers should create the same number of points.

__len__()[source]

Returns the number of points that the sampler will create or has created.

Note

This can be only called if the number of points is set with n_points. Elsewise the the number can only be known after the first call to sample_points methode or may even change after each call. If you know the number of points yourself, you can set this with .set_length.

sample_points(params=Points: {}, device='cpu')[source]

The method that creates the points. Also implemented in all child classes.

Parameters:
  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points

class torchphysics.problem.samplers.sampler_base.ConcatSampler(sampler_a, sampler_b)[source]

Bases: PointSampler

A sampler that adds two single samplers together. Will concatenate the data points of both samplers.

Parameters:
  • sampler_a (PointSampler) – The two PointSamplers that should be connected.

  • sampler_b (PointSampler) – The two PointSamplers that should be connected.

__len__()[source]

Returns the number of points that the sampler will create or has created.

Note

This can be only called if the number of points is set with n_points. Elsewise the the number can only be known after the first call to sample_points methode or may even change after each call. If you know the number of points yourself, you can set this with .set_length.

sample_points(params=Points: {}, device='cpu')[source]

The method that creates the points. Also implemented in all child classes.

Parameters:
  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points

class torchphysics.problem.samplers.sampler_base.EmptySampler[source]

Bases: PointSampler

A sampler that creates only empty Points. Can be used as a placeholder.

sample_points(params=Points: {}, device='cpu', **kwargs)[source]

The method that creates the points. Also implemented in all child classes.

Parameters:
  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points

class torchphysics.problem.samplers.sampler_base.PointSampler(n_points=None, density=None, filter_fn=None)[source]

Bases: object

Handles the creation and interconnection of training/validation points.

Parameters:
  • n_points (int, optional) – The number of points that should be sampled.

  • density (float, optional) – The desired density of the created points.

  • filter_fn (callable, optional) – A function that restricts the possible positions of sample points. A point that is allowed should return True, therefore a point that should be removed must return false. The filter has to be able to work with a batch of inputs. The Sampler will use a rejection sampling to find the right amount of points.

__add__(other)[source]

Creates a sampler which samples from two different samples and concatenates both outputs, see ConcatSampler.

__iter__()[source]

Creates a iterator of this Pointsampler, with next the sample_points methode can be called.

__len__()[source]

Returns the number of points that the sampler will create or has created.

Note

This can be only called if the number of points is set with n_points. Elsewise the the number can only be known after the first call to sample_points methode or may even change after each call. If you know the number of points yourself, you can set this with .set_length.

__mul__(other)[source]

Creates a sampler that samples from the ‘Cartesian product’ of the samples of two samplers, see ProductSampler.

append(other)[source]

Creates a sampler which samples from two different samples and makes a column stack of both outputs, see AppendSampler.

classmethod empty(**kwargs)[source]

Creates an empty Sampler object that samples empty points.

Returns:

The empty sampler-object.

Return type:

EmptySampler

property is_adaptive

Checks if the Sampler is a AdaptiveSampler, e.g. samples points depending on the loss of the previous iteration.

property is_static

Checks if the Sampler is a StaticSampler, e.g. retuns always the same points.

make_static(resample_interval=inf)[source]

Transforms a sampler to an StaticSampler. A StaticSampler only creates points the first time .sample_points() is called. Afterwards the points are saved and will always be returned if .sample_points() is called again. Useful if the same points should be used while training/validation or if it is not practicall to create new points in each iteration (e.g. grid points).

Parameters:

resample_interval (int, optional) – Parameter to specify if new sampling of points should be created after a fixed number of iterations. E.g. resample_interval =5, will use the same points for five iterations and then sample a new batch that will be used for the next five iterations.

sample_points(params=Points: {}, device='cpu')[source]

The method that creates the points. Also implemented in all child classes.

Parameters:
  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points

set_length(length)[source]

If a density is used, the number of points will not be known before hand. If len(PointSampler) is needed one can set the expected number of points here.

Parameters:

length (int) – The expected number of points that this sampler will create.

Notes

If the domain is independent of other variables and a density is used, the sampler will, after the first call to ‘sample_points’, set this value itself.

class torchphysics.problem.samplers.sampler_base.ProductSampler(sampler_a, sampler_b)[source]

Bases: PointSampler

A sampler that constructs the product of two samplers. Will create a meshgrid (Cartesian product) of the data points of both samplers.

Parameters:
  • sampler_a (PointSampler) – The two PointSamplers that should be connected.

  • sampler_b (PointSampler) – The two PointSamplers that should be connected.

__len__()[source]

Returns the number of points that the sampler will create or has created.

Note

This can be only called if the number of points is set with n_points. Elsewise the the number can only be known after the first call to sample_points methode or may even change after each call. If you know the number of points yourself, you can set this with .set_length.

sample_points(params=Points: {}, device='cpu')[source]

The method that creates the points. Also implemented in all child classes.

Parameters:
  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points

class torchphysics.problem.samplers.sampler_base.StaticSampler(sampler, resample_interval=inf)[source]

Bases: PointSampler

Constructs a sampler that saves the first points created and afterwards only returns these points again. Has the advantage that the points only have to be computed once. Can also be customized to created new points after a fixed number of iterations.

Parameters:
  • sampler (Pointsampler) – The basic sampler that will create the points.

  • resample_interval (int, optional) – Parameter to specify if new sampling of points should be created after a fixed number of iterations. E.g. resample_interval =5, will use the same points for five iterations and then sample a new batch that will be used for the next five iterations.

__len__()[source]

Returns the number of points that the sampler will create or has created.

Note

This can be only called if the number of points is set with n_points. Elsewise the the number can only be known after the first call to sample_points methode or may even change after each call. If you know the number of points yourself, you can set this with .set_length.

make_static(resample_interval=inf)[source]

Transforms a sampler to an StaticSampler. A StaticSampler only creates points the first time .sample_points() is called. Afterwards the points are saved and will always be returned if .sample_points() is called again. Useful if the same points should be used while training/validation or if it is not practicall to create new points in each iteration (e.g. grid points).

Parameters:

resample_interval (int, optional) – Parameter to specify if new sampling of points should be created after a fixed number of iterations. E.g. resample_interval =5, will use the same points for five iterations and then sample a new batch that will be used for the next five iterations.

sample_points(params=Points: {}, device='cpu', **kwargs)[source]

The method that creates the points. Also implemented in all child classes.

Parameters:
  • params (torchphysics.spaces.Points) – Additional parameters for the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points-Object containing the created points and, if parameters were passed as an input, the parameters. Whereby the input parameters will get repeated, so that each row of the tensor corresponds to valid point in the given (product) domain.

Return type:

Points