from typing import Optional, Tuple import torch from torch import Tensor class…
Private research annotation
Sign in to add a private research label or note.
- User
from typing import Optional, Tuple
import torch from torch import Tensor
class DenseMinCutPooling(torch.nn.Module): r"""The MinCut pooling operator from the "Spectral Clustering in Graph Neural Networks for Graph Pooling" https://arxiv.org/abs/1907.00481_ paper.
.. math:: \mathbf{X}^{\prime} &= {\mathrm{softmax}(\mathbf{S})}^{\top} \cdot \mathbf{X}
\mathbf{A}^{\prime} &= {\mathrm{softmax}(\mathbf{S})}^{\top} \cdot \mathbf{A} \cdot \mathrm{softmax}(\mathbf{S})
based on dense learned assignments :math:\mathbf{S} \in \mathbb{R}^{B \times N \times C}. Returns the pooled node feature matrix, the coarsened and symmetrically normalized adjacency matrix and two auxiliary objectives: (1) The MinCut loss
.. math:: \mathcal{L}_c = - \frac{\mathrm{Tr}(\mathbf{S}^{\top} \mathbf{A} \mathbf{S})} {\mathrm{Tr}(\mathbf{S}^{\top} \mathbf{D} \mathbf{S})}
where :math:\mathbf{D} is the degree matrix, and (2) the orthogonality loss
.. math:: \mathcal{L}_o = {\left| \frac{\mathbf{S}^{\top} \mathbf{S}} {{|\mathbf{S}^{\top} \mathbf{S}|}_F} -\frac{\mathbf{I}_C}{\sqrt{C}} \right|}_F.
Args: in_channels (int): Size of each input sample. out_channels (int): Number of output clusters. temp (float, optional): Temperature parameter for softmax function. (default: :obj:1.0) mlp_hidden_channels (int, optional): Number of hidden channels in the MLP. If not specified, a linear layer is used. (default: :obj:None) """ def init(self, in_channels: int, out_channels: int, temp: float = 1.0,): super().init()
self.in_channels = in_channels self.out_channels = out_channels self.temp = temp self.lin = torch.nn.Linear(in_channels, out_channels)
def forward( self, x: Tensor, edge_index: Tensor, mask: Optional[Tensor] = None, ) -> Tuple[Tensor, Tensor, Tensor, Tensor]: r""" Args: x (torch.Tensor): Node feature tensor :math:\mathbf{X} \in \mathbb{R}^{B \times N \times F}, with batch-size :math:B, (maximum) number of nodes :math:N for each graph, and feature dimension :math:F. edge_index (torch.Tensor): Edge index tensor :math:\mathbf{edge_index} \in \mathbb{R}^{2 \times E}. mask (torch.Tensor, optional): Mask matrix :math:\mathbf{M} \in {{ 0, 1 }}^{B \times N} indicating the valid nodes for each graph. (default: :obj:None)
:rtype: (:class:
torch.Tensor, :class:torch.Tensor, :class:torch.Tensor, :class:torch.Tensor) """ x = x.unsqueeze(0) if x.dim() == 2 else x (batch_size, num_nodes, _), k = x.size(), self.out_channelsadj = torch.zeros(batch_size, num_nodes, num_nodes, dtype=x.dtype, device=x.device) row, col = edge_index adj[torch.arange(batch_size).view(-1, 1, 1), row, col] = 1
s = self.lin(x) s = torch.softmax(s / self.temp if self.temp != 1.0 else s, dim=-1)
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.