Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Nope it does not work, to be more precise: scorestensor has shape of [nrules,…

  1. 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]

  2. Assistant

    I see what you're aiming for. You have a tensor scores_tensor of shape [n_rules, num_users_in_the_batch, num_items], where each slice scores_tensor[i] contains the scores from rule i for 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 a final_scores tensor 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 the torch.max() function to perform this operation.

    Here's how you can modify your disjunction_rule_non_fuzzy method:

    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_scores
    

    Explanation:

    • torch.max(scores_tensor, dim=0) computes the maximum values along the specified dimension (dim=0 in this case).
    • It returns a tuple (values, indices), where values contains the maximum values, and indices contains the indices of the maximum values.
    • By specifying dim=0, you're collapsing the n_rules dimension by taking the maximum across all rules for each user-item pair.
    • The output final_scores will 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_fuzzy function:

    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_tensor is 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_scores
    

    This function directly computes the maximum values along a specified dimension.

    Final Integration:

    Make sure that when you call your disjunction_rule_non_fuzzy method, you pass the scores_tensor without 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!