From cd6184fbc5aa103f453b40fecc135b003b947a77 Mon Sep 17 00:00:00 2001 From: Quy Dinh Date: Mon, 12 Aug 2024 19:44:51 +0700 Subject: [PATCH] #641 Fix BiVAECF expected dense matrix --- cornac/models/bivaecf/bivae.py | 8 ++++---- tests/cornac/models/bivae/test_recommender.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 tests/cornac/models/bivae/test_recommender.py diff --git a/cornac/models/bivaecf/bivae.py b/cornac/models/bivaecf/bivae.py index a66f9d36f..9c237a204 100644 --- a/cornac/models/bivaecf/bivae.py +++ b/cornac/models/bivaecf/bivae.py @@ -198,7 +198,7 @@ def learn( i_count = 0 for i_ids in train_set.item_iter(batch_size, shuffle=False): i_batch = tx[i_ids, :] - i_batch = i_batch.A + i_batch = i_batch.todense().A i_batch = torch.tensor(i_batch, dtype=dtype, device=device) # Reconstructed batch @@ -228,7 +228,7 @@ def learn( u_count = 0 for u_ids in train_set.user_iter(batch_size, shuffle=False): u_batch = x[u_ids, :] - u_batch = u_batch.A + u_batch = u_batch.todense().A u_batch = torch.tensor(u_batch, dtype=dtype, device=device) # Reconstructed batch @@ -259,7 +259,7 @@ def learn( # infer mu_beta for i_ids in train_set.item_iter(batch_size, shuffle=False): i_batch = tx[i_ids, :] - i_batch = i_batch.A + i_batch = i_batch.todense().A i_batch = torch.tensor(i_batch, dtype=dtype, device=device) beta, _, i_mu, _ = bivae(i_batch, user=False, theta=bivae.theta) @@ -268,7 +268,7 @@ def learn( # infer mu_theta for u_ids in train_set.user_iter(batch_size, shuffle=False): u_batch = x[u_ids, :] - u_batch = u_batch.A + u_batch = u_batch.todense().A u_batch = torch.tensor(u_batch, dtype=dtype, device=device) theta, _, u_mu, _ = bivae(u_batch, user=True, beta=bivae.beta) diff --git a/tests/cornac/models/bivae/test_recommender.py b/tests/cornac/models/bivae/test_recommender.py new file mode 100644 index 000000000..d2dcb91fe --- /dev/null +++ b/tests/cornac/models/bivae/test_recommender.py @@ -0,0 +1,15 @@ +import unittest + +from cornac.data import Dataset, Reader +from cornac.models import BiVAECF + + +class TestRecommender(unittest.TestCase): + def setUp(self): + self.data = Reader().read("./tests/data.txt") + + def test_run(self): + bivae = BiVAECF(k=1, seed=123) + dataset = Dataset.from_uir(self.data) + # Assert runs without error + bivae.fit(dataset)