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>
66 行
2.9 KiB
ReStructuredText
66 行
2.9 KiB
ReStructuredText
.. _guide-graph-feature:
|
|
|
|
1.3 Node and Edge Features
|
|
--------------------------
|
|
|
|
:ref:`(中文版)<guide_cn-graph-feature>`
|
|
|
|
The nodes and edges of a :class:`~dgl.DGLGraph` can have several user-defined named features for
|
|
storing graph-specific properties of the nodes and edges. These features can be accessed
|
|
via the :py:attr:`~dgl.DGLGraph.ndata` and :py:attr:`~dgl.DGLGraph.edata` interface. For example, the following code creates two node
|
|
features (named ``'x'`` and ``'y'`` in line 8 and 15) and one edge feature (named ``'x'`` in line 9).
|
|
|
|
.. code-block:: python
|
|
:linenos:
|
|
|
|
>>> import dgl
|
|
>>> import torch as th
|
|
>>> g = dgl.graph(([0, 0, 1, 5], [1, 2, 2, 0])) # 6 nodes, 4 edges
|
|
>>> g
|
|
Graph(num_nodes=6, num_edges=4,
|
|
ndata_schemes={}
|
|
edata_schemes={})
|
|
>>> g.ndata['x'] = th.ones(g.num_nodes(), 3) # node feature of length 3
|
|
>>> g.edata['x'] = th.ones(g.num_edges(), dtype=th.int32) # scalar integer feature
|
|
>>> g
|
|
Graph(num_nodes=6, num_edges=4,
|
|
ndata_schemes={'x' : Scheme(shape=(3,), dtype=torch.float32)}
|
|
edata_schemes={'x' : Scheme(shape=(,), dtype=torch.int32)})
|
|
>>> # different names can have different shapes
|
|
>>> g.ndata['y'] = th.randn(g.num_nodes(), 5)
|
|
>>> g.ndata['x'][1] # get node 1's feature
|
|
tensor([1., 1., 1.])
|
|
>>> g.edata['x'][th.tensor([0, 3])] # get features of edge 0 and 3
|
|
tensor([1, 1], dtype=torch.int32)
|
|
|
|
Important facts about the :py:attr:`~dgl.DGLGraph.ndata`/:py:attr:`~dgl.DGLGraph.edata` interface:
|
|
|
|
- Only features of numerical types (e.g., float, double, and int) are allowed. They can
|
|
be scalars, vectors or multi-dimensional tensors.
|
|
- Each node feature has a unique name and each edge feature has a unique name.
|
|
The features of nodes and edges can have the same name. (e.g., 'x' in the above example).
|
|
- A feature is created via tensor assignment, which assigns a feature to each
|
|
node/edge in the graph. The leading dimension of that tensor must be equal to the
|
|
number of nodes/edges in the graph. You cannot assign a feature to a subset of the
|
|
nodes/edges in the graph.
|
|
- Features of the same name must have the same dimensionality and data type.
|
|
- The feature tensor is in row-major layout -- each row-slice stores the feature of one
|
|
node or edge (e.g., see lines 16 and 18 in the above example).
|
|
|
|
For weighted graphs, one can store the weights as an edge feature as below.
|
|
|
|
.. code-block:: python
|
|
|
|
>>> # edges 0->1, 0->2, 0->3, 1->3
|
|
>>> edges = th.tensor([0, 0, 0, 1]), th.tensor([1, 2, 3, 3])
|
|
>>> weights = th.tensor([0.1, 0.6, 0.9, 0.7]) # weight of each edge
|
|
>>> g = dgl.graph(edges)
|
|
>>> g.edata['w'] = weights # give it a name 'w'
|
|
>>> g
|
|
Graph(num_nodes=4, num_edges=4,
|
|
ndata_schemes={}
|
|
edata_schemes={'w' : Scheme(shape=(,), dtype=torch.float32)})
|
|
|
|
|
|
See APIs: :py:attr:`~dgl.DGLGraph.ndata`, :py:attr:`~dgl.DGLGraph.edata`.
|