dimensionality-driven-learning
dimensionality-driven-learning copied to clipboard
Version
Hello, I want to reproduce your results. I set up everything with Python 3.6.6(and 3.6.10), TensorFlow 1.9.0, and Keras 2.2.0 ( which were the valid versions when the code was released) , but I'm still getting errors due to version mismatch. Would please tell me which versions I should set up?
Hi Hsabaei, I would recommend you rework the code in Pytorch instead of using TF and Keras implementation. They are pretty old. Hope I can release an updated version someday. Here is a pytroch version of how to compute the LID value (or have a look of this pytorch implementation https://github.com/pokaxpoka/deep_Mahalanobis_detector):
import torch
import numpy as np
from scipy.spatial.distance import cdist
# a pytorch version
def lid_mle(data, reference, k=20, get_idx=False):
b = data.shape[0]
k = min(k, b-2)
data = torch.flatten(data, start_dim=1)
reference = torch.flatten(reference, start_dim=1)
r = torch.cdist(data, reference, p=2)
a, idx = torch.sort(r, dim=1)
lids = -k / torch.sum(torch.log(a[:, 1:k] / a[:, k].view(-1, 1) + 1.e-4), dim=1)
if get_idx:
return idx, lids
return lids
# from the ICML2018 paper (a numpy version)
def mle_batch(data, batch, k):
data = np.asarray(data, dtype=np.float32)
batch = np.asarray(batch, dtype=np.float32)
k = min(k, len(data)-1)
def f(v): return - k / np.sum(np.log(v/v[-1]))
a = cdist(batch, data)
a = np.apply_along_axis(np.sort, axis=1, arr=a)[:, 1:k+1]
a = np.apply_along_axis(f, axis=1, arr=a)
return a
# lid of a single query point x
def mle_single(data, x, k=20, dist=True, metric='euclidean'):
data = np.asarray(data, dtype=np.float32)
x = np.asarray(x, dtype=np.float32)
if x.ndim == 1:
x = x.reshape((-1, x.shape[0]))
if dist:
k = min(k, len(data)-1)
def f(v): return - k / np.sum(np.log(v/v[-1]))
if dist:
a = cdist(x, data, metric=metric)
a = np.apply_along_axis(np.sort, axis=1, arr=a)[:, 1:k+1]
else:
a = data
a = np.apply_along_axis(np.sort, axis=1, arr=a)
a = np.apply_along_axis(f, axis=1, arr=a)
return a[0]