Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mo_gaal.py #558

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions pyod/models/mo_gaal.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class MO_GAAL(BaseDetector):
momentum : float, optional (default=0.9)
The momentum parameter for SGD.

verbose : int, optional (default=1)
Verbosity mode.
- 0 = silent
- 1 = show print

Attributes
----------
decision_scores_ : numpy array of shape (n_samples,)
Expand All @@ -89,13 +94,14 @@ class MO_GAAL(BaseDetector):
``threshold_`` on ``decision_scores_``.
"""

def __init__(self, k=10, stop_epochs=20, lr_d=0.01, lr_g=0.0001, momentum=0.9, contamination=0.1):
def __init__(self, k=10, stop_epochs=20, lr_d=0.01, lr_g=0.0001, momentum=0.9, contamination=0.1, verbose:int = 1):
super(MO_GAAL, self).__init__(contamination=contamination)
self.k = k
self.stop_epochs = stop_epochs
self.lr_d = lr_d
self.lr_g = lr_g
self.momentum = momentum
self.verbose = verbose

def fit(self, X, y=None):
"""Fit detector. y is ignored in unsupervised methods.
Expand Down Expand Up @@ -143,13 +149,15 @@ def fit(self, X, y=None):

# Start iteration
for epoch in range(epochs):
print('Epoch {} of {}'.format(epoch + 1, epochs))
if self.verbose > 0:
print('Epoch {} of {}'.format(epoch + 1, epochs))
batch_size = min(500, data_size)
num_batches = int(data_size / batch_size)

for index in range(num_batches):
print('\nTesting for epoch {} index {}:'.format(epoch + 1,
index + 1))
if self.verbose > 0:
print('\nTesting for epoch {} index {}:'.format(epoch + 1,
index + 1))

# Generate noise
noise_size = batch_size
Expand Down Expand Up @@ -197,7 +205,7 @@ def fit(self, X, y=None):
discriminator_loss)

# Get the target value of sub-generator
pred_scores = self.discriminator.predict(X).ravel()
pred_scores = self.discriminator.predict(X, verbose=self.verbose).ravel()

for i in range(self.k):
names['T' + str(i)] = np.percentile(pred_scores,
Expand All @@ -218,8 +226,10 @@ def fit(self, X, y=None):
else:
for i in range(self.k):
names['sub_generator' + str(i) + '_loss'] = names[
'combine_model' + str(i)].evaluate(noise, names[
'trick' + str(i)])
'combine_model' + str(i)].evaluate(noise,
names['trick' + str(i)],
verbose=self.verbose
)
self.train_history[
'sub_generator{}_loss'.format(i)].append(
names['sub_generator' + str(i) + '_loss'])
Expand All @@ -236,7 +246,7 @@ def fit(self, X, y=None):
stop = 1

# Detection result
self.decision_scores_ = self.discriminator.predict(X).ravel()
self.decision_scores_ = self.discriminator.predict(X, verbose=self.verbose).ravel()
self._process_decision_scores()
return self

Expand All @@ -260,5 +270,5 @@ def decision_function(self, X):
"""
check_is_fitted(self, ['discriminator'])
X = check_array(X)
pred_scores = self.discriminator.predict(X).ravel()
pred_scores = self.discriminator.predict(X, verbose=self.verbose).ravel()
return pred_scores