Skip to content

Commit

Permalink
TESTING: adding test for sequence_precision_recall evaluation metric
Browse files Browse the repository at this point in the history
  • Loading branch information
nikisix committed May 22, 2018
1 parent c70d909 commit 3077db7
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions tests/test_evaluation_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from spotlight.evaluation import precision_recall_score
from spotlight.evaluation import precision_recall_score, sequence_precision_recall_score
from spotlight.cross_validation import random_train_test_split
from spotlight.datasets import movielens
from spotlight.factorization.implicit import ImplicitFactorizationModel
Expand All @@ -14,7 +14,7 @@


@pytest.fixture(scope='module')
def data():
def data_implicit_factorization():

interactions = movielens.get_movielens_dataset('100K')

Expand All @@ -33,14 +33,57 @@ def data():
return train, test, model


@pytest.fixture(scope='module')
def data_implicit_sequence():

interactions = movielens.get_movielens_dataset('100K')

train, test = random_train_test_split(interactions,
random_state=RANDOM_STATE)

h = hyperparameters

model = ImplicitSequenceModel(loss='adaptive_hinge',
representation='lstm',
batch_size=[8, 16, 32, 256],
learning_rate=[1e-3, 1e-2, 5 * 1e-2, 1e-1],
l2=[1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 0.0],
n_iter=list(range(1, 2)),
use_cuda=CUDA,
random_state=random_state)

model.fit(train, verbose=True)

return train, test, model


@pytest.mark.parametrize('k', 10)
def test_precision_recall(data_implicit_sequence, k):

(train, test, model) = data_implicit_sequence

interactions = movielens.get_movielens_dataset('100K')
train, test = random_train_test_split(interactions,
random_state=RANDOM_STATE)

precision, recall = sequence_precision_recall_score(model, test, train, k=k)

assert precision.shape == recall.shape

if not isinstance(k, list):
assert len(precision.shape) == 1
else:
assert precision.shape[1] == len(k)


@pytest.mark.parametrize('k', [
1,
[1, 1],
[1, 1, 1]
])
def test_precision_recall(data, k):
def test_precision_recall(data_implicit_factorization, k):

(train, test, model) = data
(train, test, model) = data_implicit_factorization

interactions = movielens.get_movielens_dataset('100K')
train, test = random_train_test_split(interactions,
Expand Down

0 comments on commit 3077db7

Please sign in to comment.