-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Adding readout layers for use in grover #3269
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 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 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,88 @@ | ||
from typing import List | ||
try: | ||
import torch | ||
import torch.nn as nn | ||
except ModuleNotFoundError: | ||
raise ImportError('The module requires PyTorch to be installed') | ||
|
||
from deepchem.models.torch_models.attention import SelfAttention | ||
|
||
|
||
class GroverReadout(nn.Module): | ||
"""Performs readout on a batch of graph | ||
|
||
The readout module is used for performing readouts on batched graphs to | ||
convert node embeddings/edge embeddings into graph embeddings. It is used | ||
in the Grover architecture to generate a graph embedding from node and edge | ||
embeddings. The generate embedding can be used in downstream tasks like graph | ||
classification or graph prediction problems. | ||
|
||
Parameters | ||
---------- | ||
rtype: str | ||
Readout type, can be 'mean' or 'self-attention' | ||
hidden_size: int | ||
Input layer hidden size | ||
attn_hidden_size: int | ||
If readout type is attention, size of hidden layer in attention network. | ||
attn_out_size: int | ||
If readout type is attention, size of attention out layer. | ||
|
||
Example | ||
------- | ||
>>> import torch | ||
>>> from deepchem.models.torch_models.readout import GroverReadout | ||
>>> n_nodes, n_features = 6, 32 | ||
>>> readout = GroverReadout(rtype="mean") | ||
>>> embedding = torch.ones(n_nodes, n_features) | ||
>>> result = readout(embedding, scope=[(0, 6)]) | ||
>>> result.size() | ||
torch.Size([1, 32]) | ||
""" | ||
|
||
def __init__(self, | ||
rtype: str = 'mean', | ||
in_features: int = 128, | ||
attn_hidden_size: int = 32, | ||
attn_out_size: int = 32): | ||
super(GroverReadout, self).__init__() | ||
self.cached_zero_vector = nn.Parameter(torch.zeros(in_features), | ||
requires_grad=False) | ||
self.rtype = rtype | ||
if rtype == "self_attention": | ||
self.attn = SelfAttention(hidden_size=attn_hidden_size, | ||
in_features=in_features, | ||
out_features=attn_out_size) | ||
|
||
def forward(self, graph_embeddings: torch.Tensor, | ||
scope: List[List]) -> torch.Tensor: | ||
"""Given a batch node/edge embedding and a scope list, produce the graph-level embedding by scope. | ||
|
||
Parameters | ||
---------- | ||
embeddings: torch.Tensor | ||
The embedding matrix, num_nodes x in_features or num_edges x in_features. | ||
scope: List[List] | ||
A list, in which the element is a list [start, range]. `start` is the index, | ||
`range` is the length of scope. (start + range = end) | ||
|
||
Returns | ||
---------- | ||
graph_embeddings: torch.Tensor | ||
A stacked tensor containing graph embeddings of shape len(scope) x in_features if readout type is mean or len(scope) x attn_out_size when readout type is self-attention. | ||
""" | ||
embeddings: List[torch.Tensor] = [] | ||
for _, (a_start, a_size) in enumerate(scope): | ||
if a_size == 0: | ||
embeddings.append(self.cached_zero_vector) | ||
else: | ||
embedding = graph_embeddings.narrow(0, a_start, a_size) | ||
if self.rtype == "self_attention": | ||
embedding, attn = self.attn(embedding) | ||
embedding = embedding.flatten() | ||
elif self.rtype == "mean": | ||
embedding = embedding.sum(dim=0) / a_size | ||
embeddings.append(embedding) | ||
|
||
graph_embeddings = torch.stack(embeddings, dim=0) | ||
return graph_embeddings |
This file contains 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,39 @@ | ||
import pytest | ||
|
||
try: | ||
import torch | ||
except ModuleNotFoundError: | ||
pass | ||
|
||
|
||
@pytest.mark.torch | ||
def testGroverReadout(): | ||
from deepchem.models.torch_models.readout import GroverReadout | ||
n_nodes, n_features = 6, 32 | ||
readout_mean = GroverReadout(rtype="mean") | ||
|
||
# testing a simple scenario where each embedding corresponds to an unique graph | ||
embedding = torch.ones(n_nodes, n_features) | ||
scope = [(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1)] | ||
readout = readout_mean(embedding, scope) | ||
assert readout.shape == (n_nodes, n_features) | ||
assert (readout == torch.ones(n_nodes, n_features)).all().tolist() | ||
|
||
# here embeddings 0, 1 belong to a scope, 2, 3 to another scope and 4, 5 to another scope | ||
# thus, we sill have 3 graphs | ||
n_graphs = n_nodes // 2 | ||
scope = [(0, 2), (2, 2), (4, 2)] | ||
embedding[torch.tensor([0, 2, 4])] = torch.zeros_like( | ||
embedding[torch.tensor([0, 2, 4])]) | ||
readout = readout_mean(embedding, scope) | ||
assert readout.shape == (n_graphs, n_features) | ||
assert (readout == torch.ones(n_graphs, n_features) / 2).all().tolist() | ||
|
||
attn_out = 8 | ||
readout_attn = GroverReadout(rtype="self_attention", | ||
in_features=n_features, | ||
attn_hidden_size=32, | ||
attn_out_size=attn_out) | ||
|
||
readout = readout_attn(embedding, scope) | ||
assert readout.shape == (n_graphs, attn_out * n_features) |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a few sentences explaining how this is used in the Grover model?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added docs in 521a676