|
| 1 | +from typing import Any, Dict, List, Optional, Union |
| 2 | + |
| 3 | +import torch |
| 4 | +from torch import Tensor |
| 5 | + |
| 6 | +from torch_geometric.nn.aggr import Aggregation, MultiAggregation |
| 7 | +from torch_geometric.utils import degree |
| 8 | + |
| 9 | + |
| 10 | +class DegreeScalerAggregation(Aggregation): |
| 11 | + """ |
| 12 | + Class that combines together one or more aggregators and then transforms |
| 13 | + the result with one or more scalers. The scalers are normalised by the |
| 14 | + in-degree of the training set and so must be provided at construction. |
| 15 | +
|
| 16 | + Args: |
| 17 | + aggr (string or list or Aggregation, optional): The list of |
| 18 | + aggregations given as :class:`~torch_geometric.nn.aggr.Aggregation` |
| 19 | + (or any string that automatically resolves to it). |
| 20 | + scalers (list of str): Set of scaling function identifiers, namely |
| 21 | + :obj:`"identity"`, :obj:`"amplification"`, |
| 22 | + :obj:`"attenuation"`, :obj:`"linear"` and |
| 23 | + :obj:`"inverse_linear"`. |
| 24 | + deg (Tensor): Histogram of in-degrees of nodes in the training set, |
| 25 | + used by scalers to normalize. |
| 26 | + aggr_kwargs (List[Dict[str, Any]], optional): Arguments passed to the |
| 27 | + respective aggregation functions in case it gets automatically |
| 28 | + resolved. (default: :obj:`None`) |
| 29 | + """ |
| 30 | + def __init__(self, aggrs: List[Union[Aggregation, str]], |
| 31 | + scalers: List[str], deg: Tensor, |
| 32 | + aggrs_kwargs: Optional[List[Dict[str, Any]]] = None): |
| 33 | + |
| 34 | + super().__init__() |
| 35 | + |
| 36 | + self.agg = MultiAggregation(aggrs, aggrs_kwargs) |
| 37 | + self.scalersz = scalers |
| 38 | + |
| 39 | + deg = deg.to(torch.float) |
| 40 | + num_nodes = int(deg.sum()) |
| 41 | + bin_degrees = torch.arange(deg.numel()) |
| 42 | + self.avg_deg: Dict[str, float] = { |
| 43 | + 'lin': float((bin_degrees * deg).sum()) / num_nodes, |
| 44 | + 'log': float(((bin_degrees + 1).log() * deg).sum()) / num_nodes, |
| 45 | + 'exp': float((bin_degrees.exp() * deg).sum()) / num_nodes, |
| 46 | + } |
| 47 | + |
| 48 | + def forward(self, x: Tensor, index: Optional[Tensor] = None, |
| 49 | + ptr: Optional[Tensor] = None, dim_size: Optional[int] = None, |
| 50 | + dim: int = -2) -> Tensor: |
| 51 | + |
| 52 | + out = self.agg(x, index, ptr, dim_size, dim) |
| 53 | + |
| 54 | + deg = degree(index, dtype=out.dtype) |
| 55 | + deg = deg.clamp_(1).view(-1, 1, 1) |
| 56 | + |
| 57 | + outs = [] |
| 58 | + for scaler in self.scalers: |
| 59 | + if scaler == 'identity': |
| 60 | + pass |
| 61 | + elif scaler == 'amplification': |
| 62 | + out = out * (torch.log(deg + 1) / self.avg_deg['log']) |
| 63 | + elif scaler == 'attenuation': |
| 64 | + out = out * (self.avg_deg['log'] / torch.log(deg + 1)) |
| 65 | + elif scaler == 'linear': |
| 66 | + out = out * (deg / self.avg_deg['lin']) |
| 67 | + elif scaler == 'inverse_linear': |
| 68 | + out = out * (self.avg_deg['lin'] / deg) |
| 69 | + else: |
| 70 | + raise ValueError(f'Unknown scaler "{scaler}".') |
| 71 | + outs.append(out) |
| 72 | + return torch.cat(outs, dim=-1) |
0 commit comments