pyod icon indicating copy to clipboard operation
pyod copied to clipboard

When k is too large, MO_GAAL cannot operate normally

Open Matt-Wang88 opened this issue 3 years ago • 3 comments

Description

MO_GAAL can not work when k (number of sub generators) larget than some constant.

Steps/Code to Reproduce

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from pyod.models.mo_gaal import MO_GAAL

# data
X, y = load_iris(return_X_y=True)
data_set_1 = X[y == 1][:, 1:3]
data_set_2 = X[y == 0][-4:, 1:3]
X_train = np.vstack([data_set_1, data_set_2])


plt.plot(data_set_1[:, 0], data_set_1[:, 1], "bo")
plt.plot(data_set_2[:, 0], data_set_2[:, 1], "ro")
plt.show()

# Model work with k < 10
model = MO_GAAL(k=9)
model.fit(X=X_train)
model.labels_

plt.plot(X_train[model.labels_ == 0][:, 0],
         X_train[model.labels_ == 0][:, 1], "bo")
plt.plot(X_train[model.labels_ == 1][:, 0],
         X_train[model.labels_ == 1][:, 1], "ro")
plt.show()

# Model unwork with k >= 10
model2 = MO_GAAL(k=10)
model2.fit(X=X_train)
model2.labels_

Actual Behavior

Epoch 1 of 60

Testing for epoch 1 index 1:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
D:\python3_6_8_64\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in _truncate_execution_to_epoch(self)
   1155       if should_truncate:
-> 1156         self._steps_per_execution.assign(self._inferred_steps)
   1157         self._steps_per_execution_value = self._inferred_steps

AttributeError: 'int' object has no attribute 'assign'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
c:\Users\Matt\Desktop\test_pyod\test_mo_gaal.py in 
      29 
      30 model2 = MO_GAAL(k=10)
----> 31 model2.fit(X=X_train)
      32 model2.labels_

D:\python3_6_8_64\lib\site-packages\pyod\models\mo_gaal.py in fit(self, X, y)
    168                         names['generated_data' + str(i)] = names[
    169                             'sub_generator' + str(i)].predict(
--> 170                             names['noise' + str(i)], verbose=0)
    171                     else:
    172                         noise_start = int(

D:\python3_6_8_64\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
    128       raise ValueError('{} is not supported in multi-worker mode.'.format(
    129           method.__name__))
--> 130     return method(self, *args, **kwargs)
    131 
    132   return tf_decorator.make_decorator(

D:\python3_6_8_64\lib\site-packages\tensorflow\python\keras\engine\training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1593       self._predict_counter.assign(0)
   1594       callbacks.on_predict_begin()
-> 1595       for _, iterator in data_handler.enumerate_epochs():  # Single epoch.
   1596         with data_handler.catch_stop_iteration():
   1597           for step in data_handler.steps():

D:\python3_6_8_64\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in enumerate_epochs(self)
   1135   def enumerate_epochs(self):
   1136     """Yields `(epoch, tf.data.Iterator)`."""
-> 1137     with self._truncate_execution_to_epoch():
   1138       data_iterator = iter(self._dataset)
   1139       for epoch in range(self._initial_epoch, self._epochs):

D:\python3_6_8_64\lib\contextlib.py in __enter__(self)
     79     def __enter__(self):
     80         try:
---> 81             return next(self.gen)
     82         except StopIteration:
     83             raise RuntimeError("generator didn't yield") from None

D:\python3_6_8_64\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in _truncate_execution_to_epoch(self)
   1159     finally:
   1160       if should_truncate:
-> 1161         self._steps_per_execution.assign(original_value)
   1162         self._steps_per_execution_value = original_value
   1163 

AttributeError: 'int' object has no attribute 'assign'

Operating System

Windows 10

Python Version

Python 3.6.8

pyod Version (Use : python -m pip show pyod)

0.8.3

Matt-Wang88 avatar Dec 08 '20 06:12 Matt-Wang88

Hi, after I compiled the information, I found something new. I would like to ask you this idea is correct. The parameter "k" may have range limits. In the program line 156 when "noise_size" <"block", "noise_start" and "noise_end" will lose their function. To avoid this situation, "noise_size" must larger than "block".

image

Matt-Wang88 avatar Dec 10 '20 07:12 Matt-Wang88

Sorry for being late on this. MO and SO GAAL is adapted from their original implementation. PR is welcomed!

yzhao062 avatar Jan 25 '21 16:01 yzhao062

Same issue.

abhishek-km23 avatar Aug 23 '21 14:08 abhishek-km23