Reconstruction

Functions related to the reconstruction of series.

class ecgan.anomaly_detection.reconstruction.Reconstructor(reconstruction_cfg, module, **_kwargs)[source]

Bases: abc.ABC

Base class for different reconstruction strategies.

abstract reconstruct(x)[source]

Reconstruct the latent representation of a given Tensor.

Parameters

x (Tensor) -- A single data sample which is to be reconstructed.

Return type

Tensor

Returns

Reconstructed series.

class ecgan.anomaly_detection.reconstruction.InterpolationReconstructor(module, reconstruction_cfg, **_kwargs)[source]

Bases: ecgan.anomaly_detection.reconstruction.Reconstructor

Reconstruct samples based on the AnoGAN approach (Schlegl et al. 2017).

Optimize through latent space to search for a series similar to the input series.

Parameters
  • module (BaseGANModule) -- Generative module used for interpolation.

  • reconstruction_cfg (NestedDataclass) -- Config containing relevant parameters for latent walk.

reconstruct(x)[source]

Reconstruct the latent representation of a given Tensor.

Procedure:

  1. Randomly sample data \(z_0\) from the latent space of the model.

  2. Create a synthetic series \(G(z_0)\).

  3. Compare the similarity \(sim(x, G(z_0))\).

  4. Optimize through latent space to find \(z_1\) which generates a series \(G(z_1)\) which is more similar to \(x\) than \(G(z_0)\).

  5. Repeat until \(G(z_i)\) is similar enough, defined by: \(dissimilarity(x, G(z_i)) = 1-sim(x, G(z_i)) \leq \epsilon\).

Parameters

x (Tensor) -- The input data in shape (N x +) that shall be reconstructed.

Return type

Tensor

Returns

Reconstructed series.

adapt_learning_rate(loss, optimizer)[source]

Adapt the learning rate if the loss is below a previously set learning rate threshold.

Return type

None

class ecgan.anomaly_detection.reconstruction.InverseMappingReconstructor(module, reconstruction_cfg, **kwargs)[source]

Bases: ecgan.anomaly_detection.reconstruction.Reconstructor

Reconstruct the samples based on the ALAD approach by Zenati et al. 2018.

Learn an inverse mapping from the data space to the latent space to avoid the costly interpolation of AnoGAN. The mapping cans be learned during training (e.g. using an autoencoder based GAN or CycleGAN) or after training. If the mapping is not learned during training (i.e. if the module is not an instance of ecgan.modules.generative.base.BaseEncoderGANModule), we train this module during initialization of the inverse mapping reconstructor. This can take quite some time.

reconstruct(x)[source]

Reconstruct the latent representation of a given Tensor.

Procedure:
  1. Query inverse mapping for latent representation of \(z_0\).

  2. Create a synthetic series \(G(z_0)\).

Parameters

x (Tensor) -- The input data in shape \((N \times *)\) that shall be reconstructed.

Return type

Tensor

Returns

Reconstructed series.

class ecgan.anomaly_detection.reconstruction.ReconstructorFactory[source]

Bases: object

Factory module for creating ecgan.anomaly_detection.reconstruction.Reconstructor objects.