forked from pyg-team/pytorch_geometric
-
Notifications
You must be signed in to change notification settings - Fork 0
CS224W - Bag of Tricks for Node Classification with GNN - Label Usage #2
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
Open
mattjhayes3
wants to merge
32
commits into
master
Choose a base branch
from
label_usage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 27 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
996a189
Create label_usage.py
chriskynguyen e3d14ad
Merge branch 'label_usage' of https://github.com/mattjhayes3/pytorch_…
chriskynguyen f18989f
Upload label_usage.py
chriskynguyen ad7e940
Update label_usage.py
chriskynguyen f5ddba8
Merge branch 'label_usage' of https://github.com/mattjhayes3/pytorch_…
chriskynguyen 7d9835e
Update __init__.py
chriskynguyen 40f06d7
Update label_usage.py
chriskynguyen 339f6bc
Update label_usage.py
chriskynguyen 2b4ca6b
Update label_usage.py
chriskynguyen c704abe
Update label_usage.py
chriskynguyen f35c1d7
Revised label_usage, added test file
chriskynguyen b7130f7
Update test_label_usage.py
chriskynguyen 2533608
Update label_usage.py
chriskynguyen 558b570
fixes for label_usage
chriskynguyen a9b5030
added mini-batch coverage
chriskynguyen 845d618
reworded mini-batch arg
chriskynguyen 9cd9f2e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6e51e2a
updated fixes based on review
chriskynguyen 47ab81a
merge conflict fix
chriskynguyen 65a3c7f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 52ae665
update label_usage output
chriskynguyen 52bca31
Merge branch 'label_usage' of https://github.com/mattjhayes3/pytorch_…
chriskynguyen 88b16f8
fixed formatting to match pyg formatting
chriskynguyen 185190c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9a41d15
updated label_usage from review
chriskynguyen 770e6ce
updated label_usage based on review
chriskynguyen 825691e
added training param description
chriskynguyen 060a5c4
remove explicit training parameter
chriskynguyen 33f98f4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 72ff2c2
changelog and precommit hook fix
chriskynguyen 5b0b775
Merge branch 'label_usage' of https://github.com/mattjhayes3/pytorch_…
chriskynguyen e34cc4b
Merge branch 'master' into label_usage
chriskynguyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import torch | ||
|
|
||
| from torch_geometric.nn.models import GCN, LabelUsage | ||
|
|
||
|
|
||
| def test_label_usage(): | ||
| # Test mask index tensor | ||
| x = torch.rand(6, 4) # 6 nodes, 4 features | ||
| y = torch.tensor([1, 0, 0, 2, 1, 1]) | ||
| edge_index = torch.tensor([[0, 1, 1, 2, 4, 5], [1, 0, 2, 1, 5, 4]]) | ||
| mask = torch.tensor([0, 2, 3, 5]) | ||
|
|
||
| num_classes = len(torch.unique(y)) | ||
| base_model = GCN(in_channels=x.size(1) + num_classes, hidden_channels=8, | ||
| num_layers=3, out_channels=num_classes) | ||
| label_usage = LabelUsage(base_model=base_model, num_classes=num_classes, | ||
| split_ratio=0.6, num_recycling_iterations=10, | ||
| return_tuple=True) | ||
|
|
||
| output, train_labels_idx, train_pred_idx = label_usage( | ||
| feat=x, edge_index=edge_index, y=y, mask=mask) | ||
|
|
||
| # Check output shapes | ||
| assert output.size(0) == x.size(0) | ||
| assert output.size(1) == num_classes | ||
|
|
||
| # Test mask bool tensor | ||
| num_nodes = x.size(0) | ||
| mask_bool = torch.zeros(num_nodes, dtype=torch.bool) | ||
| mask_bool[mask] = True | ||
|
|
||
| label_usage_bool = LabelUsage(base_model=base_model, num_classes=num_classes, | ||
| split_ratio=0.6, num_recycling_iterations=10, | ||
| return_tuple=True) | ||
|
|
||
| output, train_labels_idx, train_pred_idx = label_usage( | ||
| feat=x, edge_index=edge_index, y=y, mask=mask_bool) | ||
|
|
||
| # Check output shapes | ||
| assert output.size(0) == x.size(0) | ||
| assert output.size(1) == num_classes | ||
|
|
||
| # Test zero recycling iterations | ||
| label_usage_zero_recycling = LabelUsage( | ||
| base_model=base_model, | ||
| num_classes=num_classes, | ||
| ) | ||
|
|
||
| output = label_usage_zero_recycling(feat=x, edge_index=edge_index, y=y, | ||
| mask=mask) | ||
| assert output.size(0) == x.size(0) | ||
| assert output.size(1) == num_classes | ||
|
|
||
| # Test 2D label tensor | ||
| y = torch.tensor([[1], [0], [0], [2], [1], [1]]) # Node labels (N, 1) | ||
| label_usage_2d = LabelUsage(base_model=base_model, num_classes=num_classes, | ||
| split_ratio=0.6, num_recycling_iterations=10, | ||
| return_tuple=False) | ||
| output = label_usage_2d(feat=x, edge_index=edge_index, y=y, mask=mask) | ||
| assert output.size(0) == x.size(0) | ||
| assert output.size(1) == num_classes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import torch | ||
| import torch.nn.functional as F | ||
| from torch import Tensor | ||
|
|
||
| from torch_geometric.typing import Adj | ||
|
|
||
|
|
||
| class LabelUsage(torch.nn.Module): | ||
| r"""The label usage operator for semi-supervised node classification, | ||
| as introduced in `"Bag of Tricks for Node Classification" | ||
| <https://arxiv.org/abs/2103.13355>`_ paper. | ||
|
|
||
| Label usage splits training nodes into labeled and unlabeled subsets. The | ||
| labeled subset incorporates labels as features while the unlabeled subset | ||
| labels are zeroed and used for prediction. During inference, previously | ||
| predicted soft labels for unlabeled nodes are recycled as inputs for the | ||
| model, refining predictions iteratively. | ||
|
|
||
| .. note:: | ||
|
|
||
| When using the :class:`LabelUsage`, adjust the model's input dimension | ||
| accordingly to include both features and classes. | ||
|
|
||
| Args: | ||
| base_model: An instance of the model that will do the | ||
| inner forward pass. | ||
| num_classes (int): Number of classes in dataset | ||
| split_ratio (float): Proportion of true labels to use as features | ||
| during training (default: :obj:'0.5') | ||
| num_recycling_iterations (int): Number of iterations for the | ||
| label reuse procedure to cycle predicted soft labels | ||
| (default: :obj:'0') | ||
| return_tuple (bool): If true, returns (pred, train_label, | ||
| train_pred) during training otherwise returns | ||
| prediction output (default :obj:'False') | ||
| training (bool): If true, sets forward method to training mode and | ||
chriskynguyen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| utilizes split ratio else runs evaluation and uses all training | ||
| node labels as features (default :obj:'True') | ||
| """ | ||
| def __init__( | ||
| self, | ||
| base_model: torch.nn.Module, | ||
| num_classes: int, | ||
| split_ratio: float = 0.5, | ||
| num_recycling_iterations: int = 0, | ||
| return_tuple: bool = False, | ||
chriskynguyen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| training: bool = True | ||
| ): | ||
|
|
||
| super().__init__() | ||
| self.base_model = base_model | ||
| self.num_classes = num_classes | ||
| self.split_ratio = split_ratio | ||
| self.num_recycling_iterations = num_recycling_iterations | ||
| self.return_tuple = return_tuple | ||
| self.training = training | ||
|
|
||
| def forward( | ||
| self, | ||
| feat: Tensor, | ||
| edge_index: Adj, | ||
| y: Tensor, | ||
| mask: Tensor, | ||
| ): | ||
| r"""Forward pass using label usage algorithm. | ||
|
|
||
| Args: | ||
| feat (torch.Tensor): Node feature tensor of dimension (N,F) | ||
| where N is the number of nodes and F is the number | ||
| of features per node | ||
| edge_index (torch.Tensor or SparseTensor): The edge connectivity | ||
| to be passed to base_model | ||
| y (torch.Tensor): Node ground-truth labels tensor of dimension | ||
| of (N,) for 1D tensor or (N,1) for 2D tensor | ||
| mask (torch.Tensor): A mask or index tensor denoting which nodes | ||
| are used during training | ||
| """ | ||
| assert feat.dim() == 2, f"feat must be 2D but got shape {feat.shape}" | ||
| assert y.dim() == 1 or (y.dim() == 2 and y.size(1) == 1),\ | ||
| f"Expected y to be either (N,) or (N, 1), but got shape {y.shape}" | ||
|
|
||
| # set unlabeled mask for unlabeled indices | ||
| unlabeled_mask = torch.ones(feat.size(0), | ||
| dtype=torch.bool).to(feat.device) | ||
|
|
||
| # add labels to features for train_labels nodes if in training | ||
| # else fill true labels for all nodes in mask | ||
| # zero value nodes in train_pred | ||
| onehot = torch.zeros([feat.shape[0], self.num_classes]).to(feat.device) | ||
| if self.training: | ||
| # random split mask based on split ratio | ||
| if mask.dtype == torch.bool: | ||
chriskynguyen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mask = mask.nonzero(as_tuple=False).view(-1) | ||
| split_mask = torch.rand(mask.shape) < self.split_ratio | ||
| train_labels = mask[split_mask] # D_L: nodes with labels | ||
| train_pred = mask[~split_mask] # D_U: nodes to predict labels | ||
|
|
||
| unlabeled_mask[train_labels] = False | ||
|
|
||
| # create a one-hot encoding according to tensor dim | ||
| if y.dim() == 2: | ||
| onehot[train_labels, y[train_labels, 0]] = 1 | ||
| else: | ||
| onehot[train_labels, y[train_labels]] = 1 | ||
| else: | ||
| unlabeled_mask[mask] = False | ||
|
|
||
| # create a one-hot encoding according to tensor dim | ||
| if y.dim() == 2: | ||
| onehot[mask, y[mask, 0]] = 1 | ||
| else: | ||
| onehot[mask, y[mask]] = 1 | ||
|
|
||
| feat = torch.cat([feat, onehot], dim=-1) | ||
|
|
||
| pred = self.base_model(feat, edge_index) | ||
|
|
||
| # label reuse procedure | ||
| for _ in range(self.num_recycling_iterations): | ||
| pred = pred.detach() | ||
| feat[unlabeled_mask, | ||
| -self.num_classes:] = F.softmax(pred[unlabeled_mask], dim=-1) | ||
| pred = self.base_model(feat, edge_index) | ||
|
|
||
| # return tuples if specified | ||
| if self.return_tuple and self.training: | ||
chriskynguyen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return pred, train_labels, train_pred | ||
| return pred | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.