"docs/source/vscode:/vscode.git/clone" did not exist on "edb978772caea6e18b719d7dd813c597d4e84306"
Unverified Commit 44089c8b authored by Minjie Wang's avatar Minjie Wang Committed by GitHub
Browse files

[Refactor][Graph] Merge DGLGraph and DGLHeteroGraph (#1862)



* Merge

* [Graph][CUDA] Graph on GPU and many refactoring (#1791)

* change edge_ids behavior and C++ impl

* fix unittests; remove utils.Index in edge_id

* pass mx and th tests

* pass tf test

* add aten::Scatter_

* Add nonzero; impl CSRGetDataAndIndices/CSRSliceMatrix

* CSRGetData and CSRGetDataAndIndices passed tests

* CSRSliceMatrix basic tests

* fix bug in empty slice

* CUDA CSRHasDuplicate

* has_node; has_edge_between

* predecessors, successors

* deprecate send/recv; fix send_and_recv

* deprecate send/recv; fix send_and_recv

* in_edges; out_edges; all_edges; apply_edges

* in deg/out deg

* subgraph/edge_subgraph

* adj

* in_subgraph/out_subgraph

* sample neighbors

* set/get_n/e_repr

* wip: working on refactoring all idtypes

* pass ndata/edata tests on gpu

* fix

* stash

* workaround nonzero issue

* stash

* nx conversion

* test_hetero_basics except update routines

* test_update_routines

* test_hetero_basics for pytorch

* more fixes

* WIP: flatten graph

* wip: flatten

* test_flatten

* test_to_device

* fix bug in to_homo

* fix bug in CSRSliceMatrix

* pass subgraph test

* fix send_and_recv

* fix filter

* test_heterograph

* passed all pytorch tests

* fix mx unittest

* fix pytorch test_nn

* fix all unittests for PyTorch

* passed all mxnet tests

* lint

* fix tf nn test

* pass all tf tests

* lint

* lint

* change deprecation

* try fix compile

* lint

* update METIDS

* fix utest

* fix

* fix utests

* try debug

* revert

* small fix

* fix utests

* upd

* upd

* upd

* fix

* upd

* upd

* upd

* upd

* upd

* trigger

* +1s

* [kernel] Use heterograph index instead of unitgraph index (#1813)

* upd

* upd

* upd

* fix

* upd

* upd

* upd

* upd

* upd

* trigger

* +1s

* [Graph] Mutation for Heterograph (#1818)

* mutation add_nodes and add_edges

* Add support for remove_edges, remove_nodes, add_selfloop, remove_selfloop

* Fix
Co-authored-by: default avatarUbuntu <ubuntu@ip-172-31-51-214.ec2.internal>

* upd

* upd

* upd

* fix

* [Transfom] Mutable transform (#1833)

* add nodesy

* All three

* Fix

* lint

* Add some test case

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix

* fix

* triger

* Fix

* fix
Co-authored-by: default avatarUbuntu <ubuntu@ip-172-31-51-214.ec2.internal>

* [Graph] Migrate Batch & Readout module to heterograph (#1836)

* dgl.batch

* unbatch

* fix to device

* reduce readout; segment reduce

* change batch_num_nodes|edges to function

* reduce readout/ softmax

* broadcast

* topk

* fix

* fix tf and mx

* fix some ci

* fix batch but unbatch differently

* new checkk

* upd

* upd

* upd

* idtype behavior; code reorg

* idtype behavior; code reorg

* wip: test_basics

* pass test_basics

* WIP: from nx/ to nx

* missing files

* upd

* pass test_basics:test_nx_conversion

* Fix test

* Fix inplace update

* WIP: fixing tests

* upd

* pass test_transform cpu

* pass gpu test_transform

* pass test_batched_graph

* GPU graph auto cast to int32

* missing file

* stash

* WIP: rgcn-hetero

* Fix two datasety

* upd

* weird

* Fix capsuley

* fuck you

* fuck matthias

* Fix dgmg

* fix bug in block degrees; pass rgcn-hetero

* rgcn

* gat and diffpool fix
also fix ppi and tu dataset

* Tree LSTM

* pointcloud

* rrn; wip: sgc

* resolve conflicts

* upd

* sgc and reddit dataset

* upd

* Fix deepwalk, gindt and gcn

* fix datasets and sign

* optimization

* optimization

* upd

* upd

* Fix GIN

* fix bug in add_nodes add_edges; tagcn

* adaptive sampling and gcmc

* upd

* upd

* fix geometric

* fix

* metapath2vec

* fix agnn

* fix pickling problem of block

* fix utests

* miss file

* linegraph

* upd

* upd

* upd

* graphsage

* stgcn_wave

* fix hgt

* on unittests

* Fix transformer

* Fix HAN

* passed pytorch unittests

* lint

* fix

* Fix cluster gcn

* cluster-gcn is ready

* on fixing block related codes

* 2nd order derivative

* Revert "2nd order derivative"

This reverts commit 523bf6c249bee61b51b1ad1babf42aad4167f206.

* passed torch utests again

* fix all mxnet unittests

* delete some useless tests

* pass all tf cpu tests

* disable

* disable distributed unittest

* fix

* fix

* lint

* fix

* fix

* fix script

* fix tutorial

* fix apply edges bug

* fix 2 basics

* fix tutorial
Co-authored-by: default avataryzh119 <expye@outlook.com>
Co-authored-by: default avatarxiang song(charlie.song) <classicxsong@gmail.com>
Co-authored-by: default avatarUbuntu <ubuntu@ip-172-31-51-214.ec2.internal>
Co-authored-by: default avatarUbuntu <ubuntu@ip-172-31-7-42.us-west-2.compute.internal>
Co-authored-by: default avatarUbuntu <ubuntu@ip-172-31-1-5.us-west-2.compute.internal>
Co-authored-by: default avatarUbuntu <ubuntu@ip-172-31-68-185.ec2.internal>
parent 015acfd2
...@@ -74,11 +74,11 @@ def construct_graph(): ...@@ -74,11 +74,11 @@ def construct_graph():
f_1.close() f_1.close()
f_2.close() f_2.close()
pa = dgl.bipartite((paper_author_src, paper_author_dst), 'paper', 'pa', 'author') hg = dgl.heterograph({
ap = dgl.bipartite((paper_author_dst, paper_author_src), 'author', 'ap', 'paper') ('paper', 'pa', 'author') : (paper_author_src, paper_author_dst),
pc = dgl.bipartite((paper_conf_src, paper_conf_dst), 'paper', 'pc', 'conf') ('author', 'ap', 'paper') : (paper_author_dst, paper_author_src),
cp = dgl.bipartite((paper_conf_dst, paper_conf_src), 'conf', 'cp', 'paper') ('paper', 'pc', 'conf') : (paper_conf_src, paper_conf_dst),
hg = dgl.hetero_from_relations([pa, ap, pc, cp]) ('conf', 'cp', 'paper') : (paper_conf_dst, paper_conf_src)})
return hg, author_names, conf_names, paper_names return hg, author_names, conf_names, paper_names
#"conference - paper - Author - paper - conference" metapath sampling #"conference - paper - Author - paper - conference" metapath sampling
......
...@@ -24,7 +24,7 @@ citation network datasets. ...@@ -24,7 +24,7 @@ citation network datasets.
## Usage ## Usage
``` ```
python run.py [--gpu] --model MODEL_NAME --dataset DATASET_NAME [--self-loop] python run.py [--gpu GPU] --model MODEL_NAME --dataset DATASET_NAME [--self-loop]
``` ```
The hyperparameters might not be the optimal, you could specify them manually in `conf.py`. The hyperparameters might not be the optimal, you could specify them manually in `conf.py`.
...@@ -302,19 +302,19 @@ class ChebNet(nn.Module): ...@@ -302,19 +302,19 @@ class ChebNet(nn.Module):
self.g = g self.g = g
self.layers = nn.ModuleList() self.layers = nn.ModuleList()
self.layers.append( self.layers.append(
ChebConv(in_feats, n_hidden, k, bias) ChebConv(in_feats, n_hidden, k, bias=bias)
) )
for _ in range(n_layers - 1): for _ in range(n_layers - 1):
self.layers.append( self.layers.append(
ChebConv(n_hidden, n_hidden, k, bias) ChebConv(n_hidden, n_hidden, k, bias=bias)
) )
self.layers.append( self.layers.append(
ChebConv(n_hidden, n_classes, k, bias) ChebConv(n_hidden, n_classes, k, bias=bias)
) )
def forward(self, features): def forward(self, features):
h = features h = features
for layer in self.layers: for layer in self.layers:
h = layer(self.g, h, [2]) h = layer(self.g, h, [2])
return h return h
\ No newline at end of file
...@@ -3,7 +3,7 @@ import numpy as np ...@@ -3,7 +3,7 @@ import numpy as np
import torch import torch
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
from dgl import DGLGraph import dgl
from dgl.data import register_data_args, load_data from dgl.data import register_data_args, load_data
from models import * from models import *
from conf import * from conf import *
...@@ -44,16 +44,17 @@ def evaluate(model, features, labels, mask): ...@@ -44,16 +44,17 @@ def evaluate(model, features, labels, mask):
def main(args): def main(args):
# load and preprocess dataset # load and preprocess dataset
data = load_data(args) data = load_data(args)
features = torch.FloatTensor(data.features) g = data[0]
labels = torch.LongTensor(data.labels) if args.gpu < 0:
if hasattr(torch, 'BoolTensor'): cuda = False
train_mask = torch.BoolTensor(data.train_mask)
val_mask = torch.BoolTensor(data.val_mask)
test_mask = torch.BoolTensor(data.test_mask)
else: else:
train_mask = torch.ByteTensor(data.train_mask) cuda = True
val_mask = torch.ByteTensor(data.val_mask) g = g.to(args.gpu)
test_mask = torch.ByteTensor(data.test_mask) features = g.ndata['feat']
labels = g.ndata['label']
train_mask = g.ndata['train_mask']
val_mask = g.ndata['val_mask']
test_mask = g.ndata['test_mask']
in_feats = features.shape[1] in_feats = features.shape[1]
n_classes = data.num_labels n_classes = data.num_labels
n_edges = data.graph.number_of_edges() n_edges = data.graph.number_of_edges()
...@@ -68,31 +69,16 @@ def main(args): ...@@ -68,31 +69,16 @@ def main(args):
val_mask.int().sum().item(), val_mask.int().sum().item(),
test_mask.int().sum().item())) test_mask.int().sum().item()))
if args.gpu < 0:
cuda = False
else:
cuda = True
torch.cuda.set_device(args.gpu)
features = features.cuda()
labels = labels.cuda()
train_mask = train_mask.cuda()
val_mask = val_mask.cuda()
test_mask = test_mask.cuda()
# graph preprocess and calculate normalization factor # graph preprocess and calculate normalization factor
g = data.graph
# add self loop # add self loop
if args.self_loop: if args.self_loop:
g.remove_edges_from(nx.selfloop_edges(g)) g = g.remove_self_loop().add_self_loop()
g.add_edges_from(zip(g.nodes(), g.nodes()))
g = DGLGraph(g)
n_edges = g.number_of_edges() n_edges = g.number_of_edges()
# normalization # normalization
degs = g.in_degrees().float() degs = g.in_degrees().float()
norm = torch.pow(degs, -0.5) norm = torch.pow(degs, -0.5)
norm[torch.isinf(norm)] = 0 norm[torch.isinf(norm)] = 0
if cuda:
norm = norm.cuda()
g.ndata['norm'] = norm.unsqueeze(1) g.ndata['norm'] = norm.unsqueeze(1)
# create GCN model # create GCN model
...@@ -103,7 +89,7 @@ def main(args): ...@@ -103,7 +89,7 @@ def main(args):
*config['extra_args']) *config['extra_args'])
if cuda: if cuda:
model.cuda() model = model.cuda()
print(model) print(model)
......
...@@ -8,7 +8,6 @@ import torch.nn.functional as F ...@@ -8,7 +8,6 @@ import torch.nn.functional as F
import dgl import dgl
from torch.utils.data import DataLoader from torch.utils.data import DataLoader
from torchvision import datasets, transforms from torchvision import datasets, transforms
from dgl import DGLGraph
from dgl.data import register_data_args, load_data from dgl.data import register_data_args, load_data
from dgl.nn.pytorch.conv import ChebConv, GMMConv from dgl.nn.pytorch.conv import ChebConv, GMMConv
from dgl.nn.pytorch.glob import MaxPooling from dgl.nn.pytorch.glob import MaxPooling
...@@ -33,7 +32,7 @@ A = grid_graph(28, 8, metric) ...@@ -33,7 +32,7 @@ A = grid_graph(28, 8, metric)
coarsening_levels = 4 coarsening_levels = 4
L, perm = coarsen(A, coarsening_levels) L, perm = coarsen(A, coarsening_levels)
g_arr = [DGLGraph(csr) for csr in L] g_arr = [dgl.graph(csr) for csr in L]
coordinate_arr = get_coordinates(g_arr, grid_side, coarsening_levels, perm) coordinate_arr = get_coordinates(g_arr, grid_side, coarsening_levels, perm)
for g, coordinate_arr in zip(g_arr, coordinate_arr): for g, coordinate_arr in zip(g_arr, coordinate_arr):
...@@ -45,7 +44,7 @@ def batcher(batch): ...@@ -45,7 +44,7 @@ def batcher(batch):
x_batch = [] x_batch = []
y_batch = [] y_batch = []
for x, y in batch: for x, y in batch:
x = torch.cat([x.view(-1), x.new_zeros(928 - 28 ** 2)], 0) x = torch.cat([x.view(-1), x.new_zeros(len(perm) - 28 ** 2)], 0)
x = x[perm] x = x[perm]
x_batch.append(x) x_batch.append(x)
y_batch.append(y) y_batch.append(y)
...@@ -97,9 +96,11 @@ class MoNet(nn.Module): ...@@ -97,9 +96,11 @@ class MoNet(nn.Module):
def forward(self, g_arr, feat): def forward(self, g_arr, feat):
for g, layer in zip(g_arr, self.layers): for g, layer in zip(g_arr, self.layers):
u = g.edata['u'].to(feat.device) u = g.edata['u']
feat = self.pool(layer(g, feat, u).transpose(-1, -2).unsqueeze(0))\ feat = self.pool(layer(g, feat, u).transpose(-1, -2).unsqueeze(0))\
.squeeze(0).transpose(-1, -2) .squeeze(0).transpose(-1, -2)
print(feat.shape)
print(g_arr[-1].batch_size)
return self.cls(self.readout(g_arr[-1], feat)) return self.cls(self.readout(g_arr[-1], feat))
class ChebNet(nn.Module): class ChebNet(nn.Module):
...@@ -155,6 +156,7 @@ for epoch in range(10): ...@@ -155,6 +156,7 @@ for epoch in range(10):
for i, (g, x, y) in enumerate(train_loader): for i, (g, x, y) in enumerate(train_loader):
x = x.to(device) x = x.to(device)
y = y.to(device) y = y.to(device)
g = [g_i.to(device) for g_i in g]
out = model(g, x) out = model(g, x)
hit += (out.max(-1)[1] == y).sum().item() hit += (out.max(-1)[1] == y).sum().item()
tot += len(y) tot += len(y)
......
...@@ -65,16 +65,17 @@ def evaluate(model, features, pseudo, labels, mask): ...@@ -65,16 +65,17 @@ def evaluate(model, features, pseudo, labels, mask):
def main(args): def main(args):
# load and preprocess dataset # load and preprocess dataset
data = load_data(args) data = load_data(args)
features = torch.FloatTensor(data.features) g = data[0]
labels = torch.LongTensor(data.labels) if args.gpu < 0:
if False: #hasattr(torch, 'BoolTensor'): cuda = False
train_mask = torch.BoolTensor(data.train_mask)
val_mask = torch.BoolTensor(data.val_mask)
test_mask = torch.BoolTensor(data.test_mask)
else: else:
train_mask = torch.ByteTensor(data.train_mask) cuda = True
val_mask = torch.ByteTensor(data.val_mask) g = g.to(args.gpu)
test_mask = torch.ByteTensor(data.test_mask) features = g.ndata['feat']
labels = g.ndata['label']
train_mask = g.ndata['train_mask']
val_mask = g.ndata['val_mask']
test_mask = g.ndata['test_mask']
in_feats = features.shape[1] in_feats = features.shape[1]
n_classes = data.num_labels n_classes = data.num_labels
n_edges = data.graph.number_of_edges() n_edges = data.graph.number_of_edges()
...@@ -89,33 +90,12 @@ def main(args): ...@@ -89,33 +90,12 @@ def main(args):
val_mask.sum().item(), val_mask.sum().item(),
test_mask.sum().item())) test_mask.sum().item()))
if args.gpu < 0:
cuda = False
else:
cuda = True
torch.cuda.set_device(args.gpu)
features = features.cuda()
labels = labels.cuda()
train_mask = train_mask.cuda()
val_mask = val_mask.cuda()
test_mask = test_mask.cuda()
print("use cuda:", args.gpu)
# graph preprocess and calculate normalization factor # graph preprocess and calculate normalization factor
g = data.graph g = g.remove_self_loop().add_self_loop()
g.remove_edges_from(nx.selfloop_edges(g))
g = DGLGraph(g)
n_edges = g.number_of_edges() n_edges = g.number_of_edges()
us, vs = g.edges() us, vs = g.edges(order='eid')
pseudo = [] udeg, vdeg = 1 / torch.sqrt(g.in_degrees(us).float()), 1 / torch.sqrt(g.in_degrees(vs).float())
for i in range(g.number_of_edges()): pseudo = torch.cat([udeg.unsqueeze(1), vdeg.unsqueeze(1)], dim=1)
pseudo.append([
1 / np.sqrt(g.in_degree(us[i])),
1 / np.sqrt(g.in_degree(vs[i]))
])
pseudo = torch.Tensor(pseudo)
if cuda:
pseudo = pseudo.cuda()
# create GraphSAGE model # create GraphSAGE model
model = MoNet(g, model = MoNet(g,
......
...@@ -51,10 +51,6 @@ class GAT(nn.Module): ...@@ -51,10 +51,6 @@ class GAT(nn.Module):
activation, activation,
dropout): dropout):
super().__init__() super().__init__()
print(in_feats)
print(n_hidden)
print(n_classes)
print(n_layers)
self.n_layers = n_layers self.n_layers = n_layers
self.n_hidden = n_hidden self.n_hidden = n_hidden
self.n_classes = n_classes self.n_classes = n_classes
...@@ -112,7 +108,7 @@ class GAT(nn.Module): ...@@ -112,7 +108,7 @@ class GAT(nn.Module):
num_workers=args.num_workers) num_workers=args.num_workers)
for input_nodes, output_nodes, blocks in tqdm.tqdm(dataloader): for input_nodes, output_nodes, blocks in tqdm.tqdm(dataloader):
block = blocks[0] block = blocks[0].to(device)
h = x[input_nodes].to(device) h = x[input_nodes].to(device)
h_dst = h[:block.number_of_dst_nodes()] h_dst = h[:block.number_of_dst_nodes()]
...@@ -205,6 +201,9 @@ def run(args, device, data): ...@@ -205,6 +201,9 @@ def run(args, device, data):
for step, (input_nodes, seeds, blocks) in enumerate(dataloader): for step, (input_nodes, seeds, blocks) in enumerate(dataloader):
tic_step = time.time() tic_step = time.time()
# copy block to gpu
blocks = [blk.to(device) for blk in blocks]
# Load the input features as well as output labels # Load the input features as well as output labels
batch_inputs, batch_labels = load_subtensor(g, labels, seeds, input_nodes, device) batch_inputs, batch_labels = load_subtensor(g, labels, seeds, input_nodes, device)
...@@ -272,16 +271,9 @@ if __name__ == '__main__': ...@@ -272,16 +271,9 @@ if __name__ == '__main__':
graph, labels = data[0] graph, labels = data[0]
labels = labels[:, 0] labels = labels[:, 0]
graph.readonly(False)
_, _, self_e = graph.edge_ids(th.arange(graph.number_of_nodes()), th.arange(graph.number_of_nodes()), return_uv=True)
print('Total edges before adding self-loop {}'.format(graph.number_of_edges())) print('Total edges before adding self-loop {}'.format(graph.number_of_edges()))
# clean partial self-loop edges graph = graph.remove_self_loop().add_self_loop()
graph.remove_edges(self_e)
# Add all self-loop edges
graph.add_edges(th.arange(graph.number_of_nodes()), th.arange(graph.number_of_nodes()))
print('Total edges after adding self-loop {}'.format(graph.number_of_edges())) print('Total edges after adding self-loop {}'.format(graph.number_of_edges()))
graph.readonly(True)
graph = dgl.as_heterograph(graph)
in_feats = graph.ndata['feat'].shape[1] in_feats = graph.ndata['feat'].shape[1]
n_classes = (labels.max() + 1).item() n_classes = (labels.max() + 1).item()
......
...@@ -81,7 +81,7 @@ class SAGE(nn.Module): ...@@ -81,7 +81,7 @@ class SAGE(nn.Module):
num_workers=args.num_workers) num_workers=args.num_workers)
for input_nodes, output_nodes, blocks in tqdm.tqdm(dataloader): for input_nodes, output_nodes, blocks in tqdm.tqdm(dataloader):
block = blocks[0] block = blocks[0].to(device)
h = x[input_nodes].to(device) h = x[input_nodes].to(device)
h_dst = h[:block.number_of_dst_nodes()] h_dst = h[:block.number_of_dst_nodes()]
...@@ -174,6 +174,9 @@ def run(args, device, data): ...@@ -174,6 +174,9 @@ def run(args, device, data):
for step, (input_nodes, seeds, blocks) in enumerate(dataloader): for step, (input_nodes, seeds, blocks) in enumerate(dataloader):
tic_step = time.time() tic_step = time.time()
# copy block to gpu
blocks = [blk.to(device) for blk in blocks]
# Load the input features as well as output labels # Load the input features as well as output labels
batch_inputs, batch_labels = load_subtensor(g, labels, seeds, input_nodes, device) batch_inputs, batch_labels = load_subtensor(g, labels, seeds, input_nodes, device)
...@@ -240,8 +243,6 @@ if __name__ == '__main__': ...@@ -240,8 +243,6 @@ if __name__ == '__main__':
graph, labels = data[0] graph, labels = data[0]
labels = labels[:, 0] labels = labels[:, 0]
graph = dgl.as_heterograph(graph)
in_feats = graph.ndata['feat'].shape[1] in_feats = graph.ndata['feat'].shape[1]
n_classes = (labels.max() + 1).item() n_classes = (labels.max() + 1).item()
prepare_mp(graph) prepare_mp(graph)
......
...@@ -54,7 +54,10 @@ class NeighborSampler(object): ...@@ -54,7 +54,10 @@ class NeighborSampler(object):
if len(eids) > 0: if len(eids) > 0:
old_frontier = frontier old_frontier = frontier
frontier = dgl.remove_edges(old_frontier, eids) frontier = dgl.remove_edges(old_frontier, eids)
frontier.edata['weights'] = old_frontier.edata['weights'][frontier.edata[dgl.EID]] #print(old_frontier)
#print(frontier)
#print(frontier.edata['weights'])
#frontier.edata['weights'] = old_frontier.edata['weights'][frontier.edata[dgl.EID]]
block = compact_and_copy(frontier, seeds) block = compact_and_copy(frontier, seeds)
seeds = block.srcdata[dgl.NID] seeds = block.srcdata[dgl.NID]
blocks.insert(0, block) blocks.insert(0, block)
......
...@@ -41,7 +41,7 @@ class Model(nn.Module): ...@@ -41,7 +41,7 @@ class Model(nn.Module):
h = x h = x
for i in range(self.num_layers): for i in range(self.num_layers):
g = self.nng(h) g = self.nng(h).to(h.device)
h = h.view(batch_size * n_points, -1) h = h.view(batch_size * n_points, -1)
h = self.conv[i](g, h) h = self.conv[i](g, h)
h = F.leaky_relu(h, 0.2) h = F.leaky_relu(h, 0.2)
......
...@@ -18,7 +18,7 @@ class ShapeNet(object): ...@@ -18,7 +18,7 @@ class ShapeNet(object):
if not os.path.exists(data_path): if not os.path.exists(data_path):
local_path = os.path.join(download_path, data_filename) local_path = os.path.join(download_path, data_filename)
if not os.path.exists(local_path): if not os.path.exists(local_path):
download(SHAPENET_DOWNLOAD_URL, local_path) download(SHAPENET_DOWNLOAD_URL, local_path, verify_ssl=False)
with ZipFile(local_path) as z: with ZipFile(local_path) as z:
z.extractall(path=download_path) z.extractall(path=download_path)
......
...@@ -89,7 +89,7 @@ class FixedRadiusNNGraph(nn.Module): ...@@ -89,7 +89,7 @@ class FixedRadiusNNGraph(nn.Module):
src_idx = inv_idx[:src.shape[0]] src_idx = inv_idx[:src.shape[0]]
dst_idx = inv_idx[src.shape[0]:] dst_idx = inv_idx[src.shape[0]:]
g = dgl.DGLGraph((src_idx.cpu(), dst_idx.cpu()), readonly=True) g = dgl.graph((src_idx, dst_idx))
g.ndata['pos'] = pos[i][uniq] g.ndata['pos'] = pos[i][uniq]
g.ndata['center'] = center[uniq] g.ndata['center'] = center[uniq]
if feat is not None: if feat is not None:
......
...@@ -37,7 +37,8 @@ download_path = os.path.join(get_download_dir(), data_filename) ...@@ -37,7 +37,8 @@ download_path = os.path.join(get_download_dir(), data_filename)
local_path = args.dataset_path or os.path.join(get_download_dir(), 'modelnet40_normal_resampled') local_path = args.dataset_path or os.path.join(get_download_dir(), 'modelnet40_normal_resampled')
if not os.path.exists(local_path): if not os.path.exists(local_path):
download('https://shapenet.cs.stanford.edu/media/modelnet40_normal_resampled.zip', download_path) download('https://shapenet.cs.stanford.edu/media/modelnet40_normal_resampled.zip',
download_path, verify_ssl=False)
from zipfile import ZipFile from zipfile import ZipFile
with ZipFile(download_path) as z: with ZipFile(download_path) as z:
z.extractall(path=get_download_dir()) z.extractall(path=get_download_dir())
......
...@@ -47,6 +47,7 @@ def main(args): ...@@ -47,6 +47,7 @@ def main(args):
use_cuda = args.gpu >= 0 and th.cuda.is_available() use_cuda = args.gpu >= 0 and th.cuda.is_available()
if use_cuda: if use_cuda:
th.cuda.set_device(args.gpu) th.cuda.set_device(args.gpu)
g = g.to('cuda:%d' % args.gpu)
labels = labels.cuda() labels = labels.cuda()
train_idx = train_idx.cuda() train_idx = train_idx.cuda()
test_idx = test_idx.cuda() test_idx = test_idx.cuda()
......
...@@ -23,18 +23,17 @@ def extract_embed(node_embed, input_nodes): ...@@ -23,18 +23,17 @@ def extract_embed(node_embed, input_nodes):
emb[ntype] = node_embed[ntype][nid] emb[ntype] = node_embed[ntype][nid]
return emb return emb
def evaluate(model, loader, node_embed, labels, category, use_cuda): def evaluate(model, loader, node_embed, labels, category, device):
model.eval() model.eval()
total_loss = 0 total_loss = 0
total_acc = 0 total_acc = 0
count = 0 count = 0
for input_nodes, seeds, blocks in loader: for input_nodes, seeds, blocks in loader:
blocks = [blk.to(device) for blk in blocks]
seeds = seeds[category] seeds = seeds[category]
emb = extract_embed(node_embed, input_nodes) emb = extract_embed(node_embed, input_nodes)
lbl = labels[seeds] emb = {k : e.to(device) for k, e in emb.items()}
if use_cuda: lbl = labels[seeds].to(device)
emb = {k : e.cuda() for k, e in emb.items()}
lbl = lbl.cuda()
logits = model(emb, blocks)[category] logits = model(emb, blocks)[category]
loss = F.cross_entropy(logits, lbl) loss = F.cross_entropy(logits, lbl)
acc = th.sum(logits.argmax(dim=1) == lbl).item() acc = th.sum(logits.argmax(dim=1) == lbl).item()
...@@ -71,9 +70,11 @@ def main(args): ...@@ -71,9 +70,11 @@ def main(args):
val_idx = train_idx val_idx = train_idx
# check cuda # check cuda
device = 'cpu'
use_cuda = args.gpu >= 0 and th.cuda.is_available() use_cuda = args.gpu >= 0 and th.cuda.is_available()
if use_cuda: if use_cuda:
th.cuda.set_device(args.gpu) th.cuda.set_device(args.gpu)
device = 'cuda:%d' % args.gpu
train_label = labels[train_idx] train_label = labels[train_idx]
val_label = labels[val_idx] val_label = labels[val_idx]
...@@ -127,6 +128,7 @@ def main(args): ...@@ -127,6 +128,7 @@ def main(args):
t0 = time.time() t0 = time.time()
for i, (input_nodes, seeds, blocks) in enumerate(loader): for i, (input_nodes, seeds, blocks) in enumerate(loader):
blocks = [blk.to(device) for blk in blocks]
seeds = seeds[category] # we only predict the nodes with type "category" seeds = seeds[category] # we only predict the nodes with type "category"
batch_tic = time.time() batch_tic = time.time()
emb = extract_embed(node_embed, input_nodes) emb = extract_embed(node_embed, input_nodes)
...@@ -146,7 +148,7 @@ def main(args): ...@@ -146,7 +148,7 @@ def main(args):
if epoch > 3: if epoch > 3:
dur.append(time.time() - t0) dur.append(time.time() - t0)
val_loss, val_acc = evaluate(model, val_loader, node_embed, labels, category, use_cuda) val_loss, val_acc = evaluate(model, val_loader, node_embed, labels, category, device)
print("Epoch {:05d} | Valid Acc: {:.4f} | Valid loss: {:.4f} | Time: {:.4f}". print("Epoch {:05d} | Valid Acc: {:.4f} | Valid loss: {:.4f} | Time: {:.4f}".
format(epoch, val_acc, val_loss, np.average(dur))) format(epoch, val_acc, val_loss, np.average(dur)))
print() print()
......
...@@ -237,7 +237,7 @@ class EntityClassify(nn.Module): ...@@ -237,7 +237,7 @@ class EntityClassify(nn.Module):
num_workers=num_workers) num_workers=num_workers)
for input_nodes, output_nodes, blocks in tqdm.tqdm(dataloader): for input_nodes, output_nodes, blocks in tqdm.tqdm(dataloader):
block = blocks[0] block = blocks[0].to(device)
h = {k: x[k][input_nodes[k]].to(device) for k in input_nodes.keys()} h = {k: x[k][input_nodes[k]].to(device) for k in input_nodes.keys()}
h = layer(block, h) h = layer(block, h)
......
...@@ -33,6 +33,7 @@ def main(args): ...@@ -33,6 +33,7 @@ def main(args):
th.cuda.set_device(args.gpu) th.cuda.set_device(args.gpu)
labels = labels.cuda() labels = labels.cuda()
test_idx = test_idx.cuda() test_idx = test_idx.cuda()
g = g.to('cuda:%d' % args.gpu)
# create model # create model
model = EntityClassify(g, model = EntityClassify(g,
......
...@@ -48,10 +48,6 @@ MUTAG: accuracy avg(5 runs) 67.06%, best 80.88% (DGL) ...@@ -48,10 +48,6 @@ MUTAG: accuracy avg(5 runs) 67.06%, best 80.88% (DGL)
python3 entity_classify_mp.py -d mutag --l2norm 5e-4 --n-bases 30 --testing --gpu 0 --batch-size 256 --use-self-loop --n-epochs 40 --dropout=0.3 python3 entity_classify_mp.py -d mutag --l2norm 5e-4 --n-bases 30 --testing --gpu 0 --batch-size 256 --use-self-loop --n-epochs 40 --dropout=0.3
``` ```
```
python3 entity_classify_mp.py -d bgs --l2norm 5e-4 --n-bases 40 --testing --gpu 0 --fanout 40 --n-epochs=40 --batch-size=128
```
BGS: accuracy avg(5 runs) 84.14%, best 89.66% (DGL) BGS: accuracy avg(5 runs) 84.14%, best 89.66% (DGL)
``` ```
......
...@@ -94,6 +94,7 @@ def main(args): ...@@ -94,6 +94,7 @@ def main(args):
if use_cuda: if use_cuda:
model.cuda() model.cuda()
g = g.to('cuda:%d' % args.gpu)
# optimizer # optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.l2norm) optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.l2norm)
......
...@@ -161,6 +161,7 @@ def main(args): ...@@ -161,6 +161,7 @@ def main(args):
node_id, deg = node_id.cuda(), deg.cuda() node_id, deg = node_id.cuda(), deg.cuda()
edge_type, edge_norm = edge_type.cuda(), edge_norm.cuda() edge_type, edge_norm = edge_type.cuda(), edge_norm.cuda()
data, labels = data.cuda(), labels.cuda() data, labels = data.cuda(), labels.cuda()
g = g.to(args.gpu)
t0 = time.time() t0 = time.time()
embed = model(g, node_id, edge_type, edge_norm) embed = model(g, node_id, edge_type, edge_norm)
......
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