Sampler

Implementation of a read-only sampling interface for module resources.

A sampler provides access to a module's resources such as its latent space, generator and discriminator. The usage is exemplified in the implementation of loss functions where samplers are used to compute the necessary gradients.

ecgan.utils.sampler.ones(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor

Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.

Parameters

size (int...) -- a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.

Keyword Arguments
  • out (Tensor, optional) -- the output tensor.

  • dtype (torch.dtype, optional) -- the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).

  • layout (torch.layout, optional) -- the desired layout of returned Tensor. Default: torch.strided.

  • device (torch.device, optional) -- the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) -- If autograd should record operations on the returned tensor. Default: False.

Example:

>>> torch.ones(2, 3)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])

>>> torch.ones(5)
tensor([ 1.,  1.,  1.,  1.,  1.])
class ecgan.utils.sampler.BaseSampler(component, dev, num_channels, sampling_seq_length)[source]

Bases: abc.ABC

Abstract sampler that provides read-only access to a component.

Examples are modules such as the generator or discriminator, or data and labels.

abstract sample(data)[source]

Sample from the component.

sample_label_ones(sampling_size)[source]

Return a tensor filled with ones based on the sampling size.

RNN-based GANs can rely on a label for each timestep in every series instead of only one label per series. If an RNN is used, the sampling_seq_length should be set to the sequence length, 1 otherwise during initialization.

Return type

Tensor

sample_label_zeros(sampling_size)[source]

Return a tensor filled with zeros based on the sampling size.

RNN-based GANs can rely on a label for each timestep in every series instead of only one label per series. If an RNN is used, the sampling_seq_length should be set to the sequence length, 1 otherwise during initialization.

Return type

Tensor

class ecgan.utils.sampler.DataSampler(component, dev, num_channels, seq_length, name, dataset=None)[source]

Bases: ecgan.utils.sampler.BaseSampler

Sampler for a (PyTorch) dataset.

sample(data)[source]

Sample data amount of data from the provided dataset.

Parameters

data (int) -- Amount of data to draw from the dataset.

Return type

Dict

Returns

Dict containing the sample values and labels.

sample_class(num_samples, class_label)[source]

Sample num_samples amount of samples belonging to a given class from the dataset.

Parameters
  • num_samples (int) -- Amount of data to draw from the dataset.

  • class_label (int) -- Class label.

Return type

Dict

Returns

Dict containing the sampled values and (non-zero) labels.

set_dataset(dataset)[source]

Change or set datasets to sample from.

Return type

None

get_dataset_size(class_label=None)[source]

Retrieve the amount of data in the dataset or of a specific class.

Parameters

class_label (Optional[int]) -- Optional class label.

Return type

int

Returns

Amount of samples in whole dataset if no class label is given, amount of samples in class otherwise.

class ecgan.utils.sampler.DiscriminatorSampler(component, dev, num_channels, sampling_seq_length)[source]

Bases: ecgan.utils.sampler.BaseSampler

Sampler for a classification model (e.g. a GAN discriminator) to retrieve the classification scores.

sample(data)[source]

Sample the classifier.

Note that a gradient for the component is computed. You can wrap the method in a torch.no_grad() block in order to stop the computation of the gradient.

Parameters

data (Tensor) -- Input tensor that shall be judged by the discriminator.

Return type

Tensor

Returns

Probability scores for the data being real.

class ecgan.utils.sampler.GeneratorSampler(component, dev, num_channels, sampling_seq_length, latent_space, latent_size)[source]

Bases: ecgan.utils.sampler.BaseSampler

Sampler for a generator.

Can be used to either sample noise from the latent space provided during initialization, or to generate data based on a noise sample.

sample(data)[source]

Sample the generator to synthesize data space of the training data.

The resulting data is a time series with a predefined sequence length and a specified amount of channels. Note that that a gradient for the component is computed. You can wrap the method in a torch.no_grad() block in order to stop the computation of the gradient.

Parameters

data (Tensor) -- Input noise for the generator.

Return type

Tensor

Returns

Samples in the training data space.

sample_z(sample_amount)[source]

Draw a sample from from the latent space.

Sample n vectors of noise. The dimensionality of the noise should be set during initialization. The sequence length is set to 1 (contrary to what some LSTM-based GANs do). The noise is expanded if the sampling_seq_length is larger.

Return type

Tensor

class ecgan.utils.sampler.FeatureDiscriminatorSampler(component, dev, num_channels, sampling_seq_length)[source]

Bases: ecgan.utils.sampler.DiscriminatorSampler

Sampler for a discriminator model which returns a discrimination score and features.

sample(data)[source]

Sample the classifier.

Note that a gradient for the component is computed. You can wrap the method in a torch.no_grad() block in order to stop the computation of the gradient.

Parameters

data (Tensor) -- Input tensor that shall be judged by the discriminator.

Return type

Tensor

Returns

Probability scores for the data being real.

sample_features(data)[source]

Sample the model for the features.

Parameters
  • data --

  • data -- Input tensor that shall be judged by the discriminator.

Return type

Tensor

Returns

Returns the features as returned by the model.

sample_score_features(data)[source]

Sample the model for the score and the features.

Parameters

data -- Input tensor that shall be judged by the discriminator.

Return type

Tuple[Tensor, Tensor]

Returns

Returns the score and the features as returned by the model.

class ecgan.utils.sampler.EncoderBasedGeneratorSampler(component, encoder, dev, num_channels, sampling_seq_length)[source]

Bases: ecgan.utils.sampler.GeneratorSampler

Generator sampler for encoder based modules.

Since these modules do not have a traditional latent distribution, calling sample_z will result in a NotImplementedError.

sample_encoder(data)[source]

Sample the encoder of the module.

Return type

Tensor

sample_generator_encoder(data)[source]

Return the result of the encoder and the generator.

Return type

Tuple[Tensor, Tensor]

sample_z(sample_amount)[source]

Raise NotImplementedError since the sampler has no latent distribution.

class ecgan.utils.sampler.VAEGANGeneratorSampler(component, encoder, dev, num_channels, sampling_seq_length, distribution)[source]

Bases: ecgan.utils.sampler.EncoderBasedGeneratorSampler

Generator sampler for the VAEGAN module.

sample_mu_logvar(data)[source]

Sample mu and log variance for a given sample from VAEGAN encoder.

Return type

Tuple[Tensor, Tensor]

sample_encoder(data)[source]

Return the noise of the encoder for the VAEGAN module.

Parameters

data (Tensor) -- Training data.

Return type

Tensor

Returns

Learned mean value mu with reparameterized std.

sample_eps(sample_shape)[source]

Sample epsilon for reparametrization from normal distribution.

Return type

Tensor

sample_pre_computed(mu, log_var)[source]

Sample the generator given a mean value and a log variance.

Return type

Tuple[Tensor, Tensor]

sample_z(sample_amount)[source]

Raise NotImplementedError since the sampler has no latent distribution.

class ecgan.utils.sampler.EncoderDecoderSampler(encoder, decoder, dev, num_channels, sampling_seq_length, latent_size)[source]

Bases: ecgan.utils.sampler.GeneratorSampler

Sampler for a Encoder/Decoder based model, without explicit latent distribution.

As of yet it remains unused. But it is meant for usage in encoder-based architectures such as BeatGAN. These architectures do not have a typical distribution which can be queried for latent space vectors but have to query the encoder model for a latent vector.

sample(data)[source]

Sample data by feeding input to encoder and then to decoder.

Return type

Tensor

sample_z(sample_amount)[source]

Sample the implicit latent space of the encoder.

Return type

Tensor

class ecgan.utils.sampler.LatentDistributionFactory[source]

Bases: object

Meta module for creating correct loss functions.