0

参考本网站的代码 http://fa.bianp.net/blog/2011/locally-linear-embedding-and-sparse-eigensolvers/

from scipy.sparse import linalg, eye
from pyamg import smoothed_aggregation_solver
from sklearn import neighbors
import numpy as np

def locally_linear_embedding(X, n_neighbors, out_dim, tol=1e-6, max_iter=200):
    W = neighbors.kneighbors_graph(
        X, n_neighbors=n_neighbors, mode='barycenter', include_self=False)
    #"connectivity" or "distance"

    # M = (I-W)' (I-W)
    A = eye(*W.shape, format=W.format) - W
    A = (A.T).dot(A).tocsr()

    # initial approximation to the eigenvectors
    X = np.random.rand(W.shape[0], out_dim)
    ml = smoothed_aggregation_solver(A, symmetry='symmetric')
    prec = ml.aspreconditioner()

    # compute eigenvalues and eigenvectors with LOBPCG
    eigen_values, eigen_vectors = linalg.lobpcg(
        A, X, M=prec, largest=False, tol=tol, maxiter=max_iter)

    index = np.argsort(eigen_values)
    return eigen_vectors[:, index], np.sum(eigen_values)

我正在尝试根据这项研究中的代码https://github.com/liuyanfang023/KBS-RNE为我的任务实施稳健的邻域嵌入,但由于 LLE 的结果与原始的不同。

这是我应用的代码

def robust_neighbor_embedding(X, n_neighbors, out_dim, tol=1e-6, max_iter=200):

#recevied from LLE
W = neighbors.kneighbors_graph(
    X, n_neighbors=n_neighbors, mode='distance')
#"connectivity" or "distance"
#n,d = X.shape
X = np.random.rand(W.shape[0], out_dim)
n,d = X.shape
H = np.random.rand(d, n)
## W

# M = (I-W)' (I-W)
#A = eye(*W.shape, format=W.format) - W
#A = (A.T).dot(A).tocsr()
A = ((eye(n) - np.transpose(W)) * X)#.tocsr()
AA = np.dot(A.T,A) #.tocsr()
AAplus = 0.5 * (np.abs(AA) + AA)
AAsubtract = 0.5 * (np.abs(AA) - AA)

#initialize H, Y, alpha, gamma, gamma_max, mu
O = np.dot(A, H)
Y = np.zeros((n,2000))
mu = 1.1
gamma = 10
max_gamma = 10000000000.0
alpha = 1000.0

AM = np.dot(np.transpose(A), O)
AY =np.dot(np.transpose(A) , Y)
AMplus = 0.5 * (np.abs(AM) + AM)
AMsubtract = 0.5 * (np.abs(AM) - AM)
AYplus = 0.5 * (np.abs(AY) + AY)
AYsubtract = 0.5 * (np.abs(AY) - AY)
iter_num = 50
iter_numH = 30

G1 = np.diag(np.sqrt(1.0 / np.diag(np.dot(np.transpose(H), H)) + 1))
H = np.dot(H, G1)
H = np.multiply(H,np.sqrt((alpha * H) + (gamma * AMplus)+np.dot(gamma*AAsubtract, H) + AYplus/ np.dot((alpha * H ), np.dot(np.transpose(H), H))+gamma * AMsubtract + gamma * np.dot(AAplus,H)+ AYsubtract + 1))
H

# compute eigenvalues and eigenvectors 
eigen_values, eigen_vectors = np.linalg.eigh(np.dot(np.transpose(H), H))

index = np.argsort(eigen_values)

return eigen_vectors[:, index], np.sum(eigen_values)

我使用 LLE 的结果将重心更改为连接模式 在此处输入图像描述

我在 RNE 的结果(我不确定结果是什么) 在此处输入图像描述

但在我看来,如果 LLE 函数执行真实结果,我认为我的 RNE 结果会更正确

提前谢谢你T__T

4

0 回答 0