yusanshi / news-recommendation

Implementations of some methods in news recommendation.
MIT License
241 stars 50 forks source link

fix dim err of candidate_news_vector in prediction #2

Closed thinkall closed 3 years ago

thinkall commented 3 years ago

The candidate_news_vector passed to attention class should be [batch_size, len(window_sizes) num_filters], but the current version has dimension [num_candidate_news, len(window_sizes) num_filters]. In prediction mode, the batch_size should be 1 as clicked_news_vector.unsqueeze(dim=0) has done.

yusanshi commented 3 years ago

Thanks to point out this mistake. I changed the code days ago but I haven't fully tested it.

However, I would prefer this as it should be faster.

--- a/src/model/DKN/__init__.py
+++ b/src/model/DKN/__init__.py
@@ -97,7 +97,7 @@ class DKN(torch.nn.Module):
         """
         # candidate_size, len(window_sizes) * num_filters
         user_vector = self.attention(candidate_news_vector,
-                                     clicked_news_vector.unsqueeze(dim=0))
+                                     clicked_news_vector.expand(candidate_news_vector.size(0), -1, -1))
         # candidate_size
         click_probability = self.click_predictor(candidate_news_vector,
                                                  user_vector)
thinkall commented 3 years ago

Ah, yes, thanks.

yusanshi commented 3 years ago

Already merged. Thanks again~