赞
踩
torch_geometric是PyG中必不可少的一个包,也是进行图神经网络学习的必备,然而安装这个包并运行一段简单的代码踩了不少坑,记录一下。
一开始,我直接pip
pip intsall torch_geometric
果然报错,提示没有torch_sparse
很显然是没有安装依赖,于是我去查需要哪些依赖
官网地址:Installation — pytorch_geometric documentation (pytorch-geometric.readthedocs.io)
按照官网给出的代码进行安装
pip install pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv -f https://data.pyg.org/whl/torch-1.13.0+cpu.html
运行依旧没有成功...
于是手动下载
pip uninstall torch-geometric torch-scatter torch-sparse torch-cluster torch-spline-conv
这里注意需要进入D盘,cmd中进入D盘的命令为:
D:
然后cd进入包的下载页面,直接pip install
安装完所有的依赖后记得
pip install torch_geometric
import torch_geometric
依旧报错:
Pytorch AttributeError: module 'torch' has no attribute 'sparse_scs'
这个就很让人费解,于是查看了一下
发现torch.sparse里面确实没有这个模块,那为什么会报错呢?查看一些帖子后发现报这种错一般是因为版本不对,于是,降低torch_geometric版本,果然成功!!
我的版本:
至此就安装成功啦!!
简单跑了个程序
- import torch.nn.functional as F
- class GCN(torch.nn.Module):
- def __init__(self, num_node_features, num_classes):
- super(GCN, self).__init__()
- self.conv1 = GCNConv(num_node_features, 16)
- self.conv2 = GCNConv(16, num_classes)
-
- def forward(self, data):
- x, edge_index = data.x, data.edge_index
- x = self.conv1(x, edge_index)
- x = F.relu(x)
- x = F.dropout(x, training=self.training)
- x = self.conv2(x, edge_index)
- x = F.relu(x)
- x = F.dropout(x, training=self.training)
- x = F.softmax(x, dim=1)
-
- return x
- model = GCN(dataset.num_node_features, dataset.num_classes)
- print(model)
-
- def train(model, data):
- optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=1e-4)
- loss_function = torch.nn.CrossEntropyLoss()
- model.train()
- for epoch in range(200):
- out = model(data)
- optimizer.zero_grad()
- loss = loss_function(out[data.train_mask], data.y[data.train_mask])
- loss.backward()
- optimizer.step()
-
- print('Epoch {:03d} loss {:.4f}'.format(epoch, loss.item()))
-
- train(model,data)
运行结果:
没问题!!接下来就可以建立一个自己的图神经网络啦~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。