Skip to content

Commit

Permalink
verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
eddie bell committed Aug 7, 2013
1 parent a09e4d9 commit 2e32d43
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
12 changes: 8 additions & 4 deletions pegasos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,28 @@ class PegasosSVMClassifier(SVMPegasosBase):
def __init__(self,
iterations=constants.DFLT_ITERATIONS,
lambda_reg=constants.DFLT_LAMBDA_REG,
loop_type=constants.LOOP_STOCHASTIC):
loop_type=constants.LOOP_STOCHASTIC,
verbose=0):

super(SVMPegasosBase, self).__init__(
iterations,
lambda_reg,
constants.LEARNER_PEGASOS_SVM,
loop_type)
loop_type,
verbose)


class PegasosLogisticRegression(LogisticPegasosBase):
def __init__(self,
iterations=constants.DFLT_ITERATIONS,
lambda_reg=constants.DFLT_LAMBDA_REG,
loop_type=constants.LOOP_STOCHASTIC):
loop_type=constants.LOOP_STOCHASTIC,
verbose=0):

super(PegasosLogisticRegression, self).__init__(
iterations,
lambda_reg,
constants.LEARNER_PEGASOS_LOGREG,
loop_type)
loop_type,
verbose)

5 changes: 4 additions & 1 deletion pegasos/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ def __init__(self,
iterations,
lambda_reg,
learner_type,
loop_type):
loop_type,
verbose):

self.iterations = iterations
self.lambda_reg = lambda_reg
self.loop_type = loop_type
self.learner_type = learner_type

self.verbose = verbose

self.weight_vector = None

def fit(self, X, y):
Expand Down
12 changes: 8 additions & 4 deletions pegasos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def __init__(self,
iterations,
lambda_reg,
learner_type,
loop_type):
loop_type,
verbose):

if learner_type != constants.LEARNER_PEGASOS_SVM:
raise ValueError('%s only supports SVM learners' % self.__class__.__name__)
Expand All @@ -39,7 +40,8 @@ def __init__(self,
iterations,
lambda_reg,
learner_type,
loop_type)
loop_type,
verbose)


class LogisticPegasosBase(PegasosBase):
Expand All @@ -50,7 +52,8 @@ def __init__(self,
iterations,
lambda_reg,
learner_type,
loop_type):
loop_type,
verbose):

if learner_type != constants.LEARNER_PEGASOS_LOGREG:
raise ValueError('%s only supports logistic learners' % self.__class__.__name__)
Expand All @@ -59,7 +62,8 @@ def __init__(self,
iterations,
lambda_reg,
learner_type,
loop_type)
loop_type,
verbose)

def predict_proba(self, X):
if not self.weight_vector:
Expand Down
6 changes: 6 additions & 0 deletions pegasos/pegasos.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def train_stochastic(model, X, y):
else:
raise ValueError('%s: unknown learner type' % model.loop_type)

if model.verbose > 1 or (model.verbose == 1 and iteration % 1000 == 0):
print 'train_stochastic: i=%d' % iteration

def train_stochastic_balanced(model, X, y):
"""
At each training step we sample a negative and positive
Expand Down Expand Up @@ -99,6 +102,9 @@ def train_stochastic_balanced(model, X, y):
else:
raise ValueError('%s: unknown learner type' % model.loop_type)

if model.verbose > 1 or (model.verbose == 1 and iteration % 1000 == 0):
print 'train_stochastic_balanced: i=%d' % iteration

def predict(model, X):
if sparse.issparse(X):
return np.array((model.weight_vector.weights * X.T).todense()).reshape(-1)
Expand Down

0 comments on commit 2e32d43

Please sign in to comment.