解决PyG 报错 from torch_geometric.nn.pool.topk_pool import topk, filter_adj

2023-11-27 10:15

本文主要是介绍解决PyG 报错 from torch_geometric.nn.pool.topk_pool import topk, filter_adj,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题:

使用Pytorch 的 PyG 搭建 图神经网络 报错

can not import topk, filter_adj from torch_geometric.nn.pool.topk_pool 

解决

版本问题 语法变化
topk => SelectTopk
filter_adj => FilterEdges

from torch_geometric.nn.pool.connect import FilterEdges
from torch_geometric.nn.pool.select import SelectTopK

发现替换后不可以
于是进去看SelectTopK\FilterEdges 源码
发现里面有 topk, filter_adj 方法 但是直接 import 也不能用
于是手动写函数出来再 layers.py 里即可运行

def topk(x: Tensor,ratio: Optional[Union[float, int]],batch: Tensor,min_score: Optional[float] = None,tol: float = 1e-7,
) -> Tensor:if min_score is not None:# Make sure that we do not drop all nodes in a graph.scores_max = scatter(x, batch, reduce='max')[batch] - tolscores_min = scores_max.clamp(max=min_score)perm = (x > scores_min).nonzero().view(-1)return permif ratio is not None:num_nodes = scatter(batch.new_ones(x.size(0)), batch, reduce='sum')if ratio >= 1:k = num_nodes.new_full((num_nodes.size(0),), int(ratio))else:k = (float(ratio) * num_nodes.to(x.dtype)).ceil().to(torch.long)x, x_perm = torch.sort(x.view(-1), descending=True)batch = batch[x_perm]batch, batch_perm = torch.sort(batch, descending=False, stable=True)arange = torch.arange(x.size(0), dtype=torch.long, device=x.device)ptr = cumsum(num_nodes)batched_arange = arange - ptr[batch]mask = batched_arange < k[batch]return x_perm[batch_perm[mask]]def filter_adj(edge_index: Tensor,edge_attr: Optional[Tensor],node_index: Tensor,cluster_index: Optional[Tensor] = None,num_nodes: Optional[int] = None,
) -> Tuple[Tensor, Optional[Tensor]]:num_nodes = maybe_num_nodes(edge_index, num_nodes)if cluster_index is None:cluster_index = torch.arange(node_index.size(0),device=node_index.device)mask = node_index.new_full((num_nodes,), -1)mask[node_index] = cluster_indexrow, col = edge_index[0], edge_index[1]row, col = mask[row], mask[col]mask = (row >= 0) & (col >= 0)row, col = row[mask], col[mask]if edge_attr is not None:edge_attr = edge_attr[mask]return torch.stack([row, col], dim=0), edge_attr

参考官方文档

https://pytorch-geometric.readthedocs.io/en/latest/_modules/torch_geometric/nn/pool/topk_pool.html

这篇关于解决PyG 报错 from torch_geometric.nn.pool.topk_pool import topk, filter_adj的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/427496

相关文章

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

Goland debug失效详细解决步骤(合集)

《Golanddebug失效详细解决步骤(合集)》今天用Goland开发时,打断点,以debug方式运行,发现程序并没有断住,程序跳过了断点,直接运行结束,网上搜寻了大量文章,最后得以解决,特此在这... 目录Bug:Goland debug失效详细解决步骤【合集】情况一:Go或Goland架构不对情况二:

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及

Java多线程父线程向子线程传值问题及解决

《Java多线程父线程向子线程传值问题及解决》文章总结了5种解决父子之间数据传递困扰的解决方案,包括ThreadLocal+TaskDecorator、UserUtils、CustomTaskDeco... 目录1 背景2 ThreadLocal+TaskDecorator3 RequestContextH

解决JavaWeb-file.isDirectory()遇到的坑问题

《解决JavaWeb-file.isDirectory()遇到的坑问题》JavaWeb开发中,使用`file.isDirectory()`判断路径是否为文件夹时,需要特别注意:该方法只能判断已存在的文... 目录Jahttp://www.chinasem.cnvaWeb-file.isDirectory()遇

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.