# Gather some statistics about the graph. print(f'Number of nodes: {data.num_nodes}') print(f'Number of edges: {data.num_edges}') print(f'Average node degree: {data.num_edges / data.num_nodes:.2f}') print(f'Number of training nodes: {data.train_mask.sum()}') print(f'Training node label rate: {int(data.train_mask.sum()) / data.num_nodes:.2f}') print(f'Has isolated nodes: {data.has_isolated_nodes()}') print(f'Has self-loops: {data.has_self_loops()}') print(f'Is undirected: {data.is_undirected()}')
结果显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Dataset: Cora(): ====================== Number of graphs: 1 Number of features: 1433 Number of classes: 7
Data(x=[2708, 1433], edge_index=[2, 10556], y=[2708], train_mask=[2708], val_mask=[2708], test_mask=[2708]) ======================================== Number of nodes: 2708 Number of edges: 10556 Average node degree: 3.90 Number of training nodes: 140 Training node label rate: 0.05 Has isolated nodes: False Has self-loops: False Is undirected: True Done!
可视化数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 可视化部分 %matplotlib inline import matplotlib.pyplot as plt from sklearn.manifold import TSNE
defvisualize(h, color): z = TSNE(n_components=2).fit_transform(h.detach().cpu().numpy())
deftrain(): model.train() optimizer.zero_grad() out = model(data.x, data.edge_index) loss = criterion(out[data.train_mask], data.y[data.train_mask]) loss.backward() optimizer.step() return loss