dmlc--dgl
b0a9d16f25
* [Feature] Add full graph training with dgl built-in dataset. * [Feature] Add full graph training with dgl built-in dataset. * [Feature] Add full graph training with dgl built-in dataset. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Bug] fix model to cuda. * [Feature] Add test loss and accuracy * [Feature] Add test loss and accuracy * [Feature] Add test loss and accuracy * [Feature] Add test loss and accuracy * [Feature] Add test loss and accuracy * [Feature] Add test loss and accuracy * [Fix] Add random * [Bug] Fix batch norm error * [Doc] Test with CN in Sphinx * [Doc] Test with CN in Sphinx * [Doc] Remove the test CN docs. * [Feature] Add input embedding layer * [Feature] Add input embedding layer * [Feature] Add input embedding layer * [Feature] Add input embedding layer * [Feature] Add input embedding layer * [Feature] Add input embedding layer * [Feature] Add input embedding layer * [Feature] Add input embedding layer * [Feature] Add input embedding layer * [Doc] fill readme with new performance results * [Doc] Add Chinese User Guide, graph and 1.5 * [Doc] Add Chinese User Guide, graph and 1.5 * Update README.md * [Fix] Temporary remove compgcn * [Doc] Add CN user guide chapter2 * [Test] Tunning format * [Test] Tunning format * [Test] Tunning format * [Test] Tunning format * [Test] Tunning format * [Test] Section headers * [Fix] Fix format errors * [Fix] Fix format errors * [Fix] Fix format errors * [Doc] Add CN-EN EN-CN links * [Doc] Add CN-EN EN-CN links * [Doc] Copyedit chapter2 * [Doc] Copyedit chapter2 * [Doc] Remove EN in 2.1 * [Doc] Remove EN in chapter 2 * [Doc] Copyedit first 2 sections * [Doc] Copyedit first 2 sections * [Doc] copyedited chapter 2 CN * [Doc] Add chapter 3 raw texts * [Doc] Add chapter 3 preface and 3.1 * [Doc] Add chapter 3.2 and 3.3 * [Doc] Add chapter 3.2 and 3.3 * [Doc] Add chapter 3.2 and 3.3 * [Doc] Remove EN parts * [Doc] Copyediting 3.1 * [Doc] Copyediting 3.2 and 3.3 * [Doc] Proofreading 3.1 and 3.2 * [Doc] Proofreading 3.2 and 3.3 * [Doc] Add chapter 4 CN raw text. * [Clean] Remove codes in other branches * [Doc] Start to copyedit chapter 4 preface * [Doc] copyedit CN section 4.1 * [Doc] Remove EN in User Guide Chapter 4 * [Doc] Copyedit chapter 4.1 * [Doc] copyedit cn chapter 4.2, 4.3, 4.4, and 4.5. * [Doc] Fix errors in EN user guide graph feature and heterograph * [Doc] 2nd round copyediting with Murph's comments * [Doc] 3rd round copyediting with Murph's comments * [Doc] 3rd round copyediting with Murph's comments * [Doc] 3rd round copyediting with Murph's comments * [Sync] syncronize with the dgl master * [Doc] edited after Minjie's comments, 1st round * update cub Co-authored-by: Minjie Wang <wmjlyjemaine@gmail.com>
85 行
3.3 KiB
ReStructuredText
85 行
3.3 KiB
ReStructuredText
.. _guide-nn-construction:
|
|
|
|
3.1 DGL NN Module Construction Function
|
|
---------------------------------------
|
|
|
|
:ref:`(中文版) <guide_cn-nn-construction>`
|
|
|
|
The construction function performs the following steps:
|
|
|
|
1. Set options.
|
|
2. Register learnable parameters or submodules.
|
|
3. Reset parameters.
|
|
|
|
.. code::
|
|
|
|
import torch.nn as nn
|
|
|
|
from dgl.utils import expand_as_pair
|
|
|
|
class SAGEConv(nn.Module):
|
|
def __init__(self,
|
|
in_feats,
|
|
out_feats,
|
|
aggregator_type,
|
|
bias=True,
|
|
norm=None,
|
|
activation=None):
|
|
super(SAGEConv, self).__init__()
|
|
|
|
self._in_src_feats, self._in_dst_feats = expand_as_pair(in_feats)
|
|
self._out_feats = out_feats
|
|
self._aggre_type = aggregator_type
|
|
self.norm = norm
|
|
self.activation = activation
|
|
|
|
In construction function, one first needs to set the data dimensions. For
|
|
general PyTorch module, the dimensions are usually input dimension,
|
|
output dimension and hidden dimensions. For graph neural, the input
|
|
dimension can be split into source node dimension and destination node
|
|
dimension.
|
|
|
|
Besides data dimensions, a typical option for graph neural network is
|
|
aggregation type (``self._aggre_type``). Aggregation type determines how
|
|
messages on different edges are aggregated for a certain destination
|
|
node. Commonly used aggregation types include ``mean``, ``sum``,
|
|
``max``, ``min``. Some modules may apply more complicated aggregation
|
|
like an ``lstm``.
|
|
|
|
``norm`` here is a callable function for feature normalization. In the
|
|
SAGEConv paper, such normalization can be l2 normalization:
|
|
:math:`h_v = h_v / \lVert h_v \rVert_2`.
|
|
|
|
.. code::
|
|
|
|
# aggregator type: mean, max_pool, lstm, gcn
|
|
if aggregator_type not in ['mean', 'max_pool', 'lstm', 'gcn']:
|
|
raise KeyError('Aggregator type {} not supported.'.format(aggregator_type))
|
|
if aggregator_type == 'max_pool':
|
|
self.fc_pool = nn.Linear(self._in_src_feats, self._in_src_feats)
|
|
if aggregator_type == 'lstm':
|
|
self.lstm = nn.LSTM(self._in_src_feats, self._in_src_feats, batch_first=True)
|
|
if aggregator_type in ['mean', 'max_pool', 'lstm']:
|
|
self.fc_self = nn.Linear(self._in_dst_feats, out_feats, bias=bias)
|
|
self.fc_neigh = nn.Linear(self._in_src_feats, out_feats, bias=bias)
|
|
self.reset_parameters()
|
|
|
|
Register parameters and submodules. In SAGEConv, submodules vary
|
|
according to the aggregation type. Those modules are pure PyTorch nn
|
|
modules like ``nn.Linear``, ``nn.LSTM``, etc. At the end of construction
|
|
function, weight initialization is applied by calling
|
|
``reset_parameters()``.
|
|
|
|
.. code::
|
|
|
|
def reset_parameters(self):
|
|
"""Reinitialize learnable parameters."""
|
|
gain = nn.init.calculate_gain('relu')
|
|
if self._aggre_type == 'max_pool':
|
|
nn.init.xavier_uniform_(self.fc_pool.weight, gain=gain)
|
|
if self._aggre_type == 'lstm':
|
|
self.lstm.reset_parameters()
|
|
if self._aggre_type != 'gcn':
|
|
nn.init.xavier_uniform_(self.fc_self.weight, gain=gain)
|
|
nn.init.xavier_uniform_(self.fc_neigh.weight, gain=gain)
|