解决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

相关文章

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到

解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题

《解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题》:本文主要介绍解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4... 目录未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘打开pom.XM

XML重复查询一条Sql语句的解决方法

《XML重复查询一条Sql语句的解决方法》文章分析了XML重复查询与日志失效问题,指出因DTO缺少@Data注解导致日志无法格式化、空指针风险及参数穿透,进而引发性能灾难,解决方案为在Controll... 目录一、核心问题:从SQL重复执行到日志失效二、根因剖析:DTO断裂引发的级联故障三、解决方案:修复

IDEA Maven提示:未解析的依赖项的问题及解决

《IDEAMaven提示:未解析的依赖项的问题及解决》:本文主要介绍IDEAMaven提示:未解析的依赖项的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录IDEA Maven提示:未解析的依编程赖项例如总结IDEA Maven提示:未解析的依赖项例如

解决Entity Framework中自增主键的问题

《解决EntityFramework中自增主键的问题》:本文主要介绍解决EntityFramework中自增主键的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录Entity Framework中自增主键问题解决办法1解决办法2解决办法3总结Entity Fram

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错