{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\nHow Does DGL Represent A Graph?\n===============================\n\nBy the end of this tutorial you will be able to:\n\n- Construct a graph in DGL from scratch.\n- Assign node and edge features to a graph.\n- Query properties of a DGL graph such as node degrees and\n connectivity.\n- Transform a DGL graph into another graph.\n- Load and save DGL graphs.\n\n(Time estimate: 16 minutes)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "DGL Graph Construction\n----------------------\n\nDGL represents a directed graph as a ``DGLGraph`` object. You can\nconstruct a graph by specifying the number of nodes in the graph as well\nas the list of source and destination nodes. Nodes in the graph have\nconsecutive IDs starting from 0.\n\nFor instance, the following code constructs a directed star graph with 5\nleaves. The center node's ID is 0. The edges go from the\ncenter node to the leaves.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import dgl\nimport numpy as np\nimport torch\n\ng = dgl.graph(([0, 0, 0, 0, 0], [1, 2, 3, 4, 5]), num_nodes=6)\n# Equivalently, PyTorch LongTensors also work.\ng = dgl.graph((torch.LongTensor([0, 0, 0, 0, 0]), torch.LongTensor([1, 2, 3, 4, 5])), num_nodes=6)\n\n# You can omit the number of nodes argument if you can tell the number of nodes from the edge list alone.\ng = dgl.graph(([0, 0, 0, 0, 0], [1, 2, 3, 4, 5]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Edges in the graph have consecutive IDs starting from 0, and are\nin the same order as the list of source and destination nodes during\ncreation.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Print the source and destination nodes of every edge.\nprint(g.edges())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

#### Note

``DGLGraph``'s are always directed to best fit the computation\n pattern of graph neural networks, where the messages sent\n from one node to the other are often different between both\n directions. If you want to handle undirected graphs, you may consider\n treating it as a bidirectional graph. See `Graph\n Transformations`_ for an example of making\n a bidirectional graph.

\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assigning Node and Edge Features to Graph\n-----------------------------------------\n\nMany graph data contain attributes on nodes and edges.\nAlthough the types of node and edge attributes can be arbitrary in real\nworld, ``DGLGraph`` only accepts attributes stored in tensors (with\nnumerical contents). Consequently, an attribute of all the nodes or\nedges must have the same shape. In the context of deep learning, those\nattributes are often called *features*.\n\nYou can assign and retrieve node and edge features via ``ndata`` and\n``edata`` interface.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Assign a 3-dimensional node feature vector for each node.\ng.ndata['x'] = torch.randn(6, 3)\n# Assign a 4-dimensional edge feature vector for each edge.\ng.edata['a'] = torch.randn(5, 4)\n# Assign a 5x4 node feature matrix for each node. Node and edge features in DGL can be multi-dimensional.\ng.ndata['y'] = torch.randn(6, 5, 4)\n\nprint(g.edata['a'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

#### Note

The vast development of deep learning has provided us many\n ways to encode various types of attributes into numerical features.\n Here are some general suggestions:\n\n - For categorical attributes (e.g.\u00a0gender, occupation), consider\n converting them to integers or one-hot encoding.\n - For variable length string contents (e.g.\u00a0news article, quote),\n consider applying a language model.\n - For images, consider applying a vision model such as CNNs.\n\n You can find plenty of materials on how to encode such attributes\n into a tensor in the `PyTorch Deep Learning\n Tutorials `__.