@@ -9,7 +9,7 @@ Create a Graph
99
1010To begin with, let's create a graph.
1111
12- .. code-block :: python
12+ .. code :: python
1313
1414 import torch
1515 from torchdrug import data
@@ -25,15 +25,15 @@ This will plot a ring graph like the following.
2525 :width: 33%
2626
2727Internally, the graph is stored as a sparse edge list to save memory footprint. For
28- an intuitive comparison, a `scale-free graph `_ mayr have 1 million nodes and 10 million
28+ an intuitive comparison, a `scale-free graph `_ may have 1 million nodes and 10 million
2929edges. The dense version takes about 4TB, while the sparse version only requires 120MB.
3030
3131.. _scale-free graph :
3232 https://en.wikipedia.org/wiki/Scale-free_network
3333
3434Here are some commonly used properties of the graph.
3535
36- .. code-block :: python
36+ .. code :: python
3737
3838 print (graph.num_node)
3939 print (graph.num_edge)
@@ -45,7 +45,7 @@ molecules have bond types like ``single bound``, while knowledge graphs have rel
4545like ``consists of ``. To construct such a relational graph, we can pass the edge type
4646as a third variable in the edge list.
4747
48- .. code-block :: python
48+ .. code :: python
4949
5050 triplet_list = [[0 , 1 , 0 ], [1 , 2 , 1 ], [2 , 3 , 0 ], [3 , 4 , 1 ], [4 , 5 , 0 ], [5 , 0 , 1 ]]
5151 graph = data.Graph(triplet_list, num_node = 6 , num_relation = 2 )
@@ -62,7 +62,7 @@ corresponds to an edge from node :math:`i` to node :math:`j`. The relational gra
6262uses a 3D adjacency matrix :math: `A`, where non-zero :math: `A_{i,j,k}` denotes an
6363edge from node :math: `i` to node :math: `j` with edge type :math: `k`.
6464
65- .. code-block :: python
65+ .. code :: python
6666
6767 adjacency = torch.zeros(6 , 6 )
6868 adjacency[edge_list] = 1
@@ -78,7 +78,7 @@ For example, the following code creates a benzene molecule.
7878.. _SMILES :
7979 https://en.wikipedia.org/wiki/Simplified_molecular-input_line-entry_system
8080
81- .. code-block :: python
81+ .. code :: python
8282
8383 mol = data.Molecule.from_smiles(" C1=CC=CC=C1" )
8484 mol.visualize()
@@ -90,7 +90,7 @@ For example, the following code creates a benzene molecule.
9090Once the graph is created, we can transfer it between CPU and GPUs, just like
9191:class: `torch.Tensor `.
9292
93- .. code-block :: python
93+ .. code :: python
9494
9595 graph = graph.cuda()
9696 print (graph.device)
@@ -109,7 +109,7 @@ during any graph operation.
109109
110110Here we specify some features during the construction of the molecule graph.
111111
112- .. code-block :: python
112+ .. code :: python
113113
114114 mol = data.Molecule.from_smiles(" C1=CC=CC=C1" , node_feature = " default" ,
115115 edge_feature = " default" , graph_feature = " ecfp" )
@@ -122,14 +122,15 @@ We may also want to define our own attributes. This only requires to wrap the
122122assignment lines with a context manager. The following example defines edge importance
123123as the reciprocal of node degrees.
124124
125- .. code-block :: python
125+ .. code :: python
126126
127127 node_in, node_out = mol.edge_list.t()[:2 ]
128128 with mol.edge():
129129 mol.edge_importance = 1 / graph.degree_in[node_in] + 1 / graph.degree_out[node_out]
130130
131131 We can use ``mol.node() `` and ``mol.graph() `` for node- and graph-level attributes
132- respectively.
132+ respectively. Attributes may also be a reference to node/edge/graph indexes. See
133+ :doc: `reference ` for more details.
133134
134135Note in order to support batching and masking, attributes should always have the same
135136length as their corresponding components. This means the size of the first dimension of
@@ -142,7 +143,7 @@ Modern deep learning frameworks employs batched operations to accelerate computa
142143In TorchDrug, we can easily batch same kind of graphs with **arbitary sizes **. Here
143144is an example of creating a batch of 4 graphs.
144145
145- .. code-block :: python
146+ .. code :: python
146147
147148 graphs = [graph, graph, graph, graph]
148149 batch = data.Graph.pack(graphs)
@@ -170,7 +171,7 @@ where :math:`A_i` is the adjacency of :math:`i`-th graph.
170171To get a single graph from the batch, use the conventional index or
171172:meth: `PackedGraph.unpack <torchdrug.data.PackedGraph.unpack> `.
172173
173- .. code-block :: python
174+ .. code :: python
174175
175176 graph = batch[1 ]
176177 graphs = batch.unpack()
@@ -186,7 +187,7 @@ Subgraph and Masking
186187The graph data structure also provides a bunch of slicing operations to create subgraphs
187188or masked graphs in a sparse manner. Some typical operations include
188189
189- .. code-block :: python
190+ .. code :: python
190191
191192 g1 = graph.subgraph([1 , 2 , 3 , 4 ])
192193 g1.visualize()
@@ -220,7 +221,7 @@ isolated nodes.
220221The same operations can also be applied to batches. In this case, we need to convert
221222the index of a single graph into the index in a batch.
222223
223- .. code-block :: python
224+ .. code :: python
224225
225226 graph_ids = torch.tensor([0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 ])
226227 node_ids = torch.tensor([1 , 2 , 3 , 4 , 0 , 1 , 2 , 3 , 4 , 5 ])
@@ -232,7 +233,7 @@ the index of a single graph into the index in a batch.
232233
233234We can also pick a subset of graphs in a batch.
234235
235- .. code-block :: python
236+ .. code :: python
236237
237238 batch = batch[[0 , 1 ]]
238239 batch.visualize()
0 commit comments