Skip to content

Instantly share code, notes, and snippets.

View qianyizhang's full-sized avatar

keyi qianyizhang

  • beijing
View GitHub Profile
@qianyizhang
qianyizhang / encoding.py
Created July 11, 2019 09:13
replacement of scaled_l2 and aggregate in PyTorch-Encoding/encoding/functions/encoding.py with pure torch ops
def scaled_l2(X, C, S):
"""
scaled_l2 distance
Args:
X (b*n*d): original feature input
C (k*d): code words, with k codes, each with d dimension
S (k): scale cofficient
Return:
D (b*n*k): relative distance to each code
Note:
@qianyizhang
qianyizhang / one_hot.py
Last active June 6, 2018 22:48
numpy one_hot function
import numpy as np
def one_hot(nparray, depth = 0, on_value = 1, off_value = 0):
if depth == 0:
depth = np.max(nparray) + 1
assert np.max(nparray) < depth, "the max index of nparray: {} is larger than depth: {}".format(np.max(nparray), depth)
shape = nparray.shape
out = np.ones((*shape, depth)) * off_value
indices = []
for i in range(nparray.ndim):
tiles = [1] * nparray.ndim