def test_aegmm(aegmm_params): # OutlierAEGMM parameters threshold, n_gmm, w_energy, threshold_perc, return_instance_score = aegmm_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 aegmm = OutlierAEGMM(threshold=threshold, encoder_net=encoder_net, decoder_net=decoder_net, gmm_density_net=gmm_density_net, n_gmm=n_gmm) assert aegmm.threshold == threshold assert aegmm.meta == { 'name': 'OutlierAEGMM', 'detector_type': 'offline', 'data_type': None, 'version': __version__ } # fit OutlierAEGMM, infer threshold and compute scores aegmm.fit(X, w_energy=w_energy, epochs=5, batch_size=1000, verbose=False) aegmm.infer_threshold(X, threshold_perc=threshold_perc) energy = aegmm.score(X) perc_score = 100 * (energy < aegmm.threshold).astype(int).sum() / energy.shape[0] assert threshold_perc + 5 > perc_score > threshold_perc - 5 # make and check predictions od_preds = aegmm.predict(X, return_instance_score=return_instance_score) assert od_preds['meta'] == aegmm.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'] > aegmm.threshold).astype(int).sum() else: assert od_preds['data']['instance_score'] is None
def init_od_aegmm(state_dict: Dict, aegmm: tf.keras.Model) -> OutlierAEGMM: """ Initialize OutlierAEGMM. Parameters ---------- state_dict Dictionary containing the parameter values. aegmm Loaded AEGMM. Returns ------- Initialized OutlierAEGMM instance. """ od = OutlierAEGMM(threshold=state_dict['threshold'], aegmm=aegmm) 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 AEGMM detector has not been fit.') return od
threshold_net = tf.keras.Sequential([ InputLayer(input_shape=(seq_len, latent_dim)), Dense(5, activation=tf.nn.relu) ]) # define model inputs = tf.keras.Input(shape=(input_dim, )) outputs = tf.keras.layers.Dense(2, activation=tf.nn.softmax)(inputs) model = tf.keras.Model(inputs=inputs, outputs=outputs) 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,