コード例 #1
0
ファイル: saving.py プロジェクト: yt114/alibi-detect
def init_od_vaegmm(state_dict: Dict, vaegmm: tf.keras.Model) -> OutlierVAEGMM:
    """
    Initialize OutlierVAEGMM.

    Parameters
    ----------
    state_dict
        Dictionary containing the parameter values.
    vaegmm
        Loaded VAEGMM.

    Returns
    -------
    Initialized OutlierVAEGMM instance.
    """
    od = OutlierVAEGMM(threshold=state_dict['threshold'],
                       vaegmm=vaegmm,
                       samples=state_dict['samples'])
    od.phi = state_dict['phi']
    od.mu = state_dict['mu']
    od.cov = state_dict['cov']
    od.L = state_dict['L']
    od.log_det_cov = state_dict['log_det_cov']

    if not all(
            tf.is_tensor(_)
            for _ in [od.phi, od.mu, od.cov, od.L, od.log_det_cov]):
        logger.warning('Loaded VAEGMM detector has not been fit.')

    return od
コード例 #2
0
detector = [
    AdversarialAE(threshold=threshold, model=model, **kwargs),
    IForest(threshold=threshold),
    Mahalanobis(threshold=threshold),
    OutlierAEGMM(threshold=threshold,
                 gmm_density_net=gmm_density_net,
                 n_gmm=n_gmm,
                 **kwargs),
    OutlierVAE(threshold=threshold,
               latent_dim=latent_dim,
               samples=samples,
               **kwargs),
    OutlierAE(threshold=threshold, **kwargs),
    OutlierVAEGMM(threshold=threshold,
                  gmm_density_net=gmm_density_net,
                  n_gmm=n_gmm,
                  latent_dim=latent_dim,
                  samples=samples,
                  **kwargs),
    OutlierProphet(threshold=.7, growth='logistic'),
    SpectralResidual(threshold=threshold, window_amp=10, window_local=10),
    OutlierSeq2Seq(input_dim,
                   seq_len,
                   threshold=threshold,
                   threshold_net=threshold_net,
                   latent_dim=latent_dim)
]
n_tests = len(detector)


@pytest.fixture
def select_detector(request):
コード例 #3
0
ファイル: test_vaegmm.py プロジェクト: yt114/alibi-detect
def test_vaegmm(vaegmm_params):
    # OutlierVAEGMM parameters
    threshold, n_gmm, w_energy, w_recon, samples, threshold_perc, return_instance_score = vaegmm_params

    # define encoder, decoder and GMM density net
    encoder_net = tf.keras.Sequential([
        InputLayer(input_shape=(input_dim, )),
        Dense(128, activation=tf.nn.relu),
        Dense(latent_dim, activation=None)
    ])

    decoder_net = tf.keras.Sequential([
        InputLayer(input_shape=(latent_dim, )),
        Dense(128, activation=tf.nn.relu),
        Dense(input_dim, activation=tf.nn.sigmoid)
    ])

    gmm_density_net = tf.keras.Sequential([
        InputLayer(input_shape=(latent_dim + 2, )),
        Dense(10, activation=tf.nn.relu),
        Dense(n_gmm, activation=tf.nn.softmax)
    ])

    # init OutlierAEGMM
    vaegmm = OutlierVAEGMM(threshold=threshold,
                           encoder_net=encoder_net,
                           decoder_net=decoder_net,
                           gmm_density_net=gmm_density_net,
                           n_gmm=n_gmm,
                           latent_dim=latent_dim,
                           samples=samples)

    assert vaegmm.threshold == threshold
    assert vaegmm.meta == {
        'name': 'OutlierVAEGMM',
        'detector_type': 'offline',
        'data_type': None
    }

    # fit OutlierAEGMM, infer threshold and compute scores
    vaegmm.fit(X,
               w_recon=w_recon,
               w_energy=w_energy,
               epochs=5,
               batch_size=1000,
               verbose=False)
    vaegmm.infer_threshold(X, threshold_perc=threshold_perc)
    energy = vaegmm.score(X)
    perc_score = 100 * (energy <
                        vaegmm.threshold).astype(int).sum() / energy.shape[0]
    assert threshold_perc + 5 > perc_score > threshold_perc - 5

    # make and check predictions
    od_preds = vaegmm.predict(X, return_instance_score=return_instance_score)
    assert od_preds['meta'] == vaegmm.meta
    assert od_preds['data']['is_outlier'].shape == (X.shape[0], )
    if return_instance_score:
        assert od_preds['data']['is_outlier'].sum() == (
            od_preds['data']['instance_score'] >
            vaegmm.threshold).astype(int).sum()
    else:
        assert od_preds['data']['instance_score'] is None