我如何使用 t-SNE 进行降维以可视化我的 300 维词嵌入?

How can i use t-SNE for dimension reduction to visualise my 300 dimension word embeddings?

我目前正在尝试在 2d 中可视化 300 维的词向量。 我尝试了使用不同参数的 t-SNE 并阅读了 https://distill.pub/2016/misread-tsne/ 上的博客,但到目前为止我没有得到任何有用的结果。

我想要一个对应于几个选定词向量的最近邻的可视化,但是二维可视化到处都是。

我的问题不适合用TSNE吗?

from sklearn.manifold import TSNE

arr = []

for category in category_embeddings.keys():
    arr.append(category_embeddings[category][0]) 

perplex = 30
tsne_steps = 50000
lr = 10

fig_tsne = plt.figure(figsize=(18, 18), dpi=800)

tsne = TSNE(perplexity=perplex, 
            n_components=2, 
            init='pca', 
            n_iter=tsne_steps, 
            learning_rate=lr, 
            method="exact")

plot_only = len(category_embeddings.keys())
low_dim_embs = tsne.fit_transform(np.asarray(arr))

for i, title in enumerate(category_embeddings.keys()):
    x, y = low_dim_embs[i, :]
    plt.scatter(x, y)
    plt.annotate(
        title,
        xy=(x, y),
        xytext=(5, 2),
        textcoords='offset points',
        ha='right',
        va='bottom')

好的,解决了。

创建一个距离矩阵并用该矩阵输入 TSNE 会产生更好的二维可视化效果。

from sklearn.metrics.pairwise import cosine_distances

c1_c2_cos_dist = {}

# Create distance Matrix
for c1in category_embeddings.keys():
    tmp = {}
    for c2 in category_embeddings.keys():
        cos_dis = cosine_distances(category_embeddings[c1],category_embeddings[
        tmp[c2] = cos_dis[0][0]

    c1_c2_cos_dist[c1] = copy(tmp)

# --- 

from sklearn.manifold import TSNE

arr = []

for category in category_embeddings.keys():
    arr.append(category_embeddings[category][0]) 

perplex = 30
tsne_steps = 50000
lr = 10

fig_tsne = plt.figure(figsize=(18, 18), dpi=800)

tsne = TSNE(perplexity=perplex, 
            n_components=2, 
            metric="precomputed",
            n_iter=tsne_steps, 
            learning_rate=lr)

distMatrix = []
for col in c1_c2_cos_dist.keys():
    arr =[]
    for row in c1_c2_cos_dist[col]:
        arr.append(c1_c2_cos_dist[col][row])
    distMatrix.append(copy(arr))  

distMatrix = np.asarray(distMatrix)
low_dim_embs = tsne.fit_transform(distMatrix)

plot_only = len(category_embeddings.keys())

for i, title in enumerate(category_embeddings.keys()):
    x, y = low_dim_embs[i, :]
    plt.scatter(x, y)
    plt.annotate(
        title,
        xy=(x, y),
        xytext=(5, 2),
        textcoords='offset points',
        ha='right',
        va='bottom')