Nope it does not work, to be more precise: scorestensor has shape of [nrules,…
- User
Nope it does not work, to be more precise: scores_tensor has shape of [n_rules, num_users_in_the_batch, num_items] therefore : scores_tensor[0] indicates the score obtained through rule 0 for each user in the batch wrt all the items, and it has shape of [num_user_in_the_batch, num_items]. The OR function, i.e. disjunction_rule_non_fuzzy, must put in OR all the rules score, such that, in the end, for each user-item pair it will take only tha maximum value along the rules. So the final_scores = self.disjunction_rule_non_fuzzy(scores_tensor) must have shape of [num_user_in_the_batch, num_items]
- Assistant
I see what you're aiming for. You have a tensor
scores_tensorof shape[n_rules, num_users_in_the_batch, num_items], where each slicescores_tensor[i]contains the scores from ruleifor all users and items. You want to apply an OR operation (which corresponds to taking the maximum in fuzzy logic) across all the rules for each user-item pair. The goal is to obtain afinal_scorestensor of shape[num_users_in_the_batch, num_items].To achieve this, you need to compute the element-wise maximum across the first dimension (
n_rules). In PyTorch, you can use thetorch.max()function to perform this operation.Here's how you can modify your
disjunction_rule_non_fuzzymethod:def disjunction_rule_non_fuzzy(self, scores_tensor): # Take the maximum over the first dimension (n_rules) # The result will have the shape [num_users_in_the_batch, num_items] final_scores, _ = torch.max(scores_tensor, dim=0) return final_scoresExplanation:
torch.max(scores_tensor, dim=0)computes the maximum values along the specified dimension (dim=0in this case).- It returns a tuple
(values, indices), wherevaluescontains the maximum values, andindicescontains the indices of the maximum values. - By specifying
dim=0, you're collapsing then_rulesdimension by taking the maximum across all rules for each user-item pair. - The output
final_scoreswill have the desired shape[num_users_in_the_batch, num_items].
Example:
Suppose you have the following
scores_tensor:# Example tensor with n_rules=3, num_users_in_the_batch=2, num_items=4 scores_tensor = torch.tensor([ [[0.1, 0.5, 0.3, 0.4], # Rule 0 scores for User 0 [0.2, 0.6, 0.1, 0.7]], # Rule 0 scores for User 1 [[0.3, 0.2, 0.5, 0.1], # Rule 1 scores for User 0 [0.4, 0.1, 0.2, 0.5]], # Rule 1 scores for User 1 [[0.2, 0.4, 0.6, 0.3], # Rule 2 scores for User 0 [0.5, 0.3, 0.4, 0.2]] # Rule 2 scores for User 1 ])Applying the
disjunction_rule_non_fuzzyfunction:final_scores, _ = torch.max(scores_tensor, dim=0) print(final_scores)Output:
tensor([[0.3, 0.5, 0.6, 0.4], [0.5, 0.6, 0.4, 0.7]])As you can see, for each user-item pair, the maximum score across all rules has been selected.
Note:
- Ensure that your
scores_tensoris a PyTorch tensor (torch.Tensor). - If you only need the maximum values and not the indices, you can ignore the second output of
torch.max().
Alternative Using
torch.amax:In newer versions of PyTorch, you can also use
torch.amax():def disjunction_rule_non_fuzzy(self, scores_tensor): final_scores = torch.amax(scores_tensor, dim=0) return final_scoresThis function directly computes the maximum values along a specified dimension.
Final Integration:
Make sure that when you call your
disjunction_rule_non_fuzzymethod, you pass thescores_tensorwithout modifying its shape, and the function will handle the rest.Let me know if this solves the issue or if you have any more questions!