This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |