Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unbatch functionality #4628

Merged
merged 26 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5d16319
Add unbatching tool
tzuhanchang May 12, 2022
62e606c
Add an unbatching tool
tzuhanchang May 12, 2022
24c412f
Add an unbatching tool
tzuhanchang May 12, 2022
0e9d10d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 12, 2022
044d16d
Merge branch 'master' into master
rusty1s May 14, 2022
09bb871
Add unbatch to utils
tzuhanchang May 15, 2022
06871c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2022
084ecb9
Revert changes to loader/utils.py
tzuhanchang May 16, 2022
eb034a7
Update unbatch.py
tzuhanchang May 16, 2022
1ab59b7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 16, 2022
b807908
Update unbatch.py
tzuhanchang May 16, 2022
00cad09
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 16, 2022
8fc94bb
Create test_unbatch.py
tzuhanchang May 16, 2022
ebe17d9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 16, 2022
7ace865
Add unbatch
tzuhanchang May 16, 2022
7da2807
Update test_unbatch.py
tzuhanchang May 16, 2022
ef66115
Update unbatch.py
tzuhanchang May 16, 2022
c1392d9
Update unbatch.py
tzuhanchang May 16, 2022
2a430a6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 16, 2022
74d3ccc
merge
rusty1s May 16, 2022
fd46637
Merge branch 'master' of github.com:tzuhanchang/pytorch_geometric
rusty1s May 16, 2022
a83f0b0
update
rusty1s May 16, 2022
ed634fc
changelog
rusty1s May 16, 2022
7365414
Merge branch 'master' into master
rusty1s May 16, 2022
d1331a0
typo
rusty1s May 16, 2022
3992f87
Merge branch 'master' of github.com:tzuhanchang/pytorch_geometric
rusty1s May 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/utils/test_unbatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import torch

from torch_geometric.utils import unbatch


def test_unbatch():
src = torch.arange(10)
batch = torch.tensor([0, 0, 0, 1, 1, 2, 2, 3, 4, 4])

out = unbatch(src, batch)
assert len(out) == 5
for i in range(len(out)):
assert torch.equal(out[i], src[batch == i])
2 changes: 2 additions & 0 deletions torch_geometric/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .to_dense_batch import to_dense_batch
from .to_dense_adj import to_dense_adj
from .sparse import dense_to_sparse
from .unbatch import unbatch
from .normalized_cut import normalized_cut
from .grid import grid
from .geodesic import geodesic_distance
Expand Down Expand Up @@ -61,6 +62,7 @@
'to_dense_batch',
'to_dense_adj',
'dense_to_sparse',
'unbatch',
'normalized_cut',
'grid',
'geodesic_distance',
Expand Down
23 changes: 23 additions & 0 deletions torch_geometric/utils/unbatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import List

import torch
from torch import Tensor

from torch_geometric.utils import degree


def unbatch(src: Tensor, batch: Tensor, dim: int = 0) -> List[Tensor]:
r"""Splits :obj:`src` according to a :obj:`batch` vector along dimension
:obj:`dim`.

Args:
src (Tensor): The source tensor.
batch (LongTensor): The batch vector
:math:`\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N`, which assigns each
entry in :obj:`src` to a specific example. Must be ordered.
dim (int): The dimension along which to split the :obj:`src` tensor.

:rtype: :class:`List[Tensor]`
"""
sizes = degree(batch, dtype=torch.long).tolist()
return src.split(sizes, dim)