Read the following text with equations:
As pointed out by the authors in \cite{DBLP:journals/corr/abs-1904-01554} due to the multiplicative design of these layers, when many of the
weight in wj have values which are not zero or one,
the gradient can becomes extremely small leading to the vanishing gradient problem.
To this end we leverage (or adopt) the solution proposed in \cite{DBLP:conf/nips/WangZLW21} and redefine the conjunction and disjunction rules as follows:
\begin{align}
c_{uj}(\mathbf{e}i) &= \mathbb{P} \left ( (\prod{l = 1}^k 1 - \mathbf{w}_j [l] ( 1 - \mathbf{e}i[l])) + \epsilon \right) \
d{uj}(\mathbf{e}i) &= 1 - \mathbb{P} \left (\prod{l = 1}^k 1 - ( \mathbf{w}_j [l] \mathbf{e}_i [l] ) + \epsilon \right )
\end{align}
Where ϵ is a small constant, e.g., 10, and is defined as:
\begin{equation}
\mathbb{P} (a) = \frac{-1}{-1 + log(a)}
\end{equation}
Assistant
−10
P
Can you expand them to avoid multiplication ? and show me an implementation in pytorch
To address the vanishing gradient problem caused by multiplicative operations in the conjunction and disjunction rules, you can expand the product terms into sums and higher-order interactions. This approach transforms multiplicative dependencies into additive ones, which generally preserve gradient magnitudes better during backpropagation.
Below, I'll first expand the original expressions to avoid explicit multiplications and then provide a PyTorch implementation based on the expanded forms.
Expanded Expressions Without Multiplication
Original Expressions
The original conjunction and disjunction rules are defined as:
(\mathbf{w}_j) is the weight vector for the (j)-th conjunctive/disjunctive rule.
(\mathbf{e}_i) is the input vector.
(\epsilon) is a small constant (e.g., (10^{-10})).
(\mathbb{P}) is defined as:
[
\mathbb{P}(a) = \frac{-1}{-1 + \log(a)}
]
Expanding the Products
To eliminate the product terms, you can approximate the products using a First-Order Taylor Expansion, which retains only the linear terms and ignores higher-order interactions. This is a common approximation to simplify multiplicative dependencies.
This approximation eliminates the multiplicative interactions by replacing the product with a sum.
PyTorch Implementation
Below is a PyTorch implementation based on the expanded expressions without explicit multiplications. This implementation assumes that weights is a tensor of shape (num_rules, k), and inputs is a tensor of shape (batch_size, k).
python
import torch
import torch.nn as nn
class ExpandedConjunctionDisjunction(nn.Module):
def __init__(self, num_rules, k, epsilon=1e-10):
super(ExpandedConjunctionDisjunction, self).__init__()
self.num_rules = num_rules
self.k = k
self.epsilon = epsilon
# Initialize weights with requires_grad=True for learning
self.weights = nn.Parameter(torch.randn(num_rules, k))
def P(self, a):
# To ensure numerical stability, clamp 'a' away from 0 and 1
a = torch.clamp(a, min=1e-9, max=1 - 1e-9)
return -1.0 / (-1.0 + torch.log(a))
def forward(self, inputs):
'''
inputs: Tensor of shape (batch_size, k)
'''
# Ensure inputs are in the range [0,1]
inputs = torch.sigmoid(inputs) # If not already in [0,1]
# Compute conjunction: c_{uj}(e_i)
# c_{uj}(e_i) ≈ P(1 - sum(w_j[l] * (1 - e_i[l])) + epsilon)
term_c = 1 - torch.matmul(self.weights, (1 - inputs).transpose(0,1)) # Shape: (num_rules, batch_size)
term_c = term_c + self.epsilon
p_c = self.P(term_c)
c = p_c.transpose(0,1) # Shape: (batch_size, num_rules)
# Compute disjunction: d_{uj}(e_i)
# d_{uj}(e_i) ≈ 1 - P(1 - sum(w_j[l] * e_i[l]) + epsilon)
term_d = 1 - torch.matmul(self.weights, inputs.transpose(0,1)) # Shape: (num_rules, batch_size)
term_d = term_d + self.epsilon
p_d = self.P(term_d)
d = 1 - p_d.transpose(0,1) # Shape: (batch_size, num_rules)
return c, d
# Example Usage
if __name__ == "__main__":
batch_size = 4
k = 5
num_rules = 3
# Instantiate the module
model = ExpandedConjunctionDisjunction(num_rules=num_rules, k=k)
# Sample input
inputs = torch.randn(batch_size, k) # Raw inputs; sigmoid will squash to [0,1]
# Forward pass
c, d = model(inputs)
print("Conjunction Outputs (c):")
print(c)
print("\nDisjunction Outputs (d):")
print(d)
Explanation of the Implementation
Module Definition:
ExpandedConjunctionDisjunction is a custom nn.Module that encapsulates the expanded conjunction and disjunction rules.
It initializes a weight matrix self.weights of shape (num_rules, k), which will be learned during training.
P Function:
The function P(a) is implemented as defined.
To maintain numerical stability and avoid taking the logarithm of non-positive numbers, the input a is clamped to a small range away from 0 and 1.
Forward Pass:
Inputs: The inputs are first passed through a sigmoid activation to ensure they're within the range ([0, 1]). If your inputs are already within this range, you can omit this step.
Conjunction (c):
Computes (1 - \sum_{l=1}^k w_j[l] \cdot (1 - e_i[l])) for each rule and each input in the batch.
Adds a small constant (\epsilon) for numerical stability.
Applies the function (P) to the result.
Disjunction (d):
Computes (1 - \sum_{l=1}^k w_j[l] \cdot e_i[l]) for each rule and each input in the batch.
Adds a small constant (\epsilon) for numerical stability.
Applies the function (P) and then subtracts the result from 1, as per the disjunction rule.
Matrix Multiplications:
The torch.matmul function efficiently computes the sums across the (k) dimensions for all rules and inputs simultaneously without explicit loops.
Outputs:
c and d are tensors of shape (batch_size, num_rules) representing the conjunction and disjunction outputs, respectively.
Notes and Considerations
Higher-Order Approximations: The first-order approximation ignores higher-order interaction terms (e.g., products of two or more (a_l) terms). If higher accuracy is needed and computational resources permit, you can include second-order or higher terms in the expansion. However, this will involve additional computations and complexity.
Numerical Stability: The small constant (\epsilon) ensures that the arguments to the logarithm in the (P) function remain positive and sufficiently away from zero to prevent computational issues.
Learning Dynamics: By replacing multiplicative interactions with additive ones, you may alter the expressiveness of your model. Ensure that this approximation aligns with your application's requirements.
Activation Functions: Adjust the activation functions based on the range and distribution of your input data to ensure that the approximations hold effectively.
Conclusion
By expanding the product terms into sums, you can mitigate issues related to vanishing gradients caused by multiplicative operations in neural network layers. The provided PyTorch implementation demonstrates how to apply this expansion in practice, ensuring that the model remains trainable and effective.