Unverified Commit 5008af22 authored by Hongzhi (Steve), Chen's avatar Hongzhi (Steve), Chen Committed by GitHub
Browse files

[Misc] Rename number_of_edges and number_of_nodes to num_edges and num_nodes in examples. (#5492)



* pytorch_example

* fix

---------
Co-authored-by: default avatarUbuntu <ubuntu@ip-172-31-28-63.ap-northeast-1.compute.internal>
parent 3c8ac093
......@@ -22,11 +22,11 @@ val_idx = split_idx["valid"]["paper"]
test_idx = split_idx["test"]["paper"]
paper_labels = labels["paper"].squeeze()
train_mask = th.zeros((hg.number_of_nodes("paper"),), dtype=th.bool)
train_mask = th.zeros((hg.num_nodes("paper"),), dtype=th.bool)
train_mask[train_idx] = True
val_mask = th.zeros((hg.number_of_nodes("paper"),), dtype=th.bool)
val_mask = th.zeros((hg.num_nodes("paper"),), dtype=th.bool)
val_mask[val_idx] = True
test_mask = th.zeros((hg.number_of_nodes("paper"),), dtype=th.bool)
test_mask = th.zeros((hg.num_nodes("paper"),), dtype=th.bool)
test_mask[test_idx] = True
hg.nodes["paper"].data["train_mask"] = train_mask
hg.nodes["paper"].data["val_mask"] = val_mask
......
......@@ -41,11 +41,11 @@ def load_ogb(dataset):
if ntype == category:
category_id = i
train_mask = th.zeros((hg.number_of_nodes("paper"),), dtype=th.bool)
train_mask = th.zeros((hg.num_nodes("paper"),), dtype=th.bool)
train_mask[train_idx] = True
val_mask = th.zeros((hg.number_of_nodes("paper"),), dtype=th.bool)
val_mask = th.zeros((hg.num_nodes("paper"),), dtype=th.bool)
val_mask[val_idx] = True
test_mask = th.zeros((hg.number_of_nodes("paper"),), dtype=th.bool)
test_mask = th.zeros((hg.num_nodes("paper"),), dtype=th.bool)
test_mask[test_idx] = True
hg.nodes["paper"].data["train_mask"] = train_mask
hg.nodes["paper"].data["val_mask"] = val_mask
......@@ -104,7 +104,7 @@ if __name__ == "__main__":
print(
"load {} takes {:.3f} seconds".format(args.dataset, time.time() - start)
)
print("|V|={}, |E|={}".format(g.number_of_nodes(), g.number_of_edges()))
print("|V|={}, |E|={}".format(g.num_nodes(), g.num_edges()))
print(
"train: {}, valid: {}, test: {}".format(
th.sum(g.nodes["paper"].data["train_mask"]),
......
......@@ -74,11 +74,11 @@ for key in edge_map:
eid_map = dgl.distributed.id_map.IdMap(edge_map)
for ntype in node_map:
assert hg.number_of_nodes(ntype) == th.sum(
assert hg.num_nodes(ntype) == th.sum(
node_map[ntype][:, 1] - node_map[ntype][:, 0]
)
for etype in edge_map:
assert hg.number_of_edges(etype) == th.sum(
assert hg.num_edges(etype) == th.sum(
edge_map[etype][:, 1] - edge_map[etype][:, 0]
)
......@@ -209,9 +209,9 @@ for partid in range(num_parts):
for ntype in orig_node_ids:
nids = th.cat(orig_node_ids[ntype])
nids = th.sort(nids)[0]
assert np.all((nids == th.arange(hg.number_of_nodes(ntype))).numpy())
assert np.all((nids == th.arange(hg.num_nodes(ntype))).numpy())
for etype in orig_edge_ids:
eids = th.cat(orig_edge_ids[etype])
eids = th.sort(eids)[0]
assert np.all((eids == th.arange(hg.number_of_edges(etype))).numpy())
assert np.all((eids == th.arange(hg.num_edges(etype))).numpy())
......@@ -21,8 +21,8 @@ print(hg)
g = dgl.to_homogeneous(hg)
g.ndata["orig_id"] = g.ndata[dgl.NID]
g.edata["orig_id"] = g.edata[dgl.EID]
print("|V|=" + str(g.number_of_nodes()))
print("|E|=" + str(g.number_of_edges()))
print("|V|=" + str(g.num_nodes()))
print("|E|=" + str(g.num_edges()))
print("|NTYPE|=" + str(len(th.unique(g.ndata[dgl.NTYPE]))))
# Store the metadata of nodes.
......@@ -58,7 +58,7 @@ dst_id = dst_id[not_self_loop_idx]
orig_id = g.edata["orig_id"][not_self_loop_idx]
etype = g.edata[dgl.ETYPE][not_self_loop_idx]
# Remove duplicated edges.
ids = (src_id * g.number_of_nodes() + dst_id).numpy()
ids = (src_id * g.num_nodes() + dst_id).numpy()
uniq_ids, idx = np.unique(ids, return_index=True)
duplicate_idx = np.setdiff1d(np.arange(len(ids)), idx)
duplicate_src_id = src_id[duplicate_idx]
......@@ -85,7 +85,7 @@ np.savetxt(
)
print(
"There are {} edges, remove {} self-loops and {} duplicated edges".format(
g.number_of_edges(), len(self_loop_src_id), len(duplicate_src_id)
g.num_edges(), len(self_loop_src_id), len(duplicate_src_id)
)
)
......@@ -97,7 +97,7 @@ for etype in hg.etypes:
dgl.data.utils.save_tensors("edge_feat.dgl", edge_feats)
# Store the basic metadata of the graph.
graph_stats = [g.number_of_nodes(), len(src_id), num_node_weights]
graph_stats = [g.num_nodes(), len(src_id), num_node_weights]
with open("mag_stats.txt", "w") as filehandle:
filehandle.writelines(
"{} {} {}".format(graph_stats[0], graph_stats[1], graph_stats[2])
......
......@@ -60,7 +60,7 @@ def main(args):
test_mask = g.ndata["test_mask"]
in_feats = features.shape[1]
n_classes = data.num_labels
n_edges = g.number_of_edges()
n_edges = g.num_edges()
print(
"""----Data statistics------'
#Edges %d
......@@ -77,7 +77,7 @@ def main(args):
)
)
n_edges = g.number_of_edges()
n_edges = g.num_edges()
# add self loop
g = dgl.remove_self_loop(g)
g = dgl.add_self_loop(g)
......
......@@ -52,7 +52,7 @@ def main(args):
test_mask = g.ndata["test_mask"]
in_feats = features.shape[1]
n_classes = data.num_labels
n_edges = g.number_of_edges()
n_edges = g.num_edges()
print(
"""----Data statistics------'
#Edges %d
......@@ -70,7 +70,7 @@ def main(args):
)
# graph preprocess and calculate normalization factor
n_edges = g.number_of_edges()
n_edges = g.num_edges()
# normalization
degs = g.in_degrees().float()
norm = torch.pow(degs, -0.5)
......
......@@ -39,7 +39,7 @@ def main(args):
test_mask = g.ndata["test_mask"]
in_feats = features.shape[1]
n_classes = data.num_labels
n_edges = g.number_of_edges()
n_edges = g.num_edges()
print(
"""----Data statistics------'
#Edges %d
......@@ -60,7 +60,7 @@ def main(args):
# add self loop
if args.self_loop:
g = g.remove_self_loop().add_self_loop()
n_edges = g.number_of_edges()
n_edges = g.num_edges()
# create TAGCN model
model = TAGCN(
......
......@@ -108,7 +108,7 @@ def main(args):
model.train()
for step, batch in enumerate(train_loader):
g = batch.graph.to(device)
n = g.number_of_nodes()
n = g.num_nodes()
h = th.zeros((n, args.h_size)).to(device)
c = th.zeros((n, args.h_size)).to(device)
if step >= 3:
......@@ -129,9 +129,7 @@ def main(args):
pred = th.argmax(logits, 1)
acc = th.sum(th.eq(batch.label, pred))
root_ids = [
i
for i in range(g.number_of_nodes())
if g.out_degrees(i) == 0
i for i in range(g.num_nodes()) if g.out_degrees(i) == 0
]
root_acc = np.sum(
batch.label.cpu().data.numpy()[root_ids]
......@@ -160,7 +158,7 @@ def main(args):
model.eval()
for step, batch in enumerate(dev_loader):
g = batch.graph.to(device)
n = g.number_of_nodes()
n = g.num_nodes()
with th.no_grad():
h = th.zeros((n, args.h_size)).to(device)
c = th.zeros((n, args.h_size)).to(device)
......@@ -170,7 +168,7 @@ def main(args):
acc = th.sum(th.eq(batch.label, pred)).item()
accs.append([acc, len(batch.label)])
root_ids = [
i for i in range(g.number_of_nodes()) if g.out_degrees(i) == 0
i for i in range(g.num_nodes()) if g.out_degrees(i) == 0
]
root_acc = np.sum(
batch.label.cpu().data.numpy()[root_ids]
......@@ -212,7 +210,7 @@ def main(args):
model.eval()
for step, batch in enumerate(test_loader):
g = batch.graph.to(device)
n = g.number_of_nodes()
n = g.num_nodes()
with th.no_grad():
h = th.zeros((n, args.h_size)).to(device)
c = th.zeros((n, args.h_size)).to(device)
......@@ -221,9 +219,7 @@ def main(args):
pred = th.argmax(logits, 1)
acc = th.sum(th.eq(batch.label, pred)).item()
accs.append([acc, len(batch.label)])
root_ids = [
i for i in range(g.number_of_nodes()) if g.out_degrees(i) == 0
]
root_ids = [i for i in range(g.num_nodes()) if g.out_degrees(i) == 0]
root_acc = np.sum(
batch.label.cpu().data.numpy()[root_ids]
== pred.cpu().data.numpy()[root_ids]
......
......@@ -99,11 +99,11 @@ class SAGE(nn.Module):
# Therefore, we compute the representation of all nodes layer by layer. The nodes
# on each layer are of course splitted in batches.
# TODO: can we standardize this?
nodes = th.arange(g.number_of_nodes())
nodes = th.arange(g.num_nodes())
ys = []
for l, layer in enumerate(self.layers):
y = th.zeros(
g.number_of_nodes(),
g.num_nodes(),
self.n_hidden if l != len(self.layers) - 1 else self.n_classes,
)
......
......@@ -103,7 +103,7 @@ class SAGE(nn.Module):
# Therefore, we compute the representation of all nodes layer by layer. The nodes
# on each layer are of course splitted in batches.
# TODO: can we standardize this?
nodes = th.arange(g.number_of_nodes())
nodes = th.arange(g.num_nodes())
for l, layer in enumerate(self.layers):
y = g.ndata["hist_%d" % (l + 1)]
......@@ -217,7 +217,7 @@ def create_history_storage(g, args, n_classes):
for l in range(args.num_layers):
dim = args.num_hidden if l != args.num_layers - 1 else n_classes
g.ndata["hist_%d" % (l + 1)] = th.zeros(
g.number_of_nodes(), dim
g.num_nodes(), dim
).share_memory_()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment