torch_geometric是PyG中必不可少的一個(gè)包,也是進(jìn)行圖神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)的必備,然而安裝這個(gè)包并運(yùn)行一段簡(jiǎn)單的代碼踩了不少坑,記錄一下。
1、安裝torch_geometric
一開始,我直接pip
pip intsall torch_geometric
果然報(bào)錯(cuò),提示沒有torch_sparse
很顯然是沒有安裝依賴,于是我去查需要哪些依賴
官網(wǎng)地址:Installation — pytorch_geometric documentation (pytorch-geometric.readthedocs.io)
按照官網(wǎng)給出的代碼進(jìn)行安裝
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
運(yùn)行依舊沒有成功...
于是手動(dòng)下載
下載地址:https://data.pyg.org/whl/
另一個(gè)下載地址:pytorch-geometric.com/whl/torch-1.4.0.html
(1)卸載安裝的相關(guān)包
pip uninstall torch-geometric torch-scatter torch-sparse torch-cluster torch-spline-conv
(2)選擇適合自己的torch和cuda版本
?(3)按照自己的python版本和操作系統(tǒng)下載
(4)進(jìn)入conda激活虛擬環(huán)境,然后進(jìn)入下載地址
這里注意需要進(jìn)入D盤,cmd中進(jìn)入D盤的命令為:
D:
?然后cd進(jìn)入包的下載頁(yè)面,直接pip install
安裝完所有的依賴后記得?
pip install torch_geometric
?2、運(yùn)行代碼
import torch_geometric
?依舊報(bào)錯(cuò):
Pytorch AttributeError: module 'torch' has no attribute 'sparse_scs'
這個(gè)就很讓人費(fèi)解,于是查看了一下
?發(fā)現(xiàn)torch.sparse里面確實(shí)沒有這個(gè)模塊,那為什么會(huì)報(bào)錯(cuò)呢?查看一些帖子后發(fā)現(xiàn)報(bào)這種錯(cuò)一般是因?yàn)榘姹静粚?duì),于是,降低torch_geometric版本,果然成功??!
我的版本:
?至此就安裝成功啦?。?/p>
簡(jiǎn)單跑了個(gè)程序
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)
運(yùn)行結(jié)果:
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-515943.html
沒問題??!接下來(lái)就可以建立一個(gè)自己的圖神經(jīng)網(wǎng)絡(luò)啦~?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-515943.html
到了這里,關(guān)于torch_geometric踩坑實(shí)戰(zhàn)--安裝與運(yùn)行 親測(cè)有效?。〉奈恼戮徒榻B完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!