Unverified Commit 02dd6d90 authored by zhjwy9343's avatar zhjwy9343 Committed by GitHub
Browse files

[Feature] Add dot product attention (#1604)



* Add dotproduct attention

* [Feature] Add dotproduct attention

* [Feature] Add dotproduct attention

* [Feature] Add dotproduct attention

* [New] Update landing page

* [New] Update landing page

* [New] Update landing page

* [Doc] Update landing page

* [Doc] Update landing page

* [Doc] Update landing page

* [Doc] Update landing page

* [Doc] Update landing page

* [Doc] Update landing page

* [Doc] Update landing page

* [Doc] Update landing page
Co-authored-by: default avatarJinjing Zhou <VoVAllen@users.noreply.github.com>
parent fe207b45
...@@ -18,9 +18,11 @@ DGL is an easy-to-use, high performance and scalable Python package for deep lea ...@@ -18,9 +18,11 @@ DGL is an easy-to-use, high performance and scalable Python package for deep lea
</p> </p>
## <img src="http://data.dgl.ai/asset/image/new.png" width="30">DGL News ## <img src="http://data.dgl.ai/asset/image/new.png" width="30">DGL News
*03/31/2020*: The new **v0.4.3 release** includes official TensorFlow support, with 15 popular GNN modules. DGL-KE and DGL-LifeSci, two packages for knowledge graph embedding and chemi- and bio-informatics respectively, have graduated as standalone packages and can be installed by pip and conda. The new release provides full support of graph sampling on heterogeneous graphs, with multi-GPU acceleration. See our [new feature walkthrough](https://www.dgl.ai/release/2020/04/01/release.html) and [release note](https://github.com/dmlc/dgl/releases/tag/0.4.3). **06/11/2020**: Amazon Shanghai AI Lab and AWS Deep Engine Science team working along with academic collaborators from the University of Minnesota, The Ohio State University, and Hunan University have created the **[Drug Repurposing Knowledge Graph (DRKG)](https://github.com/gnn4dr/DRKG)** and a set of machine learning tools, [DGL-KE](https://github.com/awslabs/dgl-ke), that can be used to prioritize drugs for repurposing studies.
DRKG is a comprehensive biological knowledge graph that relates human genes, compounds, biological processes, drug side effects, diseases and symptoms. DRKG includes, curates, and normalizes information from six publicly available databases and data that were collected from recent publications related to Covid-19. It has 97,238 entities belonging to 13 types of entities, and 5,874,261 triplets belonging to 107 types of relations.
More about the dataset is in this [blogpost](https://www.dgl.ai/news/2020/06/09/covid.html).
*03/02/2020*: **Check out this cool paper: [Benchmarking Graph Neural Networks](https://arxiv.org/abs/2003.00982)!** It includes a DGL-based benchmark framework for novel medium-scale graph datasets, covering mathematical modeling, computer vision, chemistry and combinatorial problems. See [repo here](https://github.com/graphdeeplearning/benchmarking-gnns). **03/31/2020**: The new **v0.4.3 release** includes official TensorFlow support, with 15 popular GNN modules. DGL-KE and DGL-LifeSci, two packages for knowledge graph embedding and chemi- and bio-informatics respectively, have graduated as standalone packages and can be installed by pip and conda. The new release provides full support of graph sampling on heterogeneous graphs, with multi-GPU acceleration. See our [new feature walkthrough](https://www.dgl.ai/release/2020/04/01/release.html) and [release note](https://github.com/dmlc/dgl/releases/tag/0.4.3).
## Using DGL ## Using DGL
......
"""Torch modules for graph attention networks(GAT)."""
# pylint: disable= no-member, arguments-differ, invalid-name
from torch import nn
from .... import function as fn
from ..softmax import edge_softmax
from ....utils import expand_as_pair
class DotGatConv(nn.Module):
r"""Apply dot product version of self attention in GCN.
.. math::
h_i^{(l+1)} = \sum_{j\in \mathcal{N}(i)} \alpha_{i, j} h_j^{(l)}
where :math:`\alpha_{ij}` is the attention score bewteen node :math:`i` and node :math:`j`:
.. math::
\alpha_{i, j} = \mathrm{softmax_i}(e_{ij}^{l})
e_{ij}^{l} = ({W_i^{(l)} h_i^{(l)}})^T \cdot {W_j^{(l)} h_j^{(l)}}
where :math:`W_i` and :math:`W_j` transform node :math:`i`'s and node :math:`j`'s
features into the same dimension, so that when compute note features' similarity,
we can use dot-product.
"""
def __init__(self,
in_feats,
out_feats
):
super(DotGatConv, self).__init__()
self._in_src_feats, self._in_dst_feats = expand_as_pair(in_feats)
self._out_feats = out_feats
if isinstance(in_feats, tuple):
self.fc_src = nn.Linear(self._in_src_feats, self._out_feats, bias=False)
self.fc_dst = nn.Linear(self._in_dst_feats, self._out_feats, bias=False)
else:
self.fc = nn.Linear(self._in_src_feats, self._out_feats, bias=False)
def forward(self, graph, feat):
r"""Apply dot product version of self attention in GCN.
Parameters
----------
graph: DGLGraph or bi_partities graph
The graph
feat: torch.Tensor or pair of torch.Tensor
If a torch.Tensor is given, the input feature of shape :math:`(N, D_{in})` where
:math:`D_{in}` is size of input feature, :math:`N` is the number of nodes.
If a pair of torch.Tensor is given, the pair must contain two tensors of shape
:math:`(N_{in}, D_{in_{src}})` and :math:`(N_{out}, D_{in_{dst}})`.
Returns
-------
torch.Tensor
The output feature of shape :math:`(N, D_{out})` where :math:`D_{out}` is size
of output feature.
"""
graph = graph.local_var()
# check if feat is a tuple
if isinstance(feat, tuple):
h_src = feat[0]
h_dst = feat[1]
feat_src = self.fc_src(h_src)
feat_dst = self.fc_dst(h_dst)
else:
h_src = feat
feat_src = feat_dst = self.fc(h_src)
# Assign features to nodes
graph.srcdata.update({'ft': feat_src})
graph.dstdata.update({'ft': feat_dst})
# Step 1. dot product
graph.apply_edges(fn.u_dot_v('ft', 'ft', 'a'))
# Step 2. edge softmax to compute attention scores
graph.edata['sa'] = edge_softmax(graph, graph.edata['a'])
# Step 3. Broadcast softmax value to each edge, and then attention is done
graph.apply_edges(lambda edges: {'attn': edges.src['ft'] * \
edges.data['sa'].unsqueeze(dim=0).T})
# Step 4. Aggregate attention to dst,user nodes, so formula 7 is done
graph.update_all(fn.copy_e('attn', 'm'), fn.sum('m', 'agg_u'))
# output results to the destination nodes
rst = graph.dstdata['agg_u']
return rst
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment