当前位置: 首页 > news >正文

Pytorch Geometric(PyG)入门

PyG (PyTorch Geometric) 是建立在 PyTorch 基础上的一个库,用于轻松编写和训练图形神经网络 (GNN),适用于与结构化数据相关的各种应用。官方文档

Install PyG

PyG适用于python3.8-3.12
一般使用场景:pip install torch_geometricconda install pyg -c pyg

Get Started

PyG 具有以下主要功能:

  • Data Handling of Graphs
  • Common Benchmark Datasets
  • Mini-batches
  • Data Transforms
  • Learning Methods on Graphs
  • Exercises

Data Handling of Graphs

PyG 中的单个图由 torch_geometric.data.Data 的一个实例描述,默认情况下该实例拥有以下属性:

  • data.x: Node feature matrix with shape [num_nodes, num_node_features]
  • data.edge_index: Graph connectivity in COO format with shape [2, num_edges] and type torch.long
  • data.edge_attr: Edge feature matrix with shape [num_edges, num_edge_features]
  • data.y: Target to train against (may have arbitrary shape), e.g., node-level targets of shape [num_nodes, *] or graph-level targets of shape [1, *]
  • data.pos: Node position matrix with shape [num_nodes, num_dimensions]

Colab Notebooks and Video Tutorials

官方文档
Pytroch Geometric Tutorials

Tutorials 1

理解一个节点出发的计算图,理解多次计算图后可能节点信息就包含整个图数据信息了,反而没有用。
对应whl地址

安装torch版本对应的pyg,如下所示:

import os
import torch
os.environ['TORCH'] = torch.__version__
print(torch.__version__)!pip install -q torch-scatter -f https://data.pyg.org/whl/torch-${TORCH}.html
!pip install -q torch-sparse -f https://data.pyg.org/whl/torch-${TORCH}.html
!pip install -q git+https://github.com/pyg-team/pytorch_geometric.git

可视化网络的函数实现

# 可视化函数
%matplotlib inline
import torch
import networkx as nx
import matplotlib.pyplot as plt# visualization function for NX graph or Pytorch tensor
def visualize(h, color, epoch=None, loss=None):plt.figure(figsize=(7,7))plt.xticks([])plt.yticks([])if torch.is_tensor(h):# 可视化神经网络运行中间结果h = h.detach().cpu().numpy()plt.scatter(h[:, 0], h[:, 1], s=140, c=color, cmap="Set2")if epoch is not None and loss is not None:plt.xlabel(f'Epoch:{epoch}, Loss:{loss.item():.4f}', fontsize=16)else:nx.draw_networkx(G, pos=nx.spring_layout(G, seed=42), with_labels=False, node_color=color, cmap="Set2")plt.show()

例如:

from torch_geometric.utils import to_networkxG = to_networkx(data, to_undirected=True)
visualize(G, color=data.y)

如图所示:
在这里插入图片描述

参考:

PyTorch Geometric (PyG) 入门教程

http://www.lryc.cn/news/382337.html

相关文章:

  • 大模型KV Cache节省神器MLA学习笔记(包含推理时的矩阵吸收分析)
  • 项目中eventbus和rabbitmq配置后,不起作用
  • 文库小程序搭建部署:实现资源共享正向反馈
  • ONLYOFFICE 桌面编辑器8.1---一个高效且强大的办公软件
  • QThread 与QObject::moveToThread利用Qt事件循环在子线程执行多个函数
  • 6-2 归并排序
  • Java NIO(一) 概述
  • JUC线程池最佳实践
  • 2024最新版Node.js下载安装及环境配置教程(非常详细)
  • 计算机网络5:运输层
  • 昂科烧录器支持HangShun航顺芯片的32位微控制器HK32F030C8T6
  • 纯css星空动画
  • 使用Apache Flink实现实时数据同步与清洗:MySQL和Oracle到目标MySQL的ETL流程
  • postman教程-22-Newman结合Jenkins执行自动化测试
  • uniapp实现tabBar功能常见的方法
  • 智慧在线医疗在线诊疗APP患者端+医生端音视频诊疗并开处方
  • 攻防平台搭建与简易渗透工具箱编写
  • SQL EXISTS 关键字的使用与理解
  • 开源低代码平台,JeecgBoot v3.7.0 里程碑版本发布
  • 名侦探李先生第一话:谁是真正的凶手(只出现一次的数字相关题解(力扣)+位操作符回忆)
  • 【PA交易】BackTrader(一): 如何使用实时tick数据和蜡烛图
  • HTML(16)——边距问题
  • 【Godot4自学手册】第四十二节实现拖拽进行物品交换和数量叠加
  • 存储系统概述
  • Trilium windows上修改笔记目录,创建多个笔记空间方法
  • <Rust><iced>在iced中显示gif动态图片的一种方法
  • 【Unity设计模式】状态编程模式
  • 圆的面积并三角形面积并
  • Spring Data JPA介绍与CRUD实战演练
  • Python网络爬虫实战6—下一页,模拟用户点击,切换窗口