Negative fuzzy v2 So i am implementing a RSs model in pytorch that works with embedding…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
Negative fuzzy v2
So i am implementing a RSs model in pytorch that works with embedding that represent values in fuzzy logic. The conjunction (AND) and disjunction (OR) are made as follows, but they work only with values in (0, 1). The selector ‘u’ is a tensor used to determine which values from the ‘selected’ array should be considered in an AND operation.
For example, if selector of dimension (1, 4) is equal to [[1, 0, 0, 1]], it means only the first and last elements of the "selected" array will be included in the operation. Given selected = [[1, 1, 1, 0]], applying the selector results in: 1 AND 0 = 0.
Other examples: users = torch.tensor([[1, 0, 0, 1], [0, 0, 1, 0], [1, 1, 1, 1]]) items = torch.tensor([[0, 1, 1, 0.2], [0, 0, 0, 1], [0.9, 1, 1, 1]]) res_ands = conjunction_rule(u=users, i=items)
Give the results: res_ands = tensor([0.0106, 0.0107, 0.9047]).
The functions are the following, and they are made to be differentiable and to avoid the vanishing gradient problem: def conjunction_rule(u: torch.Tensor, i: torch.Tensor, epsilon: float = 1e-40) -> torch.Tensor: """ Compute the conjunction (AND) function.
codeArgs: u (torch.Tensor): Tensor of shape [batch_size, embed_k], representing user embeddings. i (torch.Tensor): Tensor of shape [batch_size, embed_k], representing item embeddings. epsilon (float, optional): Small constant to prevent log(0). Defaults to 1e-12. Returns: torch.Tensor: Tensor of shape [batch_size], containing the conjunction scores. """ # Ensure u and i are in the expected range [0, 1] # u = torch.clamp(u, 0.0, 1.0) # i = torch.clamp(i, 0.0, 1.0) # Compute the element-wise terms expr = 1 - u * (1 - i) + epsilon # epsilon prevents log(0) # Take the logarithm of each term log_expr = torch.log(expr) # Sum over the embedding dimensions sum_log_expr = torch.sum(log_expr, dim=1) # Compute the final AND function value res = -1.0 / (-1.0 + sum_log_expr) return resdef disjunction_rule(and_rules: torch.Tensor, epsilon: float = 1e-40) -> torch.Tensor: """ Compute the disjunction (OR) function.
code - Assistant