secmlt.data package

Submodules

secmlt.data.distributions module

Implementation for uncommon distributions.

class secmlt.data.distributions.Distribution[source]

Bases: ABC

Abstract class for distributions.

abstract sample(shape: torch.Size) torch.Tensor[source]

Sample from the distribution.

This method generates a sample from the distribution, with the specified shape. If no shape is specified, a single sample is returned.

Parameters:

shape (torch.Size, optional) – The shape of the sample to be generated. Default is torch.Size(), which corresponds to a single sample.

Returns:

A tensor of samples from the distribution, with the specified shape.

Return type:

torch.Tensor

class secmlt.data.distributions.GeneralizedNormal[source]

Bases: Distribution

Generalized normal distribution.

\[f(x; \mu, \alpha, \beta) = \frac{\beta}{2 \alpha \Gamma(1 / \beta)} e^{-(|x-\mu| / \alpha)^\beta}\]

where mu is the location parameter, alpha is the scale parameter, and beta is the shape parameter.

sample(shape: torch.Size, p: float = 2) torch.Tensor[source]

Sample from the generalized normal distribution.

This method generates a sample from the generalized normal distribution, with shape parameter p. The shape of the output is determined by the shape parameter.

Parameters:
  • shape (torch.Size) – The shape of the sample to be generated.

  • p (float, optional) – The shape parameter of the generalized normal distribution. Default is 2.

Returns:

A tensor of samples from the generalized normal distribution.

Return type:

torch.Tensor

Examples

>>> dist = GeneralizedNormal()
>>> sample = dist.sample((3, 4))
class secmlt.data.distributions.Rademacher[source]

Bases: Distribution

Samples from Rademacher distribution (-1, 1) with equal probability.

sample(shape: torch.Size) torch.Tensor[source]

Sample from the Rademacher distribution.

This method generates a sample from the Rademacher distribution, where each sample is either -1 or 1 with equal probability. The shape of the output is determined by the shape parameter.

Parameters:

shape (torch.Size) – The shape of the sample to be generated.

Returns:

A tensor of samples from the Rademacher distribution, with values -1 or 1.

Return type:

torch.Tensor

Examples

>>> dist = Rademacher()
>>> sample = dist.sample((3, 4))

secmlt.data.lp_uniform_sampling module

Implementation of Lp uniform sampling.

class secmlt.data.lp_uniform_sampling.LpUniformSampling(p: str = 'l2')[source]

Bases: object

Uniform sampling from the unit Lp ball.

This class provides a method for sampling uniformly from the unit Lp ball, where Lp is a norm defined by a parameter p. The class supports sampling from the L0, L2, and Linf norms.

The sampling method is based on the following reference: https://arxiv.org/abs/math/0503650

Variables:

p (str) – The norm to use for sampling. Must be one of ‘l0’, ‘l1’, ‘l2’, ‘linf’.

__init__(p: str = 'l2') None[source]

Initialize the LpUniformSampling object.

Parameters:

p (str, optional) – The norm to use for sampling. Must be one of ‘L0’, ‘L2’, or ‘Linf’. Default is ‘L2’.

sample(num_samples: int = 1, dim: int = 2) torch.Tensor[source]

Sample uniformly from the unit Lp ball.

This method generates a specified number of samples from the unit Lp ball, where Lp is a norm defined by the p parameter. The samples are generated using the algorithm described in the class documentation.

Parameters:
  • num_samples (int) – The number of samples to generate.

  • dim (int) – The dimension of the samples.

Returns:

A tensor of samples from the unit Lp ball, with shape (num_samples, dim).

Return type:

torch.Tensor

sample_like(x: torch.Tensor) torch.Tensor[source]

Sample from the unit Lp ball with the same shape as a given tensor.

Parameters:

x (torch.Tensor) – The input tensor whose shape is used to determine the shape of the samples.

Returns:

A tensor of samples from the unit Lp ball, with the same shape as the input tensor x.

Return type:

torch.Tensor

Module contents

Functionalities for handling data.