新建PyG,torch_geometric环境

1.新建conda环境

conda create -n PyG python=3.8

2.安装pytorch环境

官网:PyTorch

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

3.安装PyG,torch_geometric

官网:安装 — pytorch_geometric 文档 (pytorch-geometric.readthedocs.io)

pip install torch_geometric
或
pip install pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv -f https://data.pyg.org/whl/torch-2.4.0+cu121.html

4.安装xarray环境

官网:Installation (xarray.dev)

conda install -c conda-forge xarray dask netCDF4 bottleneck

5.安装Cartopy

详见:Installing Cartopy — cartopy 0.19.0rc2.dev8+gd251b2f documentation (scitools.org.uk)

conda install -c conda-forge cartopy

6.安装jupyter

官网:Project Jupyter | Installing Jupyter

pip install jupyter

7.tensorboard

pip install tensorboard

PyGTorch Geometric)是一个用于图神经网络(Graph Neural Networks, GNNs)的Python库,它是基于PyTorch构建的。如果你想使用PyG搭建GCN(Graph Convolutional Network)网络,首先需要安装`torch_geometric`库。以下是一个简单的步骤概述: 1. **安装**: ```bash pip install torch torchvision torchaudio -f https://download.pytorch.org/whl/torch_stable.html pip install torch-scatter -f https://github.com/rusty1s/pytorch_scatter/blobs/main/dist/ pip install torch-sparse -f https://github.com/rusty1s/pytorch_sparse/blobs/main/dist/ pip install torch-cluster -f https://github.com/rusty1s/pytorch_cluster/blobs/main/dist/ pip install torch-geometric ``` 2. **导入所需模块**: ```python import torch from torch_geometric.nn import GCNConv, global_mean_pool ``` 3. **构建数据结构**: 使用`torch_geometric.data.Data`类来创建图的数据结构,包括节点特征和边的信息。 4. **定义模型**: ```python class GCN(torch.nn.Module): def __init__(self, in_channels, hidden_channels, out_channels): super(GCN, self).__init__() self.conv1 = GCNConv(in_channels, hidden_channels) self.conv2 = GCNConv(hidden_channels, out_channels) def forward(self, data): x, edge_index = data.x, data.edge_index x = F.relu(self.conv1(x, edge_index)) x = self.conv2(x, edge_index) return global_mean_pool(x, data.batch) ``` 5. **训练和预测**: 定义损失函数、优化器,并通过`DataLoader`加载数据进行训练。 ```python model = GCN(...).to(device) optimizer = torch.optim.Adam(model.parameters(), lr=0.01) for epoch in range(100): # 假设这是训练循环 optimizer.zero_grad() output = model(data) loss = F.nll_loss(output[data.train_mask], data.y[data.train_mask]) loss.backward() optimizer.step() # 预测阶段 with torch.no_grad(): pred = model(data) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值