|
|
""" |
|
|
edge_conv.py includes edge_attr to edge_conv |
|
|
""" |
|
|
from typing import Callable, Optional, Union |
|
|
|
|
|
import torch |
|
|
from torch import Tensor |
|
|
from torch.nn import Linear |
|
|
from torch_geometric.nn.conv import MessagePassing |
|
|
from torch_geometric.nn.dense.linear import Linear |
|
|
from torch_geometric.typing import Adj, OptPairTensor, OptTensor, Size |
|
|
|
|
|
|
|
|
class EdgeConvConv(MessagePassing): |
|
|
def __init__(self, nn: Callable, eps: float = 0., train_eps: bool = False, |
|
|
edge_dim: Optional[int] = None, **kwargs): |
|
|
kwargs.setdefault('aggr', 'add') |
|
|
super().__init__(**kwargs) |
|
|
self.nn = nn |
|
|
self.initial_eps = eps |
|
|
if train_eps: |
|
|
self.eps = torch.nn.Parameter(torch.Tensor([eps])) |
|
|
else: |
|
|
self.register_buffer('eps', torch.Tensor([eps])) |
|
|
if edge_dim is not None: |
|
|
if hasattr(self.nn, 'in_features'): |
|
|
in_channels = self.nn.in_features |
|
|
else: |
|
|
in_channels = self.nn.in_channels |
|
|
self.lin = Linear(edge_dim, in_channels) |
|
|
else: |
|
|
self.lin = None |
|
|
|
|
|
def forward(self, x: Union[Tensor, OptPairTensor], edge_index: Adj, |
|
|
edge_attr: OptTensor = None, size: Size = None) -> Tensor: |
|
|
"""""" |
|
|
if isinstance(x, Tensor): |
|
|
x: OptPairTensor = (x, x) |
|
|
|
|
|
|
|
|
out = self.propagate(edge_index, x=x, edge_attr=edge_attr, size=size) |
|
|
|
|
|
return out |
|
|
|
|
|
def message(self, x_i: Tensor, x_j: Tensor, edge_attr: Tensor) -> Tensor: |
|
|
|
|
|
temp = torch.cat([x_i, x_j, edge_attr], dim=1) |
|
|
|
|
|
return self.nn(temp) |
|
|
|
|
|
def __repr__(self) -> str: |
|
|
return f'{self.__class__.__name__}(nn={self.nn})' |
|
|
|