函数介绍
在使用dgl框架自定义卷积算子或者看dgl实现函数的源代码时,总会碰到如下的代码形式:
with graph.local_scope():
if isinstance(feat, tuple):
feat, _ = feat # dst feature not used
cj = graph.srcdata['cj']
ci = graph.dstdata['ci']
if self.device is not None:
cj = cj.to(self.device)
ci = ci.to(self.device)
with graph.local_scope()
函数到底起到什么作用,这里做一下介绍,graph使用dgl定义的一个图,官方的介绍如下:
Enter a local scope context for the graph.
By entering a local scope, any out-place mutation to the feature data will not reflect to the original graph, thus making it easier to use in a function scope (e.g. forward computation of a model).
If set, the local scope will use same initializers for node features and edge features.

本文详细介绍了DGL库中local_scope函数的作用,如何在自定义卷积和模型计算中防止特征值意外修改。通过实例展示了如何在函数中临时操作图特征并保持原图不变,以及为何在位运算下会反映到原图。
最低0.47元/天 解锁文章
3088





