def knn_batch(data, k): batch = [] _, ind = knn(data, k) for i in range(data.shape[0]): batch.append(data[ind[i], :]) return batch
def knn_batch(data, k): batch = [] _, _, ind = knn(data, k) for i in range(data.shape[0]): batch.append(data[ind[i], :]) return batch
import numpy as np import matplotlib.pyplot as plt from tools.geometric import knn data = np.loadtxt("swiss.dat") data = data+abs(np.min(data)) n,dim = data.shape dimh = 5 id = 2 k = 8 iters = 100000 l_r = 0.001 t = 1.0 dist,neighbors = knn.knn(data,k) s = np.exp(-dist**2)/t x = tf.placeholder("float",[n,dim]) nei = tf.placeholder("float",[n,k,dim]) s_w = tf.placeholder("float",[n,k]) W1 = tf.Variable(tf.truncated_normal([dim,dimh])) b1 = tf.Variable(tf.truncated_normal([dimh])) Wh = tf.Variable(tf.truncated_normal([dimh,id])) bh = tf.Variable(tf.truncated_normal([id])) W2 = tf.Variable(tf.truncated_normal([id,dim])) b2 = tf.Variable(tf.truncated_normal([dim]))
import matplotlib.pyplot as plt from sklearn.preprocessing import normalize from tools.geometric import knn iters = 100000 l_r = 0.005 dimh = 5 id = 2 k = 8 pro = 27 data = np.loadtxt("swiss.dat") #data = normalize(data,axis=1, norm='l2') n,dim = data.shape _,neighbors = knn.knn(data,k) x = tf.placeholder("float",[n,dim]) nei = tf.placeholder("float",[n,dim,k]) pW = tf.Variable(tf.truncated_normal([n,k,dim,pro]),"float") p_r = (tf.batch_matmul(nei,tf.reshape(pW,[n,k,dim*pro]))) pI = tf.reshape(p_r,[n,dim,dim,pro]) x_com = tf.batch_matmul(tf.expand_dims(x,1),tf.reshape(pI,[n,dim,dim*pro])) x_com = tf.reshape(x_com,[n,dim,pro])
import numpy as np from tools.geometric import knn data = np.random.rand(400, 3) k = 8 n = data.shape[0] [_, index] = knn.knn(data, k) traverse = [] start = np.random.randint(n) temp = index[start, 0] for i in range(n): c = 1 neigh_end = False neighbors = np.random.permutation(k) while temp in traverse: temp = index[start, neighbors[c]] if c == k - 1: neigh_end = True break c = c + 1 if neigh_end:
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt from tools.geometric import knn data = np.loadtxt("swiss.dat") n,dim = data.shape dimh = 5 reg_residual = 0.0125 id = 2 k = 5 iters = 50000 l_r = 0.125 dist,vicini = knn.knn(data,k) x = tf.placeholder("float",[n,dim]) nei = tf.placeholder("float",[n,dim,k]) neigh_space = tf.batch_matmul(nei,tf.transpose(nei,[0,2,1])) tan_space = tf.Variable(tf.truncated_normal([n,dim,id]),"float") proj = (tf.batch_matmul(neigh_space,tan_space)) x_pro = tf.squeeze(tf.batch_matmul(tf.expand_dims(x,1),proj)) W1 = tf.Variable(tf.truncated_normal([id,dimh])) b1 = tf.Variable(tf.truncated_normal([dimh]))