Skip to content

Commit

Permalink
allow 1D input to global_*_pool functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajdamo committed Jan 24, 2023
1 parent c3be6b0 commit 076d667
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions torch_geometric/nn/pool/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ def global_add_pool(x: Tensor, batch: Optional[Tensor],
size (int, optional): The number of examples :math:`B`.
Automatically calculated if not given. (default: :obj:`None`)
"""
dim = -1 if x.dim() == 1 else -2

if batch is None:
return x.sum(dim=-2, keepdim=x.dim() == 2)
return x.sum(dim=dim, keepdim=x.dim() == 2 or x.dim() == 1)
size = int(batch.max().item() + 1) if size is None else size
return scatter(x, batch, dim=-2, dim_size=size, reduce='sum')
return scatter(x, batch, dim=dim, dim_size=size, reduce='sum')


def global_mean_pool(x: Tensor, batch: Optional[Tensor],
Expand All @@ -53,10 +55,12 @@ def global_mean_pool(x: Tensor, batch: Optional[Tensor],
size (int, optional): The number of examples :math:`B`.
Automatically calculated if not given. (default: :obj:`None`)
"""
dim = -1 if x.dim() == 1 else -2

if batch is None:
return x.mean(dim=-2, keepdim=x.dim() == 2)
return x.mean(dim=dim, keepdim=x.dim() == 2 or x.dim() == 1)
size = int(batch.max().item() + 1) if size is None else size
return scatter(x, batch, dim=-2, dim_size=size, reduce='mean')
return scatter(x, batch, dim=dim, dim_size=size, reduce='mean')


def global_max_pool(x: Tensor, batch: Optional[Tensor],
Expand All @@ -80,7 +84,9 @@ def global_max_pool(x: Tensor, batch: Optional[Tensor],
size (int, optional): The number of examples :math:`B`.
Automatically calculated if not given. (default: :obj:`None`)
"""
dim = -1 if x.dim() == 1 else -2

if batch is None:
return x.max(dim=-2, keepdim=x.dim() == 2)[0]
return x.max(dim=dim, keepdim=x.dim() == 2 or x.dim() == 1)[0]
size = int(batch.max().item() + 1) if size is None else size
return scatter(x, batch, dim=-2, dim_size=size, reduce='max')
return scatter(x, batch, dim=dim, dim_size=size, reduce='max')

0 comments on commit 076d667

Please sign in to comment.