Unverified Commit 35f27c73 authored by zhjwy9343's avatar zhjwy9343 Committed by GitHub
Browse files

[Doc] user guide Chinese chapter 5 (#2384)

* [Doc] Add CN chapter 5

* [Doc] Add CN 5.1

* [Doc] Add CN 5.2, 5.3, and 5.4 raw text

* [Doc] Add CN 5.2, 5.3, and 5.4 raw text

* [Doc] Put 5.1 CN parts

* [Doc] Put 5.2,5.3, and 5.4 raw CN parts

* [Doc] Fix linkage bugs.

* [Doc] 1st round of copyediting, Chapter5, 5.1, and part of 5.2

* [Doc] 2nd round of copyediting, removing EN parts.

* [Doc] 2nd round of copyediting, removing EN parts.

* [Doc] 2nd round of copyediting, removing EN parts.

* [Doc] 2nd round of copyediting, removing EN parts.

* [Doc] 3rd round of copyediting.

* [Doc] 4th round of copyediting to Minjie's comments.

* [Doc] 4th round of copyediting to Minjie's comments.

* [Doc] 4th round of copyediting to Minjie's comments.

* [Doc] 4th round of copyediting to Minjie's comments.

* [Doc] 4th round of copyediting to Minjie's comments.

* [Doc] 4th round of copyediting to Minjie's comments.

* [Doc] 5th round of copyediting to Murphy's comments.
parent 27b05584
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
A heterogeneous graph can have nodes and edges of different types. Nodes/Edges of A heterogeneous graph can have nodes and edges of different types. Nodes/Edges of
different types have independent ID space and feature storage. For example in the figure below, the different types have independent ID space and feature storage. For example in the figure below, the
user and game node IDs both start from zero and the they have different features. user and game node IDs both start from zero and they have different features.
.. figure:: https://data.dgl.ai/asset/image/user_guide_graphch_2.png .. figure:: https://data.dgl.ai/asset/image/user_guide_graphch_2.png
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
5.2 Edge Classification/Regression 5.2 Edge Classification/Regression
--------------------------------------------- ---------------------------------------------
:ref:`(中文版) <guide_cn-training-edge-classification>`
Sometimes you wish to predict the attributes on the edges of the graph. In that Sometimes you wish to predict the attributes on the edges of the graph. In that
case, you would like to have an *edge classification/regression* model. case, you would like to have an *edge classification/regression* model.
...@@ -39,7 +41,7 @@ Model Implementation Difference from Node Classification ...@@ -39,7 +41,7 @@ Model Implementation Difference from Node Classification
Assuming that you compute the node representation with the model from Assuming that you compute the node representation with the model from
the previous section, you only need to write another component that the previous section, you only need to write another component that
computes the edge prediction with the computes the edge prediction with the
:meth:`~dgl.DGLHeteroGraph.apply_edges` method. :meth:`~dgl.DGLGraph.apply_edges` method.
For instance, if you would like to compute a score for each edge for For instance, if you would like to compute a score for each edge for
edge regression, the following code computes the dot product of incident edge regression, the following code computes the dot product of incident
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
5.4 Graph Classification 5.4 Graph Classification
---------------------------------- ----------------------------------
:ref:`(中文版) <guide_cn-training-graph-classification>`
Instead of a big single graph, sometimes one might have the data in the Instead of a big single graph, sometimes one might have the data in the
form of multiple graphs, for example a list of different types of form of multiple graphs, for example a list of different types of
communities of people. By characterizing the friendship among people in communities of people. By characterizing the friendship among people in
...@@ -215,9 +217,6 @@ graphs in mini-batches. ...@@ -215,9 +217,6 @@ graphs in mini-batches.
drop_last=False, drop_last=False,
shuffle=True) shuffle=True)
Loop
^^^^
Training loop then simply involves iterating over the dataloader and Training loop then simply involves iterating over the dataloader and
updating the model. updating the model.
......
...@@ -3,9 +3,11 @@ ...@@ -3,9 +3,11 @@
5.3 Link Prediction 5.3 Link Prediction
--------------------------- ---------------------------
:ref:`(中文版) <guide_cn-training-link-prediction>`
In some other settings you may want to predict whether an edge exists In some other settings you may want to predict whether an edge exists
between two given nodes or not. Such model is called a *link prediction* between two given nodes or not. Such task is called a *link prediction*
model. task.
Overview Overview
~~~~~~~~ ~~~~~~~~
...@@ -48,9 +50,6 @@ feedback <https://arxiv.org/ftp/arxiv/papers/1205/1205.2618.pdf>`__ or ...@@ -48,9 +50,6 @@ feedback <https://arxiv.org/ftp/arxiv/papers/1205/1205.2618.pdf>`__ or
estimation <http://proceedings.mlr.press/v9/gutmann10a/gutmann10a.pdf>`__ estimation <http://proceedings.mlr.press/v9/gutmann10a/gutmann10a.pdf>`__
is. is.
Model Implementation Difference from Edge Classification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The neural network model to compute the score between :math:`u` and The neural network model to compute the score between :math:`u` and
:math:`v` is identical to the edge regression model described :math:`v` is identical to the edge regression model described
:ref:`above <guide-training-edge-classification>`. :ref:`above <guide-training-edge-classification>`.
...@@ -86,8 +85,8 @@ distribution. ...@@ -86,8 +85,8 @@ distribution.
src, dst = graph.edges() src, dst = graph.edges()
neg_src = src.repeat_interleave(k) neg_src = src.repeat_interleave(k)
neg_dst = torch.randint(0, graph.number_of_nodes(), (len(src) * k,)) neg_dst = torch.randint(0, graph.num_nodes(), (len(src) * k,))
return dgl.graph((neg_src, neg_dst), num_nodes=graph.number_of_nodes()) return dgl.graph((neg_src, neg_dst), num_nodes=graph.num_nodes())
The model that predicts edge scores is the same as that of edge The model that predicts edge scores is the same as that of edge
classification/regression. classification/regression.
...@@ -111,7 +110,7 @@ computes loss. ...@@ -111,7 +110,7 @@ computes loss.
def compute_loss(pos_score, neg_score): def compute_loss(pos_score, neg_score):
# Margin loss # Margin loss
n_edges = pos_score.shape[0] n_edges = pos_score.shape[0]
return (1 - neg_score.view(n_edges, -1) + pos_score.unsqueeze(1)).clamp(min=0).mean() return (1 - pos_score.unsqueeze(1) + neg_score.view(n_edges, -1)).clamp(min=0).mean()
node_features = graph.ndata['feat'] node_features = graph.ndata['feat']
n_features = node_features.shape[1] n_features = node_features.shape[1]
...@@ -169,10 +168,10 @@ edge type you are performing link prediction on as well. ...@@ -169,10 +168,10 @@ edge type you are performing link prediction on as well.
utype, _, vtype = etype utype, _, vtype = etype
src, dst = graph.edges(etype=etype) src, dst = graph.edges(etype=etype)
neg_src = src.repeat_interleave(k) neg_src = src.repeat_interleave(k)
neg_dst = torch.randint(0, graph.number_of_nodes(vtype), (len(src) * k,)) neg_dst = torch.randint(0, graph.num_nodes(vtype), (len(src) * k,))
return dgl.heterograph( return dgl.heterograph(
{etype: (neg_src, neg_dst)}, {etype: (neg_src, neg_dst)},
num_nodes_dict={ntype: graph.number_of_nodes(ntype) for ntype in graph.ntypes}) num_nodes_dict={ntype: graph.num_nodes(ntype) for ntype in graph.ntypes})
The model is a bit different from that in edge classification on The model is a bit different from that in edge classification on
heterogeneous graphs since you need to specify edge type where you heterogeneous graphs since you need to specify edge type where you
...@@ -196,7 +195,7 @@ The training loop is similar to that of homogeneous graphs. ...@@ -196,7 +195,7 @@ The training loop is similar to that of homogeneous graphs.
def compute_loss(pos_score, neg_score): def compute_loss(pos_score, neg_score):
# Margin loss # Margin loss
n_edges = pos_score.shape[0] n_edges = pos_score.shape[0]
return (1 - neg_score.view(n_edges, -1) + pos_score.unsqueeze(1)).clamp(min=0).mean() return (1 - pos_score.unsqueeze(1) + neg_score.view(n_edges, -1)).clamp(min=0).mean()
k = 5 k = 5
model = Model(10, 20, 5, hetero_graph.etypes) model = Model(10, 20, 5, hetero_graph.etypes)
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
5.1 Node Classification/Regression 5.1 Node Classification/Regression
-------------------------------------------------- --------------------------------------------------
:ref:`(中文版) <guide_cn-training-node-classification>`
One of the most popular and widely adopted tasks for graph neural One of the most popular and widely adopted tasks for graph neural
networks is node classification, where each node in the networks is node classification, where each node in the
training/validation/test set is assigned a ground truth category from a training/validation/test set is assigned a ground truth category from a
......
.. _guide-training: .. _guide-training:
Chapter 5: Training Graph Neural Networks Chapter 5: Training Graph Neural Networks
===================================================== =====================================================
:ref:`(中文版) <guide_cn-training>`
Overview Overview
-------- --------
......
...@@ -49,7 +49,7 @@ SAGEConv的数学公式如下: ...@@ -49,7 +49,7 @@ SAGEConv的数学公式如下:
.. math:: .. math::
h_{dst}^{(l+1)} = \mathrm{norm}(h_{dst}^{l}) h_{dst}^{(l+1)} = \mathrm{norm}(h_{dst}^{l+1})
源节点特征 ``feat_src`` 和目标节点特征 ``feat_dst`` 需要根据图类型被指定。 源节点特征 ``feat_src`` 和目标节点特征 ``feat_dst`` 需要根据图类型被指定。
用于指定图类型并将 ``feat`` 扩展为 ``feat_src`` 和 ``feat_dst`` 的函数是 :meth:`~dgl.utils.expand_as_pair`。 用于指定图类型并将 ``feat`` 扩展为 ``feat_src`` 和 ``feat_dst`` 的函数是 :meth:`~dgl.utils.expand_as_pair`。
......
.. _guide_cn-training-edge-classification:
5.2 边分类/回归
---------------------------------------------
:ref:`(English Version) <guide-training-edge-classification>`
有时用户希望预测图中边的属性值,这种情况下,用户需要构建一个边分类/回归的模型。
以下代码生成了一个随机图用于演示边分类/回归。
.. code:: ipython3
src = np.random.randint(0, 100, 500)
dst = np.random.randint(0, 100, 500)
# 同时建立反向边
edge_pred_graph = dgl.graph((np.concatenate([src, dst]), np.concatenate([dst, src])))
# 建立点和边特征,以及边的标签
edge_pred_graph.ndata['feature'] = torch.randn(100, 10)
edge_pred_graph.edata['feature'] = torch.randn(1000, 10)
edge_pred_graph.edata['label'] = torch.randn(1000)
# 进行训练、验证和测试集划分
edge_pred_graph.edata['train_mask'] = torch.zeros(1000, dtype=torch.bool).bernoulli(0.6)
概述
~~~~~~~~
上一节介绍了如何使用多层GNN进行节点分类。同样的方法也可以被用于计算任何节点的隐藏表示。
并从边的两个端点的表示,通过计算得出对边属性的预测。
对一条边计算预测值最常见的情况是将预测表示为一个函数,函数的输入为两个端点的表示,
输入还可以包括边自身的特征。
与节点分类在模型实现上的差别
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果用户使用上一节中的模型计算了节点的表示,那么用户只需要再编写一个用
:meth:`~dgl.DGLGraph.apply_edges` 方法计算边预测的组件即可进行边分类/回归任务。
例如,对于边回归任务,如果用户想为每条边计算一个分数,可按下面的代码对每一条边计算它的两端节点隐藏表示的点积来作为分数。
.. code:: python
import dgl.function as fn
class DotProductPredictor(nn.Module):
def forward(self, graph, h):
# h是从5.1节的GNN模型中计算出的节点表示
with graph.local_scope():
graph.ndata['h'] = h
graph.apply_edges(fn.u_dot_v('h', 'h', 'score'))
return graph.edata['score']
用户也可以使用MLP(多层感知机)对每条边生成一个向量表示(例如,作为一个未经过归一化的类别的分布)
并在下游任务中使用。
.. code:: python
class MLPPredictor(nn.Module):
def __init__(self, in_features, out_classes):
super().__init__()
self.W = nn.Linear(in_features * 2, out_classes)
def apply_edges(self, edges):
h_u = edges.src['h']
h_v = edges.dst['h']
score = self.W(torch.cat([h_u, h_v], 1))
return {'score': score}
def forward(self, graph, h):
# h是从5.1节的GNN模型中计算出的节点表示
with graph.local_scope():
graph.ndata['h'] = h
graph.apply_edges(self.apply_edges)
return graph.edata['score']
模型的训练
~~~~~~~~~~~~~
给定计算节点和边上表示的模型后,用户可以轻松地编写在所有边上进行预测的全图训练代码。
以下代码用了 :ref:`guide_cn-message-passing` 中定义的 ``SAGE`` 作为节点表示计算模型以及前一小节中定义的
``DotPredictor`` 作为边预测模型。
.. code:: python
class Model(nn.Module):
def __init__(self, in_features, hidden_features, out_features):
super().__init__()
self.sage = SAGE(in_features, hidden_features, out_features)
self.pred = DotProductPredictor()
def forward(self, g, x):
h = self.sage(g, x)
return self.pred(g, h)
在训练模型时可以使用布尔掩码区分训练、验证和测试数据集。该例子里省略了训练早停和模型保存部分的代码。
.. code:: python
node_features = edge_pred_graph.ndata['feature']
edge_label = edge_pred_graph.edata['label']
train_mask = edge_pred_graph.edata['train_mask']
model = Model(10, 20, 5)
opt = torch.optim.Adam(model.parameters())
for epoch in range(10):
pred = model(edge_pred_graph, node_features)
loss = ((pred[train_mask] - edge_label[train_mask]) ** 2).mean()
opt.zero_grad()
loss.backward()
opt.step()
print(loss.item())
.. _guide_cn-training-edge-classification-heterogeneous-graph:
异构图上的边预测模型的训练
~~~~~~~~~~~~~~~~~~~~~~~~~
例如想在某一特定类型的边上进行分类任务,用户只需要计算所有节点类型的节点表示,
然后同样通过调用 :meth:`~dgl.DGLHeteroGraph.apply_edges` 方法计算预测值即可。
唯一的区别是在调用 ``apply_edges`` 时需要指定边的类型。
.. code:: python
class HeteroDotProductPredictor(nn.Module):
def forward(self, graph, h, etype):
# h是从5.1节中对每种类型的边所计算的节点表示
with graph.local_scope():
graph.ndata['h'] = h #一次性为所有节点类型的 'h'赋值
graph.apply_edges(fn.u_dot_v('h', 'h', 'score'), etype=etype)
return graph.edges[etype].data['score']
同样地,用户也可以编写一个 ``HeteroMLPPredictor``
.. code:: python
class MLPPredictor(nn.Module):
def __init__(self, in_features, out_classes):
super().__init__()
self.W = nn.Linear(in_features * 2, out_classes)
def apply_edges(self, edges):
h_u = edges.src['h']
h_v = edges.dst['h']
score = self.W(torch.cat([h_u, h_v], 1))
return {'score': score}
def forward(self, graph, h, etype):
# h是从5.1节中对异构图的每种类型的边所计算的节点表示
with graph.local_scope():
graph.ndata['h'] = h #一次性为所有节点类型的 'h'赋值
graph.apply_edges(self.apply_edges, etype=etype)
return graph.edges[etype].data['score']
在某种类型的边上为每一条边预测的端到端模型的定义如下所示:
.. code:: python
class Model(nn.Module):
def __init__(self, in_features, hidden_features, out_features, rel_names):
super().__init__()
self.sage = RGCN(in_features, hidden_features, out_features, rel_names)
self.pred = HeteroDotProductPredictor()
def forward(self, g, x, etype):
h = self.sage(g, x)
return self.pred(g, h, etype)
使用模型时只需要简单地向模型提供一个包含节点类型和数据特征的字典。
.. code:: python
model = Model(10, 20, 5, hetero_graph.etypes)
user_feats = hetero_graph.nodes['user'].data['feature']
item_feats = hetero_graph.nodes['item'].data['feature']
label = hetero_graph.edges['click'].data['label']
train_mask = hetero_graph.edges['click'].data['train_mask']
node_features = {'user': user_feats, 'item': item_feats}
训练部分和同构图的训练基本一致。例如,如果用户想预测边类型为 ``click`` 的边的标签,只需要按下例编写代码。
.. code:: python
opt = torch.optim.Adam(model.parameters())
for epoch in range(10):
pred = model(hetero_graph, node_features, 'click')
loss = ((pred[train_mask] - label[train_mask]) ** 2).mean()
opt.zero_grad()
loss.backward()
opt.step()
print(loss.item())
在异构图中预测已有边的类型
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
预测图中已经存在的边属于哪个类型是一个非常常见的任务类型。例如,根据
:ref:`本章的异构图样例数据 <guide_cn-training-heterogeneous-graph-example>`
用户的任务是给定一条连接 ``user`` 节点和 ``item`` 节点的边,预测它的类型是 ``click`` 还是 ``dislike``
这个例子是评分预测的一个简化版本,在推荐场景中很常见。
边类型预测的第一步仍然是计算节点表示。可以通过类似
:ref:`节点分类的RGCN模型 <guide_cn-training-rgcn-node-classification>`
这一章中提到的图卷积网络获得。第二步是计算边上的预测值。
在这里可以复用上述提到的 ``HeteroDotProductPredictor``
这里需要注意的是输入的图数据不能包含边的类型信息,
因此需要将所要预测的边类型( ``click`` ``dislike``)合并成一种边的图,
并为每条边计算出每种边类型的可能得分。下面的例子使用一个拥有 ``user``
``item`` 两种节点类型和一种边类型的图。该边类型是通过合并所有从 ``user``
``item`` 的边类型( ``like`` ``dislike``)得到。
用户可以很方便地用关系切片的方式创建这个图。
.. code:: python
dec_graph = hetero_graph['user', :, 'item']
这个方法会返回一个异构图,它具有 ``user`` ``item`` 两种节点类型,
以及把它们之间的所有边的类型进行合并后的单一边类型。
由于上面这行代码将原来的边类型存成边特征 ``dgl.ETYPE``,用户可以将它作为标签使用。
.. code:: python
edge_label = dec_graph.edata[dgl.ETYPE]
将上述图作为边类型预测模块的输入,用户可以按如下方式编写预测模块:
.. code:: python
class HeteroMLPPredictor(nn.Module):
def __init__(self, in_dims, n_classes):
super().__init__()
self.W = nn.Linear(in_dims * 2, n_classes)
def apply_edges(self, edges):
x = torch.cat([edges.src['h'], edges.dst['h']], 1)
y = self.W(x)
return {'score': y}
def forward(self, graph, h):
# h是从5.1节中对异构图的每种类型的边所计算的节点表示
with graph.local_scope():
graph.ndata['h'] = h #一次性为所有节点类型的 'h'赋值
graph.apply_edges(self.apply_edges)
return graph.edata['score']
结合了节点表示模块和边类型预测模块的模型如下所示:
.. code:: python
class Model(nn.Module):
def __init__(self, in_features, hidden_features, out_features, rel_names):
super().__init__()
self.sage = RGCN(in_features, hidden_features, out_features, rel_names)
self.pred = HeteroMLPPredictor(out_features, len(rel_names))
def forward(self, g, x, dec_graph):
h = self.sage(g, x)
return self.pred(dec_graph, h)
训练部分如下所示:
.. code:: python
model = Model(10, 20, 5, hetero_graph.etypes)
user_feats = hetero_graph.nodes['user'].data['feature']
item_feats = hetero_graph.nodes['item'].data['feature']
node_features = {'user': user_feats, 'item': item_feats}
opt = torch.optim.Adam(model.parameters())
for epoch in range(10):
logits = model(hetero_graph, node_features, dec_graph)
loss = F.cross_entropy(logits, edge_label)
opt.zero_grad()
loss.backward()
opt.step()
print(loss.item())
读者可以进一步参考
`Graph Convolutional Matrix
Completion <https://github.com/dmlc/dgl/tree/master/examples/pytorch/gcmc>`__
这一示例来了解如何预测异构图中的边类型。
`模型实现文件中 <https://github.com/dmlc/dgl/tree/master/examples/pytorch/gcmc>`__
的节点表示模块称作 ``GCMCLayer``。边类型预测模块称作 ``BiDecoder``
虽然这两个模块都比上述的示例代码要复杂,但其基本思想和本章描述的流程是一致的。
.. _guide_cn-training-graph-classification:
5.4 整图分类
----------------------------------
:ref:`(English Version) <guide-training-graph-classification>`
许多场景中的图数据是由多个图组成,而不是单个的大图数据。例如不同类型的人群社区。
通过用图刻画同一社区里人与人间的友谊,可以得到多张用于分类的图。
在这个场景里,整图分类模型可以识别社区的类型,即根据结构和整体信息对图进行分类。
概述
~~~~~~~~
整图分类与节点分类或链接预测的主要区别是:预测结果刻画了整个输入图的属性。
与之前的任务类似,用户还是在节点或边上进行消息传递。但不同的是,整图分类任务还需要得到整个图的表示。
整图分类的处理流程如下图所示:
.. figure:: https://data.dgl.ai/tutorial/batch/graph_classifier.png
:alt: Graph Classification Process
整图分类流程
从左至右,一般流程是:
- 准备一个批次的图;
- 在这个批次的图上进行消息传递以更新节点或边的特征;
- 将一张图里的节点或边特征聚合成整张图的图表示;
- 根据任务设计分类层。
批次的图
^^^^^^^^^^^^^^^
整图分类任务通常需要在很多图上进行训练。如果用户在训练模型时一次仅使用一张图,训练效率会很低。
借用深度学习实践中常用的小批次训练方法,用户可将多张图组成一个批次,在整个图批次上进行一次训练迭代。
使用DGL,用户可将一系列的图建立成一个图批次。一个图批次可以被看作是一张大图,图中的每个连通子图对应一张原始小图。
.. figure:: https://data.dgl.ai/tutorial/batch/batch.png
:alt: Batched Graph
批次化的图
图读出
^^^^^^^^^^^^^
数据集中的每一张图都有它独特的结构和节点与边的特征。为了完成单个图的预测,通常会聚合并汇总单个图尽可能多的信息。
这类操作叫做“读出”。常见的聚合方法包括:对所有节点或边特征求和、取平均值、逐元素求最大值或最小值。
给定一张图 :math:`g`,对它所有节点特征取平均值的聚合读出公式如下:
.. math:: h_g = \frac{1}{|\mathcal{V}|}\sum_{v\in \mathcal{V}}h_v
其中,:math:`h_g` 是图 :math:`g` 的表征, :math:`\mathcal{V}` 是图 :math:`g` 中节点的集合,
:math:`h_v` 是节点 :math:`v` 的特征。
DGL内置了常见的图读出函数,例如 :func:`dgl.readout_nodes` 就实现了上述的平均值读出计算。
在得到 :math:`h_g` 后,用户可将其传给一个多层感知机(MLP)来获得分类输出。
编写神经网络模型
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
模型的输入是带节点和边特征的批次化图。需要注意的是批次化图中的节点和边属性没有批次大小对应的维度。
模型中应特别注意以下几点。
批次化图上的计算
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
首先,一个批次中不同的图是完全分开的,即任意两个图之间没有边连接。
根据这个良好的性质,所有消息传递函数(的计算)仍然具有相同的结果。
其次,读出函数会分别作用在图批次中的每张图上。假设批次大小为 :math:`B`,要聚合的特征大小为 :math:`D`
则图读出的张量形状为 :math:`(B, D)`
.. code:: python
import dgl
import torch
g1 = dgl.graph(([0, 1], [1, 0]))
g1.ndata['h'] = torch.tensor([1., 2.])
g2 = dgl.graph(([0, 1], [1, 2]))
g2.ndata['h'] = torch.tensor([1., 2., 3.])
dgl.readout_nodes(g1, 'h')
# tensor([3.]) # 1 + 2
bg = dgl.batch([g1, g2])
dgl.readout_nodes(bg, 'h')
# tensor([3., 6.]) # [1 + 2, 1 + 2 + 3]
最后,批次化图中的每个节点或边特征张量均通过将所有图上的相应特征拼接得到。
.. code:: python
bg.ndata['h']
# tensor([1., 2., 1., 2., 3.])
模型定义
^^^^^^^^^^^^^^^^
了解了上述计算规则后,用户可以定义一个非常简单的模型。
.. code:: python
import dgl.nn.pytorch as dglnn
import torch.nn as nn
class Classifier(nn.Module):
def __init__(self, in_dim, hidden_dim, n_classes):
super(Classifier, self).__init__()
self.conv1 = dglnn.GraphConv(in_dim, hidden_dim)
self.conv2 = dglnn.GraphConv(hidden_dim, hidden_dim)
self.classify = nn.Linear(hidden_dim, n_classes)
def forward(self, g, h):
# 应用图卷积和激活函数
h = F.relu(self.conv1(g, h))
h = F.relu(self.conv2(g, h))
with g.local_scope():
g.ndata['h'] = h
# 使用平均读出计算图表示
hg = dgl.mean_nodes(g, 'h')
return self.classify(hg)
模型的训练
~~~~~~~~~~~~~
数据加载
^^^^^^^^^^^^
模型定义完成后,用户就可以开始训练模型。由于整图分类处理的是很多相对较小的图,而不是一个大图,
因此通常可以在随机抽取的小批次图上进行高效的训练,而无需设计复杂的图采样算法。
以下例子中使用了 :ref:`guide_cn-data-pipeline` 中的整图分类数据集。
.. code:: python
import dgl.data
dataset = dgl.data.GINDataset('MUTAG', False)
整图分类数据集里的每个数据点是一个图和它对应标签的元组。为提升数据加载速度,
用户可以在DataLoader里自定义collate函数。
.. code:: python
def collate(samples):
graphs, labels = map(list, zip(*samples))
batched_graph = dgl.batch(graphs)
batched_labels = torch.tensor(labels)
return batched_graph, batched_labels
随后用户可以创建一个以小批次遍历整个图数据集的DataLoader
.. code:: python
from torch.utils.data import DataLoader
dataloader = DataLoader(
dataset,
batch_size=1024,
collate_fn=collate,
drop_last=False,
shuffle=True)
训练过程包括遍历dataloader和更新模型参数的部分。
.. code:: python
import torch.nn.functional as F
# 这仅是个例子,特征尺寸是7
model = Classifier(7, 20, 5)
opt = torch.optim.Adam(model.parameters())
for epoch in range(20):
for batched_graph, labels in dataloader:
feats = batched_graph.ndata['attr'].float()
logits = model(batched_graph, feats)
loss = F.cross_entropy(logits, labels)
opt.zero_grad()
loss.backward()
opt.step()
DGL实现了一个整图分类的样例:
`DGLGIN样例 <https://github.com/dmlc/dgl/tree/master/examples/pytorch/gin>`__
模型训练的代码请参考位于
`main.py <https://github.com/dmlc/dgl/blob/master/examples/pytorch/gin/main.py>`__ 源文件中的 ``train`` 函数。
模型实现位于
`gin.py <https://github.com/dmlc/dgl/blob/master/examples/pytorch/gin/gin.py>`__
其中使用了更多的模块组件,例如使用 :class:`dgl.nn.pytorch.GINConv`
模块作为图卷积层(DGL同样支持它在MXNetTensorFlow后端里的实现)、批量归一化等。
异构图上的整图分类模型的训练
~~~~~~~~~~~~~~~~~~~
在异构图上做整图分类和在同构图上做整图分类略有不同。用户除了需要使用异构图卷积模块,还需要在读出函数中聚合不同类别的节点。
以下代码演示了如何对每种节点类型的节点表示取平均值并求和。
.. code:: python
class RGCN(nn.Module):
def __init__(self, in_feats, hid_feats, out_feats, rel_names):
super().__init__()
self.conv1 = dglnn.HeteroGraphConv({
rel: dglnn.GraphConv(in_feats, hid_feats)
for rel in rel_names}, aggregate='sum')
self.conv2 = dglnn.HeteroGraphConv({
rel: dglnn.GraphConv(hid_feats, out_feats)
for rel in rel_names}, aggregate='sum')
def forward(self, graph, inputs):
# inputs是节点的特征
h = self.conv1(graph, inputs)
h = {k: F.relu(v) for k, v in h.items()}
h = self.conv2(graph, h)
return h
class HeteroClassifier(nn.Module):
def __init__(self, in_dim, hidden_dim, n_classes, rel_names):
super().__init__()
self.rgcn = RGCN(in_dim, hidden_dim, hidden_dim, rel_names)
self.classify = nn.Linear(hidden_dim, n_classes)
def forward(self, g):
h = g.ndata['feat']
h = self.rgcn(g, h)
with g.local_scope():
g.ndata['h'] = h
# 通过平均读出值来计算单图的表征
hg = 0
for ntype in g.ntypes:
hg = hg + dgl.mean_nodes(g, 'h', ntype=ntype)
return self.classify(hg)
剩余部分的训练代码和同构图代码相同。
.. code:: python
# etypes是一个列表,元素是字符串类型的边类型
model = HeteroClassifier(10, 20, 5, etypes)
opt = torch.optim.Adam(model.parameters())
for epoch in range(20):
for batched_graph, labels in dataloader:
logits = model(batched_graph)
loss = F.cross_entropy(logits, labels)
opt.zero_grad()
loss.backward()
opt.step()
.. _guide_cn-training-link-prediction:
5.3 链接预测
---------------------------
:ref:`(English Version) <guide-training-link-prediction>`
在某些场景中,用户可能希望预测给定节点之间是否存在边,这样的任务称作 **链接预测** 任务。
概述
~~~~~~~~
基于GNN的链接预测模型的基本思想是通过使用所需预测的节点对
:math:`u`, :math:`v` 的节点表示 :math:`\boldsymbol{h}_u^{(L)}`
:math:`\boldsymbol{h}_v^{(L)}`,计算它们之间存在链接可能性的得分 :math:`y_{u,v}`
其中 :math:`\boldsymbol{h}_u^{(L)}` :math:`\boldsymbol{h}_v^{(L)}` 由多层GNN计算得出。
.. math::
y_{u,v} = \phi(\boldsymbol{h}_u^{(L)}, \boldsymbol{h}_v^{(L)})
本节把节点 :math:`u` :math:`v` 之间存在连接可能性的 *得分* 记作 :math:`y_{u,v}`
训练一个链接预测模型涉及到比对两个相连接节点之间的得分与任意一对节点之间的得分的差异。
例如,给定一条连接 :math:`u` :math:`v` 的边,一个好的模型希望 :math:`u` :math:`v` 之间的得分要高于
:math:`u` 和从一个任意的噪声分布 :math:`v′∼Pn(v)` 中所采样的节点 :math:`v` 之间的得分。
这样的方法称作 *负采样*
许多损失函数都可以实现上述目标,包括但不限于。
- 交叉熵损失:
:math:`\mathcal{L} = - \log \sigma (y_{u,v}) - \sum_{v_i \sim P_n(v), i=1,\dots,k}\log \left[ 1 - \sigma (y_{u,v_i})\right]`
- 贝叶斯个性化排序损失:
:math:`\mathcal{L} = \sum_{v_i \sim P_n(v), i=1,\dots,k} - \log \sigma (y_{u,v} - y_{u,v_i})`
- 间隔损失:
:math:`\mathcal{L} = \sum_{v_i \sim P_n(v), i=1,\dots,k} \max(0, M - y_{u, v} + y_{u, v_i})`,
其中 :math:`M` 是常数项超参数。
如果用户熟悉 `implicit feedback <https://arxiv.org/ftp/arxiv/papers/1205/1205.2618.pdf>`__
`noise-contrastive estimation <http://proceedings.mlr.press/v9/gutmann10a/gutmann10a.pdf>`__
可能会发现这些工作的想法都很类似。
计算 :math:`u` :math:`v` 之间分数的神经网络模型与 :ref:`guide_cn-training-edge-classification`
中所述的边回归模型相同。
下面是使用点积计算边得分的例子。
.. code:: python
class DotProductPredictor(nn.Module):
def forward(self, graph, h):
# h是从5.1节的GNN模型中计算出的节点表示
with graph.local_scope():
graph.ndata['h'] = h
graph.apply_edges(fn.u_dot_v('h', 'h', 'score'))
return graph.edata['score']
模型的训练
~~~~~~~~~~~~~
因为上述的得分预测模型在图上进行计算,用户需要将负采样的样本表示为另外一个图,
其中包含所有负采样的节点对作为边。
下面的例子展示了将负采样的样本表示为一个图。每一条边 :math:`(u,v)` 都有 :math:`k`
个对应的负采样样本 :math:`(u,v_i)`,其中 :math:`v_i` 是从均匀分布中采样的。
.. code:: python
def construct_negative_graph(graph, k):
src, dst = graph.edges()
neg_src = src.repeat_interleave(k)
neg_dst = torch.randint(0, graph.num_nodes(), (len(src) * k,))
return dgl.graph((neg_src, neg_dst), num_nodes=graph.num_nodes())
预测边得分的模型和边分类/回归模型中的预测边得分模型相同。
.. code:: python
class Model(nn.Module):
def __init__(self, in_features, hidden_features, out_features):
super().__init__()
self.sage = SAGE(in_features, hidden_features, out_features)
self.pred = DotProductPredictor()
def forward(self, g, neg_g, x):
h = self.sage(g, x)
return self.pred(g, h), self.pred(neg_g, h)
训练的循环部分里会重复构建负采样图并计算损失函数值。
.. code:: python
def compute_loss(pos_score, neg_score):
# 间隔损失
n_edges = pos_score.shape[0]
return (1 - pos_score.unsqueeze(1) + neg_score.view(n_edges, -1)).clamp(min=0).mean()
node_features = graph.ndata['feat']
n_features = node_features.shape[1]
k = 5
model = Model(n_features, 100, 100)
opt = torch.optim.Adam(model.parameters())
for epoch in range(10):
negative_graph = construct_negative_graph(graph, k)
pos_score, neg_score = model(graph, negative_graph, node_features)
loss = compute_loss(pos_score, neg_score)
opt.zero_grad()
loss.backward()
opt.step()
print(loss.item())
训练后,节点表示可以通过以下代码获取。
.. code:: python
node_embeddings = model.sage(graph, node_features)
(实际应用中),有着许多使用节点嵌入的方法,例如,训练下游任务的分类器,或为相关实体推荐进行最近邻搜索或最大内积搜索。
异构图上的链接预测模型的训练
~~~~~~~~~~~~~~~~~~~~~~~~~~~
异构图上的链接预测和同构图上的链接预测没有太大区别。下文是在一种边类型上进行预测,
用户可以很容易地将其拓展为对多种边类型上进行预测。
例如,为某一种边类型,用户可以重复使用
:ref:`guide_cn-training-edge-classification-heterogeneous-graph`
里的 ``HeteroDotProductPredictor`` 来计算节点间存在连接可能性的得分。
.. code:: python
class HeteroDotProductPredictor(nn.Module):
def forward(self, graph, h, etype):
# h是从5.1节中对异构图的每种类型的边所计算的节点表示
with graph.local_scope():
graph.ndata['h'] = h
graph.apply_edges(fn.u_dot_v('h', 'h', 'score'), etype=etype)
return graph.edges[etype].data['score']
要执行负采样,用户可以对要进行链接预测的边类型构造一个负采样图。
.. code:: python
def construct_negative_graph(graph, k, etype):
utype, _, vtype = etype
src, dst = graph.edges(etype=etype)
neg_src = src.repeat_interleave(k)
neg_dst = torch.randint(0, graph.num_nodes(vtype), (len(src) * k,))
return dgl.heterograph(
{etype: (neg_src, neg_dst)},
num_nodes_dict={ntype: graph.num_nodes(ntype) for ntype in graph.ntypes})
该模型与异构图上边分类的模型有些不同,因为用户需要指定在哪种边类型上进行链接预测。
.. code:: python
class Model(nn.Module):
def __init__(self, in_features, hidden_features, out_features, rel_names):
super().__init__()
self.sage = RGCN(in_features, hidden_features, out_features, rel_names)
self.pred = HeteroDotProductPredictor()
def forward(self, g, neg_g, x, etype):
h = self.sage(g, x)
return self.pred(g, h, etype), self.pred(neg_g, h, etype)
训练的循环部分和同构图时一致。
.. code:: python
def compute_loss(pos_score, neg_score):
# 间隔损失
n_edges = pos_score.shape[0]
return (1 - pos_score.unsqueeze(1) + neg_score.view(n_edges, -1)).clamp(min=0).mean()
k = 5
model = Model(10, 20, 5, hetero_graph.etypes)
user_feats = hetero_graph.nodes['user'].data['feature']
item_feats = hetero_graph.nodes['item'].data['feature']
node_features = {'user': user_feats, 'item': item_feats}
opt = torch.optim.Adam(model.parameters())
for epoch in range(10):
negative_graph = construct_negative_graph(hetero_graph, k, ('user', 'click', 'item'))
pos_score, neg_score = model(hetero_graph, negative_graph, node_features, ('user', 'click', 'item'))
loss = compute_loss(pos_score, neg_score)
opt.zero_grad()
loss.backward()
opt.step()
print(loss.item())
.. _guide_cn-training-node-classification:
5.1 节点分类/回归
--------------------------------------------------
:ref:`(English Version) <guide-training-node-classification>`
对于图神经网络来说,最常见和被广泛使用的任务之一就是节点分类。
图数据中的训练、验证和测试集中的每个节点都具有从一组预定义的类别中分配的一个类别,即正确的标注。
节点回归任务也类似,训练、验证和测试集中的每个节点都被标注了一个正确的数字。
概述
~~~~~~~~
为了对节点进行分类,图神经网络执行了 :ref:`guide_cn-message-passing`
中介绍的消息传递机制,利用节点自身的特征和其邻节点及边的特征来计算节点的隐藏表示。
消息传递可以重复多轮,以利用更大范围的邻居信息。
编写神经网络模型
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DGL提供了一些内置的图卷积模块,可以完成一轮消息传递计算。
本章中选择 :class:`dgl.nn.pytorch.SAGEConv` 作为演示的样例代码(针对MXNetPyTorch后端也有对应的模块)
它是GraphSAGE模型中使用的图卷积模块。
对于图上的深度学习模型,通常需要一个多层的图神经网络,并在这个网络中要进行多轮的信息传递。
可以通过堆叠图卷积模块来实现这种网络架构,具体如下所示。
.. code:: python
# 构建一个2层的GNN模型
import dgl.nn as dglnn
import torch.nn as nn
import torch.nn.functional as F
class SAGE(nn.Module):
def __init__(self, in_feats, hid_feats, out_feats):
super().__init__()
# 实例化SAGEConvein_feats是输入特征的维度,out_feats是输出特征的维度,aggregator_type是聚合函数的类型
self.conv1 = dglnn.SAGEConv(
in_feats=in_feats, out_feats=hid_feats, aggregator_type='mean')
self.conv2 = dglnn.SAGEConv(
in_feats=hid_feats, out_feats=out_feats, aggregator_type='mean')
def forward(self, graph, inputs):
# 输入是节点的特征
h = self.conv1(graph, inputs)
h = F.relu(h)
h = self.conv2(graph, h)
return h
请注意,这个模型不仅可以做节点分类,还可以为其他下游任务获取隐藏节点表示,如:
:ref:`guide_cn-training-edge-classification`
:ref:`guide_cn-training-link-prediction`
:ref:`guide_cn-training-graph-classification`
关于DGL内置图卷积模块的完整列表,读者可以参考 :ref:`apinn`
有关DGL神经网络模块如何工作,以及如何编写一个自定义的带有消息传递的GNN模块的更多细节,请参考 :ref:`guide_cn-nn` 中的例子。
模型的训练
~~~~~~~~~~~~~
全图(使用所有的节点和边的特征)上的训练只需要使用上面定义的模型进行前向传播计算,并通过在训练节点上比较预测和真实标签来计算损失,从而完成后向传播。
本节使用DGL内置的数据集 :class:`dgl.data.CiteseerGraphDataset` 来展示模型的训练。
节点特征和标签存储在其图上,训练、验证和测试的分割也以布尔掩码的形式存储在图上。这与在
:ref:`guide_cn-data-pipeline` 中的做法类似。
.. code:: python
node_features = graph.ndata['feat']
node_labels = graph.ndata['label']
train_mask = graph.ndata['train_mask']
valid_mask = graph.ndata['val_mask']
test_mask = graph.ndata['test_mask']
n_features = node_features.shape[1]
n_labels = int(node_labels.max().item() + 1)
下面是通过使用准确性来评估模型的一个例子。
.. code:: python
def evaluate(model, graph, features, labels, mask):
model.eval()
with torch.no_grad():
logits = model(graph, features)
logits = logits[mask]
labels = labels[mask]
_, indices = torch.max(logits, dim=1)
correct = torch.sum(indices == labels)
return correct.item() * 1.0 / len(labels)
用户可以按如下方式实现模型的训练。
.. code:: python
model = SAGE(in_feats=n_features, hid_feats=100, out_feats=n_labels)
opt = torch.optim.Adam(model.parameters())
for epoch in range(10):
model.train()
# 使用所有节点(全图)进行前向传播计算
logits = model(graph, node_features)
# 计算损失值
loss = F.cross_entropy(logits[train_mask], node_labels[train_mask])
# 计算验证集的准确度
acc = evaluate(model, graph, node_features, node_labels, valid_mask)
# 进行反向传播计算
opt.zero_grad()
loss.backward()
opt.step()
print(loss.item())
# 如果需要的话,保存训练好的模型。本例中省略。
`DGLGraphSAGE样例 <https://github.com/dmlc/dgl/blob/master/examples/pytorch/graphsage/train_full.py>`__
提供了一个端到端的同构图节点分类的例子。用户可以在 ``GraphSAGE`` 类中看到模型实现的细节。
这个模型具有可调节的层数、dropout概率,以及可定制的聚合函数和非线性函数。
.. _guide_cn-training-rgcn-node-classification:
异构图上的节点分类模型的训练
~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果图是异构的,用户可能希望沿着所有边类型从邻居那里收集消息。
用户可以使用 :class:`dgl.nn.pytorch.HeteroGraphConv`
模块(针对MXNetPyTorch后端也有对应的模块)在所有边类型上执行消息传递,
并为每种边类型使用一种图卷积模块。
下面的代码定义了一个异构图卷积模块。模块首先对每种边类型进行单独的图卷积计算,然后将每种边类型上的消息聚合结果再相加,
并作为所有节点类型的最终结果。
.. code:: python
# Define a Heterograph Conv model
import dgl.nn as dglnn
class RGCN(nn.Module):
def __init__(self, in_feats, hid_feats, out_feats, rel_names):
super().__init__()
# 实例化HeteroGraphConvin_feats是输入特征的维度,out_feats是输出特征的维度,aggregate是聚合函数的类型
self.conv1 = dglnn.HeteroGraphConv({
rel: dglnn.GraphConv(in_feats, hid_feats)
for rel in rel_names}, aggregate='sum')
self.conv2 = dglnn.HeteroGraphConv({
rel: dglnn.GraphConv(hid_feats, out_feats)
for rel in rel_names}, aggregate='sum')
def forward(self, graph, inputs):
# 输入是节点的特征字典
h = self.conv1(graph, inputs)
h = {k: F.relu(v) for k, v in h.items()}
h = self.conv2(graph, h)
return h
``dgl.nn.HeteroGraphConv`` 接收一个节点类型和节点特征张量的字典作为输入,并返回另一个节点类型和节点特征的字典。
本章的 :ref:`guide_cn-training-heterogeneous-graph-example`
中已经有了 ``user`` ``item`` 的特征,用户可用如下代码获取。
.. code:: python
model = RGCN(n_hetero_features, 20, n_user_classes, hetero_graph.etypes)
user_feats = hetero_graph.nodes['user'].data['feature']
item_feats = hetero_graph.nodes['item'].data['feature']
labels = hetero_graph.nodes['user'].data['label']
train_mask = hetero_graph.nodes['user'].data['train_mask']
然后,用户可以简单地按如下形式进行前向传播计算:
.. code:: python
node_features = {'user': user_feats, 'item': item_feats}
h_dict = model(hetero_graph, {'user': user_feats, 'item': item_feats})
h_user = h_dict['user']
h_item = h_dict['item']
异构图上模型的训练和同构图的模型训练是一样的,只是这里使用了一个包括节点表示的字典来计算预测值。
例如,如果只预测 ``user`` 节点的类别,用户可以从返回的字典中提取 ``user`` 的节点嵌入。
.. code:: python
opt = torch.optim.Adam(model.parameters())
for epoch in range(5):
model.train()
# 使用所有节点的特征进行前向传播计算,并提取输出的user节点嵌入
logits = model(hetero_graph, node_features)['user']
# 计算损失值
loss = F.cross_entropy(logits[train_mask], labels[train_mask])
# 计算验证集的准确度。在本例中省略。
# 进行反向传播计算
opt.zero_grad()
loss.backward()
opt.step()
print(loss.item())
# 如果需要的话,保存训练好的模型。本例中省略。
DGL提供了一个用于节点分类的RGCN的端到端的例子
`RGCN <https://github.com/dmlc/dgl/blob/master/examples/pytorch/rgcn-hetero/entity_classify.py>`__
。用户可以在 `RGCN模型实现文件
<https://github.com/dmlc/dgl/blob/master/examples/pytorch/rgcn-hetero/model.py>`__
中查看异构图卷积 ``RelGraphConvLayer`` 的具体定义。
.. _guide_cn-training:
第5章:训练图神经网络
=====================================================
:ref:`(English Version) <guide-training>`
概述
--------
本章通过使用 :ref:`guide_cn-message-passing` 中介绍的消息传递方法和 :ref:`guide_cn-nn` 中介绍的图神经网络模块,
讲解了如何对小规模的图数据进行节点分类、边分类、链接预测和整图分类的图神经网络的训练。
本章假设用户的图以及所有的节点和边特征都能存进GPU。对于无法全部载入的情况,请参考用户指南的 :ref:`guide-minibatch`。
后续章节的内容均假设用户已经准备好了图和节点/边的特征数据。如果用户希望使用DGL提供的数据集或其他兼容
``DGLDataset`` 的数据(如 :ref:`guide_cn-data-pipeline` 所述),
可以使用类似以下代码的方法获取单个图数据集的图数据。
.. code:: python
import dgl
dataset = dgl.data.CiteseerGraphDataset()
graph = dataset[0]
注意: 本章代码使用PyTorch作为DGL的后端框架。
.. _guide_cn-training-heterogeneous-graph-example:
异构图训练的样例数据
~~~~~~~~~~~~~~~~~~~~~~~~~
有时用户会想在异构图上进行图神经网络的训练。本章会以下面代码所创建的一个异构图为例,来演示如何进行节点分类、边分类和链接预测的训练。
这个 ``hetero_graph`` 异构图有以下这些边的类型:
- ``('user', 'follow', 'user')``
- ``('user', 'followed-by', 'user')``
- ``('user', 'click', 'item')``
- ``('item', 'clicked-by', 'user')``
- ``('user', 'dislike', 'item')``
- ``('item', 'disliked-by', 'user')``
.. code:: python
import numpy as np
import torch
n_users = 1000
n_items = 500
n_follows = 3000
n_clicks = 5000
n_dislikes = 500
n_hetero_features = 10
n_user_classes = 5
n_max_clicks = 10
follow_src = np.random.randint(0, n_users, n_follows)
follow_dst = np.random.randint(0, n_users, n_follows)
click_src = np.random.randint(0, n_users, n_clicks)
click_dst = np.random.randint(0, n_items, n_clicks)
dislike_src = np.random.randint(0, n_users, n_dislikes)
dislike_dst = np.random.randint(0, n_items, n_dislikes)
hetero_graph = dgl.heterograph({
('user', 'follow', 'user'): (follow_src, follow_dst),
('user', 'followed-by', 'user'): (follow_dst, follow_src),
('user', 'click', 'item'): (click_src, click_dst),
('item', 'clicked-by', 'user'): (click_dst, click_src),
('user', 'dislike', 'item'): (dislike_src, dislike_dst),
('item', 'disliked-by', 'user'): (dislike_dst, dislike_src)})
hetero_graph.nodes['user'].data['feature'] = torch.randn(n_users, n_hetero_features)
hetero_graph.nodes['item'].data['feature'] = torch.randn(n_items, n_hetero_features)
hetero_graph.nodes['user'].data['label'] = torch.randint(0, n_user_classes, (n_users,))
hetero_graph.edges['click'].data['label'] = torch.randint(1, n_max_clicks, (n_clicks,)).float()
# 在user类型的节点和click类型的边上随机生成训练集的掩码
hetero_graph.nodes['user'].data['train_mask'] = torch.zeros(n_users, dtype=torch.bool).bernoulli(0.6)
hetero_graph.edges['click'].data['train_mask'] = torch.zeros(n_clicks, dtype=torch.bool).bernoulli(0.6)
本章路线图
------------
本章共有四节,每节对应一种图学习任务。
* :ref:`guide_cn-training-node-classification`
* :ref:`guide_cn-training-edge-classification`
* :ref:`guide_cn-training-link-prediction`
* :ref:`guide_cn-training-graph-classification`
.. toctree::
:maxdepth: 1
:hidden:
:glob:
training-node
training-edge
training-link
training-graph
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