I am implementi a RS , that works with logical rules. So the following two functions…
- User
I am implementi a RS , that works with logical rules. So the following two functions implements the AND ( conjunction ) and the NOT ( DISJUNCTION) OPERATION but they work with values in (-1, 1).
def conjunction_rule(self, u: torch.Tensor, i: torch.Tensor) -> torch.Tensor: """ Compute the conjunction (AND) function, extended to handle negative values.
Args: 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-40. Returns: torch.Tensor: Tensor of shape [batch_size], containing the conjunction scores. """ # Process variables to handle possible negations u_v, u_is_negated = self.process_variable(u) i_v, i_is_negated = self.process_variable(i) # Adjust variables based on negation u_v_adj = torch.where(u_is_negated, 1 - u_v, u_v) i_v_adj = torch.where(i_is_negated, 1 - i_v, i_v) # Compute the element-wise terms expr = 1 - u_v_adj * (1 - i_v_adj) + self.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(self, and_rules: torch.Tensor) -> torch.Tensor: """ Compute the disjunction (OR) function, extended to handle negative values.
Args: 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-40. Returns: torch.Tensor: Tensor of shape [batch_size], containing the disjunction scores. """ # Process variables to handle possible negations and_rules_v, and_rules_is_negated = self.process_variable(and_rules) # Adjust variables based on negation and_rules_v_adj = torch.where(and_rules_is_negated, 1 - and_rules_v, and_rules_v) # Compute the element-wise terms expr = 1 - and_rules_v_adj + self.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 resThe values in u tells what values in i to take into account in the and operation, for example: i'll explain better with an example with results, considering one user (u1) with two rules, each rules is of dim=4 and more items: u1 = [1, 1, 0, 0] U2 = [0, 0, 1, 0] U3 = [-1, 0, 1, 0] i1 = [0.9, 1, 0.2, 0] I2 = [-0.9, 1, 1, 0]
U1-i1: Will give as results 1 for the first u1-i1 i.e.: [1, 1, 0, 0] choose the first two embedding of i [0.9, 1, 0.2, 0], i.e = 0.9 AND 1 = 1 (almost)
U1-i1: for the second user u2 = [0, 0, 1, 0], will choose the third element of i1 [0.9, 1, 0.2, 0] , I.e. 0.2
U3-i2: Will give a result of 1
- Assistant