torch_geometric 笔记:TORCH_GEOMETRIC.UTILS(更新中)
生活随笔
收集整理的這篇文章主要介紹了
torch_geometric 笔记:TORCH_GEOMETRIC.UTILS(更新中)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1?torch_geometric.utils.add_self_loops
add_self_loops(edge_index, edge_weight: Optional[torch.Tensor] = None, fill_value: float = 1.0, num_nodes: Optional[int] = None)在edge_index中,對圖上每個節點i,添加一條邊(i,i)
參數介紹
| edge_index?(LongTensor)? | 原圖的edge_index |
| edge_weight?(Tensor,?optional) | 原圖那些邊的weight 【一維數組,維度需要和原來的邊數量一致】 【此時自環的權重默認為1】 |
| fill_value?(float,?optional) | 如果edge_weight非空,將用fill_value作為自環的weight |
| num_nodes?(int,?optional)? | 最初的多少個點進行自環(沒有這個參數的話,就是默認所有的點) |
?返回內容
????????(LongTensor,?Tensor) 第一個維度是更新后的edge_idx,第二個維度是邊權重,如果沒有設置edge_weight,那么第二個維度是None
2?torch_geometric.utils.remove_self_loops
去除自環
remove_self_loops(edge_index, edge_attr: Optional[torch.Tensor] = None)3 torch_geometric.utils.degree
degree(index, num_nodes: Optional[int] = None, dtype: Optional[int] = None)計算一個給定的一維index tensor的度
num_nodes也是表示計算多少個點的度
x,y=edge_index x,y #(tensor([0, 1, 2, 0, 3]), tensor([1, 0, 1, 3, 2]))torch_geometric.utils.degree(x) #tensor([2., 1., 1., 1.]) #0~3這四個點的出度torch_geometric.utils.degree(y) #tensor([1., 2., 1., 1.]) #0~3這四個點的入度4?torch_geometric.utils.get_laplacian
通過edge_index和可能有的edge_weight,計算圖拉普拉斯矩陣
get_laplacian(edge_index, edge_weight: Optional[torch.Tensor] = None, normalization: Optional[str] = None, dtype: Optional[int] = None, num_nodes: Optional[int] = None)?參數說明?
| edge_index?(LongTensor)? | 原圖的edge_index | ||||||
| edge_weight?(Tensor,?optional)? | 邊權重 | ||||||
| normalization? | 圖拉普拉斯矩陣的歸一化方法:默認是sym
| ||||||
| dtype?(torch.dtype,?optional)? | |||||||
| num_nodes?(int,?optional)? | 表示計算多少個點的拉普拉斯矩陣 |
5?to_networkx
to_networkx(data, node_attrs=None, edge_attrs=None, to_undirected=False, remove_self_loops=False)參數說明
| data?(torch_geometric.data.Data)? | 需要轉換的Data數據 |
| node_attrs?(iterable of str,?optional)? | 需要轉換的點屬性 |
| edge_attrs?(iterable of str,?optional)? | 需要轉化的邊屬性 |
| to_undirected?(bool,?optional)? | 如果是True,那么返回的就是networkx.Graph 如果是False,那么返回的就是networkx.DiGraph 無向圖會根據相應鄰接矩陣的上三角矩陣進行創建 |
| remove_self_loops?(bool,?optional)? | 是否移除自環 |
我們以ENZYMES數據集的第一個data為例:
torch_geometric筆記:數據集 ENZYMES &Minibatches_UQI-LIUWJ的博客-CSDN博客
import networkx as nx import matplotlib.pyplot as plt from torch_geometric.datasets import TUDataset from torch_geometric.utils import to_networkxdataset = TUDataset(root='', name='ENZYMES')dataset[0] #Data(edge_index=[2, 168], x=[37, 3], y=[1])x=to_networkx(dataset[0]) nx.draw(x, with_labels=True)?
總結
以上是生活随笔為你收集整理的torch_geometric 笔记:TORCH_GEOMETRIC.UTILS(更新中)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: toch_geometric 笔记:me
- 下一篇: torch_geometric 笔记:n