So, here are two funcions in neuro symbolic. they are buil to implement logic AND and…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
So, here are two funcions in neuro symbolic. they are buil to implement logic AND and Not funciton , in a differentiable manner solving the vanishing gradient problem. Furthermore here it is implemented a technique to deal with negative embeddings, can you tell me the name of these techniques ? def process_variable(self, x: torch.Tensor)-> (torch.Tensor, torch.Tensor): """ Process the variable to handle possible negations.
codeArgs: x (torch.Tensor): Input tensor with values in [-1, 1]. Returns: x_v (torch.Tensor): Absolute value of x, representing the magnitude. x_is_negated (torch.Tensor): Boolean tensor indicating if x was negative. """ x_v = torch.abs(x) x_is_negated = x < 0 return torch.clamp(x_v, -1.0, 1-0), x_is_negateddef conjunction_rule(self, u: torch.Tensor, i: torch.Tensor) -> torch.Tensor: """ Compute the conjunction (AND) function, extended to handle negative values.
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-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 res - Assistant