Anomaly Assessment

Functions and classes which are commonly used to calculate anomaly scores.

class ecgan.anomaly_detection.anomaly_assessment.InferenceDiscriminator(disc_sampler)[source]

Bases: abc.ABC

Base class for different discrimination inference strategies.

Note

This base class is intended to be used for anomaly detection. It is intended to help deciding if some data is drawn from the data distribution by e.g. using the direct output of an already trained GAN discriminator but also by comparing the activations between real and synthetic data to evaluate if they are similar.

Generally, this type of Discriminator is used to calculate a score which can be used to asses if data belongs to a given distribution. In this class we utilize only neural network based discriminators inspired by GAN architectures and AnoGAN. This discriminator can not be used for training.

abstract discriminate(data, **kwargs)[source]

Discriminate a Tensor.

Parameters

data (Tensor) -- A data sample that should be discriminated.

Return type

Tensor

Returns

Discrimination score.

class ecgan.anomaly_detection.anomaly_assessment.RawInferenceDiscriminator(disc_sampler)[source]

Bases: ecgan.anomaly_detection.anomaly_assessment.InferenceDiscriminator

Return the output of the discriminator.

discriminate(data, **kwargs)[source]

Calculate the raw disc output and return the difference from the target value (1).

The output depends a lot on the training state of the discriminator and is not necessarily reliable.

Return type

Tensor

class ecgan.anomaly_detection.anomaly_assessment.LogInferenceDiscriminator(disc_sampler)[source]

Bases: ecgan.anomaly_detection.anomaly_assessment.InferenceDiscriminator

Return the logarithmic output of the discriminator.

discriminate(data, **kwargs)[source]

Return the log of the disc output.

The output depends a lot on the training state of the discriminator and is not necessarily reliable.

Return type

Tensor

class ecgan.anomaly_detection.anomaly_assessment.FeatureMatchingInferenceDiscriminator(disc_sampler)[source]

Bases: ecgan.anomaly_detection.anomaly_assessment.InferenceDiscriminator

Match the features of the real and reconstructed series.

discriminate(data, **kwargs)[source]

Check if the features of real and fake data are similar.

The similarity is measured by the MSELoss/L2 distance between the real data and the reconstructed data. This also means that, in comparison to many other discriminators, we do not only need to reconstructed samples but the real samples as well.

Return type

Tensor

class ecgan.anomaly_detection.anomaly_assessment.DiscriminatorFactory[source]

Bases: object

Factory module for creating Discriminator objects.

ecgan.anomaly_detection.anomaly_assessment.get_pointwise_anomaly_score_anogan(discrimination_error, reconstruction_error, lambda_=0.1)[source]

Calculate the series-wise anomaly scores.

Calculate the series-wise anomaly scores including all channel- and pointwise anomaly scores. Currently based on the AnoGAN approach (\(lambda\) is the weighting between discrimination error and reconstruction error).

Parameters
  • discrimination_error (Tensor) -- Pointwise discrimination error.

  • reconstruction_error (Tensor) -- Pointwise reconstruction error.

  • lambda -- Weighting of the reconstruction error (the higher lambda, the less relevant the discrimination_error. The weight is assumed to be in [0, 1].

Return type

Tensor

Returns

The pointwise anomaly score.

ecgan.anomaly_detection.anomaly_assessment.get_anomaly_score(discrimination_error, reconstruction_error, pointwise=True, lambda_=0.1)[source]

Return an anomaly score.

Anomaly score:

  1. Default: all points for each channel.

  2. All points inside one series summarized.

Parameters
  • discrimination_error (Tensor) -- Pointwise discrimination error.

  • reconstruction_error (Tensor) -- Pointwise reconstruction error.

  • pointwise (bool) -- Signals that the pointwise anomaly score should be returned. If pointwise=False: Return serieswise anomaly score.

  • lambda -- Weighting of the reconstruction error.

Return type

Tensor

Returns

The pointwise or channelwise reconstruction error for each time series.