学习GNN的时候尝试在线安装torch_geometric ,自己安装过程中出现了挺多问题,找到官网后按照其指示最后成功在线安装(官网链接:torch-geometric · PyPI?)
以下是我的安装流程:
先激活环境:
conda activate py37
由于我安装的是:PyTorch 1.10.0,故需要执行以下的操作:
pip install torch-scatter -f https://data.pyg.org/whl/torch-1.10.0+cpu.html
pip install torch-sparse -f https://data.pyg.org/whl/torch-1.10.0+cpu.html
pip install torch-geometric
按照官网上的说明,以下为选择性安装部分(torch-cluster和torch-spline-conv):
pip install torch-cluster -f https://data.pyg.org/whl/torch-1.10.0+cpu.html
pip install torch-spline-conv -f https://data.pyg.org/whl/torch-1.10.0+cpu.html
?
?
最后我们测试一下是否成功安装:
测试代码如下:(直接利用官网上提供的测试代码)
import torch
from torch import Tensor
from torch.nn import Sequential, Linear, ReLU
from torch_geometric.nn import MessagePassing
class EdgeConv(MessagePassing):
def __init__(self, in_channels, out_channels):
super().__init__(aggr="max") # "Max" aggregation.
self.mlp = Sequential(
Linear(2 * in_channels, out_channels),
ReLU(),
Linear(out_channels, out_channels),
)
def forward(self, x: Tensor, edge_index: Tensor) -> Tensor:
# x: Node feature matrix of shape [num_nodes, in_channels]
# edge_index: Graph connectivity matrix of shape [2, num_edges]
return self.propagate(edge_index, x=x) # shape [num_nodes, out_channels]
def message(self, x_j: Tensor, x_i: Tensor) -> Tensor:
# x_j: Source node features of shape [num_edges, in_channels]
# x_i: Target node features of shape [num_edges, in_channels]
edge_features = torch.cat([x_i, x_j - x_i], dim=-1)
return self.mlp(edge_features) # shape [num_edges, out_channels]
运行结果如下:
Bingo!?
|