Source code for torch_geometric.transforms.local_degree_profile

import torch

from import Data
from import functional_transform
from torch_geometric.transforms import BaseTransform
from torch_geometric.utils import degree

[docs]@functional_transform('local_degree_profile') class LocalDegreeProfile(BaseTransform): r"""Appends the Local Degree Profile (LDP) from the `"A Simple yet Effective Baseline for Non-attribute Graph Classification" <>`_ paper (functional name: :obj:`local_degree_profile`). .. math:: \mathbf{x}_i = \mathbf{x}_i \, \Vert \, (\deg(i), \min(DN(i)), \max(DN(i)), \textrm{mean}(DN(i)), \textrm{std}(DN(i))) to the node features, where :math:`DN(i) = \{ \deg(j) \mid j \in \mathcal{N}(i) \}`. """ def __init__(self) -> None: from torch_geometric.nn.aggr.fused import FusedAggregation self.aggr = FusedAggregation(['min', 'max', 'mean', 'std']) def forward(self, data: Data) -> Data: assert data.edge_index is not None row, col = data.edge_index num_nodes = data.num_nodes deg = degree(row, num_nodes, dtype=torch.float).view(-1, 1) xs = [deg] + self.aggr(deg[col], row, dim_size=num_nodes) if data.x is not None: data.x = data.x.view(-1, 1) if data.x.dim() == 1 else data.x data.x =[data.x] + xs, dim=-1) else: data.x =, dim=-1) return data