Skip to content

Instantly share code, notes, and snippets.

View BarclayII's full-sized avatar

Quan (Andy) Gan BarclayII

  • AWS Shanghai
  • Shanghai
View GitHub Profile
@BarclayII
BarclayII / rbtree.py
Last active September 30, 2022 06:20
RBTree
class Node(object):
def __init__(self, key, red):
self.key = key
self.left = self.right = self.parent = None
self.red = red
@property
def grandparent(self):
if self.parent is None:
@BarclayII
BarclayII / movielens.py
Last active December 9, 2023 05:47
PinSage example implementation
import pandas as pd
import dgl
import os
import torch
class MovieLens(object):
def __init__(self, directory):
'''
directory: path to movielens directory which should have the three
files:
@BarclayII
BarclayII / prof.py
Last active March 8, 2019 03:10
Batched GEMM profiling for transformers in PyTorch
# coding: utf-8
import torch
import time
import pandas as pd
import tqdm
B, L, N, H, W = 64, 50, 10, 256, 3
print('warming up')
for _ in tqdm.trange(10):
@BarclayII
BarclayII / ppr.py
Last active May 14, 2019 03:50
Compute significant PPR for all nodes with Reverse Push
import scipy.sparse as ssp
import numpy as np
from sklearn.preprocessing import normalize
from numba.typed import Dict
from numba import jit, types, njit, prange
@njit(parallel=True, nogil=True)
def reverse_push_bipartite(A_indptr, A_indices, A_data, nL, nR, r_max, alpha):
'''
@BarclayII
BarclayII / DNC.py
Last active December 1, 2020 18:00
Differentiable Neural Computer
#!/usr/bin/env python
# coding: utf-8
import torch
import torch.nn as nn
import torch.nn.functional as F
import tqdm
def reverse_permute(x):
return torch.zeros_like(x).scatter_(
@BarclayII
BarclayII / bpm.py
Last active May 14, 2021 08:27
O(n^3) minimum cost bipartite matching algorithm
# I found all over the place for a O(n^3) minimum cost bipartite matching
# or equivalently maximum weight bipartite matching algorithm but I
# cannot find a clean but precise explanation; the Wikipedia ones,
# Stack Overflow, and course materials are either too high-level
# or only O(n^4).
# So I decided to read the original paper of Edmunds and Karp. The
# idea is surprisingly simple: just always find a shortest path (that
# has minimum cost) to augment and you will be good. In fact they
# talked about minimum cost maximum flow problem, but minimum weight
# bipartite matching is just a special case of that.