【问题标题】:Word embedding Visualization using TSNE not clear使用 TSNE 的词嵌入可视化不清楚
【发布时间】:2018-05-23 07:14:09
【问题描述】:

我从Word Embeddings by M. Baroni et al. 下载了预训练的词嵌入模型 我想可视化句子中单词的嵌入。我有两句话:

sentence1 = "Four people died in an accident."

sentence2 = "4 men are dead from a collision"

我有从上面的链接加载嵌入文件的功能:

def load_data(FileName = './EN-wform.w.5.cbow.neg10.400.subsmpl.txt'):

    embeddings = {}
    file = open(FileName,'r')
    i = 0
    print "Loading word embeddings first time"
    for line in file:
        # print line

        tokens = line.split('\t')

        #since each line's last token content '\n'
        # we need to remove that
        tokens[-1] = tokens[-1].strip()

        #each line has 400 tokens
        for i in xrange(1, len(tokens)):
            tokens[i] = float(tokens[i])

        embeddings[tokens[0]] = tokens[1:-1]
    print "finished"
    return embeddings

e = load_data()

从这两个句子中,我计算出单词的 lemmas忽略停用词和标点符号,所以现在我的句子变成:

sentence1 = ['Four', 'people', 'died', 'accident']
sentence2 = ['4', 'men', 'dead', 'collision']

现在,当我尝试使用 TSNE(t 分布随机邻域嵌入)可视化嵌入时,我首先为每个句子存储标签和标记:

#for sentence store labels and embeddings in list
# tokens contains vector of 400 dimensions for each label
labels1 = []
tokens1 = []
for i in sentence1:
    if i in e:
        labels1.append(i)
        tokens1.append(e[i])
    else:
        print i

labels2 = []
tokens2 = []
for i in sentence2:
    if i in e:
        labels2.append(i)
        tokens2.append(e[i])
    else:
        print i

对于 TSNE

tsne_model = TSNE(perplexity=40, n_components=2, init='random', n_iter=2000, random_state=23)
# fit transform for tokens of both sentences
new_values = tsne_model.fit_transform(tokens1)
new_values1 = tsne_model.fit_transform(tokens2)

#Plot values
x = []
y = []
x1 = []
y1 = []

for value in new_values:
    x.append(value[0])
    y.append(value[1])

for value in new_values1:
    x1.append(value[0])
    y1.append(value[1])


plt.figure(figsize=(10, 10)) 

for i in range(len(x)):
    plt.scatter(x[i],y[i])
    plt.annotate(labels[i],
                 xy=(x[i], y[i]),
                 xytext=(5, 2),
                 textcoords='offset points',
                 ha='right',
                 va='bottom')

for i in range(len(x1)):
    plt.scatter(x1[i],y1[i])
    plt.annotate(labels[i],
                 xy=(x1[i], y1[i]),
                 xytext=(5, 2),
                 textcoords='offset points',
                 ha='right',
                 va='bottom')

plt.show()

我的问题是,为什么“碰撞”和“事故”、“人”和“人”等同义词有不同的坐标?如果单词相同/同义词,它们不应该更接近吗?

距离 = euclidean_distances(tokens1) # 返回形状 (8,8)

【问题讨论】:

    标签: python nlp word2vec word-embedding


    【解决方案1】:

    来自TSNE-documentation

    t-SNE 有一个非凸的成本函数,即使用不同的初始化,我们可以得到不同的结果。

    这意味着在执行词嵌入的降维时不能保证获得相同的坐标。

    要解决这个问题,通过加入句子来执行一次 fit_transform 而不是两次:

    sentence1 = ['Four', 'people', 'died', 'accident']
    sentence2 = ['4', 'men', 'dead', 'collision']
    sentences = list(set(sentence1)| set(sentence2))
    

    编辑:您的代码中还有一个错误,您从错误的列表中绘制标签。

    【讨论】:

    • 我确实喜欢这个,但同义词彼此并不接近。你知道 Baroni el 的词嵌入吗?阿尔?还有其他方法可以解决句子的嵌入吗?
    • @Lucky 尝试在嵌入上使用 sk_learn 中的 euclidian_distances 函数,看看单词之间的距离是否有意义。
    • 你的意思是当我从 TSNE 得到坐标时,我应该计算每个术语之间的欧几里得距离,看看同义词是否有最小距离?
    • @Lucky 不,计算 TSNE 之前的欧几里得距离并打印距离矩阵。
    • 我这样做了,得到了 (8,8) 的数组。我将该距离矩阵用于model.fit,当我绘制时我没有看到任何区别。我已经在帖子中添加了该行。如果我错了,请纠正我。
    猜你喜欢
    • 2020-05-12
    • 2018-07-11
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    相关资源
    最近更新 更多