From 7c7ff2141dee17f56227b19cc4548ef1e2c8efb3 Mon Sep 17 00:00:00 2001 From: hieuddo Date: Mon, 23 Oct 2023 13:53:06 +0000 Subject: [PATCH 1/4] fix BCELoss, reduction='sum' --- cornac/models/ncf/recom_ncf_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cornac/models/ncf/recom_ncf_base.py b/cornac/models/ncf/recom_ncf_base.py index 2541a4272..bec80e4a0 100644 --- a/cornac/models/ncf/recom_ncf_base.py +++ b/cornac/models/ncf/recom_ncf_base.py @@ -218,7 +218,7 @@ def _fit_pt(self): lr=self.lr, weight_decay=self.reg, ) - criteria = nn.BCELoss() + criteria = nn.BCELoss(reduction="sum") loop = trange(self.num_epochs, disable=not self.verbose) for _ in loop: From ebce5175806c9465e0375fd0a7c4cdf334838b1a Mon Sep 17 00:00:00 2001 From: hieuddo Date: Mon, 23 Oct 2023 16:55:58 +0000 Subject: [PATCH 2/4] count #batches instead of #users --- cornac/models/ncf/recom_ncf_base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cornac/models/ncf/recom_ncf_base.py b/cornac/models/ncf/recom_ncf_base.py index bec80e4a0..2dd876cd8 100644 --- a/cornac/models/ncf/recom_ncf_base.py +++ b/cornac/models/ncf/recom_ncf_base.py @@ -218,7 +218,7 @@ def _fit_pt(self): lr=self.lr, weight_decay=self.reg, ) - criteria = nn.BCELoss(reduction="sum") + criteria = nn.BCELoss() loop = trange(self.num_epochs, disable=not self.verbose) for _ in loop: @@ -241,7 +241,7 @@ def _fit_pt(self): loss.backward() optimizer.step() - count += len(batch_users) + count += 1 sum_loss += loss.data.item() if batch_id % 10 == 0: From 268d90767da4c45657adaaadbe938f33ddd55b04 Mon Sep 17 00:00:00 2001 From: Quoc-Tuan Truong Date: Mon, 23 Oct 2023 10:07:29 -0700 Subject: [PATCH 3/4] Update recom_ncf_base.py --- cornac/models/ncf/recom_ncf_base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cornac/models/ncf/recom_ncf_base.py b/cornac/models/ncf/recom_ncf_base.py index 2dd876cd8..f2dcbddb0 100644 --- a/cornac/models/ncf/recom_ncf_base.py +++ b/cornac/models/ncf/recom_ncf_base.py @@ -178,8 +178,8 @@ def _fit_tf(self): batch_users, batch_items, batch_ratings ), ) - count += len(batch_ratings) - sum_loss += _loss * len(batch_ratings) + count += 1 + sum_loss += _loss if i % 10 == 0: loop.set_postfix(loss=(sum_loss / count)) From caa4f43c8f159f23f7c0348d4364e48cf857e294 Mon Sep 17 00:00:00 2001 From: Quoc-Tuan Truong Date: Mon, 23 Oct 2023 10:16:19 -0700 Subject: [PATCH 4/4] Update recom_ncf_base.py --- cornac/models/ncf/recom_ncf_base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cornac/models/ncf/recom_ncf_base.py b/cornac/models/ncf/recom_ncf_base.py index f2dcbddb0..4f47877ea 100644 --- a/cornac/models/ncf/recom_ncf_base.py +++ b/cornac/models/ncf/recom_ncf_base.py @@ -178,8 +178,8 @@ def _fit_tf(self): batch_users, batch_items, batch_ratings ), ) - count += 1 - sum_loss += _loss + count += len(batch_users) + sum_loss += len(batch_users) * _loss if i % 10 == 0: loop.set_postfix(loss=(sum_loss / count)) @@ -241,8 +241,8 @@ def _fit_pt(self): loss.backward() optimizer.step() - count += 1 - sum_loss += loss.data.item() + count += len(batch_users) + sum_loss += len(batch_users) * loss.data.item() if batch_id % 10 == 0: loop.set_postfix(loss=(sum_loss / count))