Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gbolmier committed Jan 26, 2021
1 parent 9a11586 commit ad88783
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Run `pip install git+https://github.com/gbolmier/funk-svd` in your terminal.
>>> val = df.drop(train.index.tolist()).sample(frac=0.5, random_state=8)
>>> test = df.drop(train.index.tolist()).drop(val.index.tolist())

>>> svd = SVD(lr=0.001, reg=0.005, n_epochs=100, n_factors=15, min_rating=1,
... max_rating=5)
>>> svd = SVD(lr=0.001, reg=0.005, n_epochs=100, n_factors=15,
... early_stopping=True, shuffle=False, min_rating=1, max_rating=5)

>>> svd.fit(X=train, X_val=val, early_stopping=True, shuffle=False)
>>> svd.fit(X=train, X_val=val)
Preprocessing data...

Epoch 1/...
Expand Down
15 changes: 10 additions & 5 deletions funk_svd/svd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pandas as pd
import time

from .fast_methods import _compute_val_metrics
Expand Down Expand Up @@ -143,6 +144,10 @@ def _preprocess_data(self, X, train=True):

return X[['u_id', 'i_id', 'rating']].values

def _init_metrics(self):
metrics = np.zeros((self.n_epochs, 3), dtype=np.float)
self.metrics_ = pd.DataFrame(metrics, columns=['Loss', 'RMSE', 'MAE'])

def _run_sgd(self, X, X_val):
"""Runs SGD algorithm, learning model weights.
Expand All @@ -163,15 +168,15 @@ def _run_sgd(self, X, X_val):
for epoch_ix in range(self.n_epochs):
start = self._on_epoch_begin(epoch_ix)

if self.shuffle_:
if self.shuffle:
X = _shuffle(X)

pu, qi, bu, bi = _run_epoch(X, bu, bi, pu, qi, self.global_mean_,
bu, bi, pu, qi = _run_epoch(X, bu, bi, pu, qi, self.global_mean_,
self.n_factors, self.lr, self.reg)

if X_val is not None:
self.metrics_.loc[epoch_ix, :] = _compute_val_metrics(
X_val, pu, qi, bu, bi,
X_val, bu, bi, pu, qi,
self.global_mean_,
self.n_factors
)
Expand All @@ -180,10 +185,10 @@ def _run_sgd(self, X, X_val):
self.metrics_.loc[epoch_ix, 'RMSE'],
self.metrics_.loc[epoch_ix, 'MAE'])

if self.early_stopping_:
if self.early_stopping:
val_rmse = self.metrics_['RMSE'].tolist()
if self._early_stopping(val_rmse, epoch_ix,
self.min_delta_):
self.min_delta):
break

else:
Expand Down
4 changes: 2 additions & 2 deletions funk_svd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ def _timer(text=''):
Examples
--------
>>> @timer(text='Greetings took ')
>>> @_timer(text='Greetings took ')
... def say_hi():
... time.sleep(1)
... print('Hey! What's up!')
... print("Hey! What's up!")
...
>>> say_hi()
Hey! What's up!
Expand Down
6 changes: 3 additions & 3 deletions run_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
val = df.drop(train.index.tolist()).sample(frac=0.5, random_state=8)
test = df.drop(train.index.tolist()).drop(val.index.tolist())

svd = SVD(lr=0.001, reg=0.005, n_epochs=100, n_factors=15, min_rating=1,
max_rating=5)
svd = SVD(lr=0.001, reg=0.005, n_epochs=100, n_factors=15, early_stopping=True,
shuffle=False, min_rating=1, max_rating=5)

svd.fit(X=train, X_val=val, early_stopping=True, shuffle=False)
svd.fit(X=train, X_val=val)

pred = svd.predict(test)
mae = mean_absolute_error(test['rating'], pred)
Expand Down

0 comments on commit ad88783

Please sign in to comment.