so the following function realizes the and and the not operation, but they work with…
- User
so the following function realizes the and and the not operation, but they work with positive values in (0, 1). How can i extend them to work also with the negative values such that the negative value is considered as the not operation ?
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.
codeArgs: and_rules (torch.Tensor): Tensor of shape [batch_size, n_rules], containing conjunction scores. epsilon (float, optional): Small constant to prevent log(0). Defaults to 1e-12. Returns: torch.Tensor: Tensor of shape [batch_size], containing the disjunction scores. """ # Ensure and_rules are in the expected range [0, 1] # and_rules = torch.clamp(and_rules, 0.0, 1.0) # Compute the element-wise terms expr = 1 - and_rules + epsilon # epsilon prevents log(0) # Take the logarithm of each term log_expr = torch.log(expr) # Sum over the rules sum_log_expr = torch.sum(log_expr, dim=1) # Compute the final OR function value res = 1 - (-1.0 / (-1.0 + sum_log_expr)) return res - Assistant