GTN代码torch-sparse,gpu改cpu

文章讲述了在Windows环境下安装GTN库及其依赖torch-sparse时遇到的问题,涉及编译器错误、MicrosoftC++BuildTools的安装、添加cl.exe到系统路径以及处理torch_geometric库中被弃用的f1_score函数。
摘要由CSDN通过智能技术生成

image

GTN代码torch-sparse,gpu改cpu

跑GTN死活安装不上torch-sparse-old

GitHub - seongjunyun/Graph_Transformer_Networks: Graph Transformer Networks (Authors' PyTorch implementation for the NeurIPS 19 paper)

我们在 pip 上以 torch-sparse-old 的名义上传了带有 backward() 的旧版本的 torch-sparse。

D:\Users\STREAM\anaconda3\Lib\site-packages\torch\utils\cpp_extension.py:381: UserWarning: Error checking compiler version for cl: [WinError 2] 系统找不到指定的文件。
        warnings.warn(f'Error checking compiler version for {compiler}: {error}')
      building 'torch_sparse._convert_cpu' extension
      error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
      [end of output]
 note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for torch_sparse
  Running setup.py clean for torch_sparse
Failed to build torch_sparse
ERROR: Could not build wheels for torch_sparse, which is required to install pyproject.toml-based projects

image

发现是无法生成wheel,安装Microsoft C++ Build Tools也没什么用,本身就安装了VS写过C#软件,但是安装文件里也没有cl.exe。

查找教程发现安装cl.exe需要在Visual Studio Installer 里安装(修改)添加C++生成工具MSVC。

image

可以看到最终cl.exe就在MSVC文件夹里,这里是VS2022,可能具体情况不完全相同,在安装文件里查找cl.exe即可。

image

复制路径,添加到系统变量Path里,设置找不到可以直接在开始处全局搜索。

image

环境变量->系统变量->Path编辑->添加cl.exe所在的目录(文件夹)

torch-sparse和torch-sparse-old就都能安装了。

image

image

然后安装PyG (PyTorch Geometric), 配置把torch版本改成自己的版本。

PyG安装命令生成:Installation — pytorch_geometric documentation (pytorch-geometric.readthedocs.io)
比如我的是torch2.2.0,仅cpu

pip install torch_geometric

# Optional dependencies:
pip install torch_scatter torch_sparse torch_cluster torch_spline_conv -f https://data.pyg.org/whl/torch-2.2.0+cpu.html

安装成功,开始跑代码。
根据github里的命令,这里跑的DBLP,用的快速GTN模型。
报错:

(base) G:\Graph_Transformer_Networks-master> python main.py --dataset DBLP --model FastGTN --num_layers 4 --epoch 100 --lr 0.02 --channel_agg mean --num_channels 2
Traceback (most recent call last):
  File "G:\Graph_Transformer_Networks-master\main.py", line 8, in <module>
    from torch_geometric.utils import f1_score, add_self_loops
ImportError: cannot import name 'f1_score' from 'torch_geometric.utils' (D:\Users\STREAM\anaconda3\Lib\site-packages\torch_geometric\utils\__init__.py)

torch_geometric​中的f1_score​ 已经被弃用,改一下引用,把第八行的f1_score​ 改到第十行。

image官方文档CPU和GPU转换:torch.Tensor — PyTorch 2.2 documentation

main.py替换:
torch.cuda.LongTensor​>>>torch.LongTensor
torch.cuda.FloatTensor​>>>torch.FloatTensor
model.cuda()​>>>model.cpu()

跑通了就是挺慢

image

环境:

image

参考:

UserWarning: Error checking compiler version for cl: [WinError 2] 系统找不到指定的文件。-CSDN博客

找不到 cl.exe 解决办法-CSDN博客

ImportError: cannot import name 'f1_score' from 'torch_geometric.utils' · Issue #41 · seongjunyun/Graph_Transformer_Networks · GitHub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值