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 so_gaal.py #560

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
22 changes: 15 additions & 7 deletions pyod/models/so_gaal.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class SO_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 @@ -85,12 +90,13 @@ class SO_GAAL(BaseDetector):
``threshold_`` on ``decision_scores_``.
"""

def __init__(self, stop_epochs=20, lr_d=0.01, lr_g=0.0001, momentum=0.9, contamination=0.1):
def __init__(self, stop_epochs=20, lr_d=0.01, lr_g=0.0001, momentum=0.9, contamination=0.1, verbose: int = 1):
super(SO_GAAL, self).__init__(contamination=contamination)
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 @@ -131,13 +137,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 @@ -166,15 +174,15 @@ def fit(self, X, y=None):
self.train_history['generator_loss'].append(generator_loss)
else:
trick = np.array([1] * noise_size)
generator_loss = self.combine_model.evaluate(noise, trick)
generator_loss = self.combine_model.evaluate(noise, trick, verbose=self.verbose)
self.train_history['generator_loss'].append(generator_loss)

# Stop training generator
if epoch + 1 > self.stop_epochs:
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 @@ -198,5 +206,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