安装geometric(PyG)库——图神经网络库

第一步:查看已安装的pytorch版本和cuda版本

1.1、进入Anaconda Prompt激活环境

d75924a1228d4c308e34c2b9cc02a377.png

1.2、输入conda list查看此环境下已安装的第三方库版本列表

fb587f7bed334e3996773141a9da30e6.png

bfd6eb4bc4f34c29b5f1c88028309018.png

pytorch版本为2.0.1、cuda版本为11.7、python版本为3.8

 

第二步:进入网址寻找轮子文件进行下载

网址:https://pytorch-geometric.com/whl/

2.1、ctrl+f查找:cu117

找到对应pytorch和cuda版本的链接点进去,cu117就代表cuda版本是11.7,而我pytorch版本是2.0.1

b729ec4ab8044832a8523177ebca7f3b.png

2.2、进入后找到对应版本的四个轮子文件:

torch_scatter、torch_sparse、torch_cluster、torch_spline_conv

注意:cp38代表python3.8版本,win代表windows系统

ccc071c6e54740fcb8452b16dc846bba.png

2.3、四个轮子文件各下载一个,前面的小版本无所谓,点击链接即可下载

2.4、将下载好的安装包剪切到C盘用户文件夹(你电脑现在运行的账户)下面

只有把下载好的轮子文件安装包放在这个路径,才能在anaconda里面直接使用pip安装这四个轮子文件

e24b002170244033b2c4ba6729a46a51.png

 

第三步:安装已经下载的轮子文件安装包

3.1、依次输入四句命令:

pip install torch_scatter-2.1.2+pt20cu117-cp38-cp38-win_amd64.whl
pip install torch_sparse-0.6.18+pt20cu117-cp38-cp38-win_amd64.whl
pip install torch_cluster-1.6.3+pt20cu117-cp38-cp38-win_amd64.whl
pip install torch_spline_conv-1.2.2+pt20cu117-cp38-cp38-win_amd64.whl

完成轮子文件的安装

 

第四步:直接pip安装geometric

4.1、输入命令:


pip install torch-geometric

或者加上清华大学镜像源:
pip install torch-geometric -i https://pypi.tuna.tsinghua.edu.cn/simple

 

第五步:安装成功-运行试试

ad397b804ec445ea949a387e15eb564e.png

c7382f73baa946359aacc15023143da2.png

 

 

 

### 图神经网络及其Python框架实现 对于图神经网络(GNN),多个Python提供了强大的支持,使得开发人员可以方便地创建和训练这些模型。以下是几个流行的图神经网络: #### PyTorch Geometric (PyG) `PyTorch Geometric` 是一个基于 `PyTorch` 的扩展,专门设计用于处理不规则数据结构的数据集,比如点云和各种类型的图表。该提供了一系列工具来简化图神经网络的构建过程。 ```python from torch_geometric.nn import GCNConv, GATConv class GraphNeuralNetwork(torch.nn.Module): def __init__(self): super(GraphNeuralNetwork, self).__init__() self.conv1 = GCNConv(dataset.num_features, 16) self.conv2 = GCNConv(16, dataset.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) return F.log_softmax(x, dim=1) ``` 此代码片段展示了如何定义一个简单的两层GCN(图卷积网络)。它使用了来自 `torch_geometric.nn` 模块中的 `GCNConv` 类[^1]。 #### Deep Graph Library (DGL) 另一个广泛使用的是 DGL (`Deep Graph Library`) ,这是一个专为高效计算大规模稀疏矩阵乘法而优化的开源项目。除了基本操作外,还实现了许多先进的算法和技术,如注意力机制等。 ```python import dgl.function as fn from dgl.nn.pytorch import GraphConv class SAGEModel(nn.Module): def __init__(self, in_feats, hiddens, n_layers, activation, dropout): super(SAGEModel, self).__init__() self.layers = nn.ModuleList() # input layer self.layers.append(dgl.nn.GraphConv(in_feats, hiddens)) ... def forward(self, g, features): h = features for conv in self.layers[:-1]: h = conv(g, h) h = F.relu(h) if self.dropout: h = F.dropout(h, p=self.dropout, training=self.training) h = self.layers[-1](g, h) return h ``` 上述例子中展示的是一个采用SAGE架构的多层感知器,在每一层都进行了ReLU激活函数的应用以及可能存在的dropout正则化措施[^2]。 #### StellarGraph 最后提到的一个重要资源就是StellarGraph——这是一款专注于机器学习任务上的大型社交网络分析平台。其特色在于易于集成其他流行的人工智能组件,并且拥有良好的文档说明和支持社区。 ```python from stellargraph.layer import GCN from tensorflow.keras.optimizers import Adam from tensorflow.keras.losses import categorical_crossentropy generator = FullBatchNodeGenerator(graph, method="gcn") train_gen = generator.flow(train_subjects.index, train_targets) gcn = GCN( layer_sizes=[32, 32], activations=['elu', 'elu'], generator=generator, dropout=0.5 ) x_inp, x_out = gcn.in_out_tensors() predictions = Dense(units=len(set(targets)), activation='softmax')(x_out) model = Model(inputs=x_inp, outputs=predictions) model.compile(optimizer=Adam(lr=0.01), loss=categorical_crossentropy, metrics=["acc"]) history = model.fit_generator( train_gen, epochs=num_epochs, validation_data=val_gen, verbose=2, shuffle=False ) ``` 这段代码演示了怎样利用 `stellargraph` 创建一个多分类问题下的GCN模型实例,并完成相应的参数配置与训练流程设置[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值