词嵌入代码入门级理解

import torchtext
# 隐藏警告消息
torchtext.disable_torchtext_deprecation_warning()
from torchtext.vocab import GloVe
import torch
from torch import nn
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
#glove算法:将单词转为向量
glove = GloVe(name="6B", dim=300)
# 使用nn.Embedding创建词嵌入
# 此时的Embedding层嵌入了Glove词向量数据
embedding = nn.Embedding.from_pretrained(glove.vectors)

print(embedding.weight.shape)
# torch.Size([400000, 300]) 4w单词,每个单词30个维度表示

words = ["man", "woman", "king", "queen", "cat", "dog", "mother", "father"]
indices = []
for word in words:
    # 将单词通过GloVe词汇表转换为索引
    index = glove.stoi[word]
    indices.append(index)
    print(word, "-->", index)

# 将索引转换为张量
indices = torch.tensor(indices)
vectors = embedding(indices).detach().numpy()
print("vectors.shape", vectors.shape)  # (8, 300) 8个单词,300个维度


# 对vector降维,从300降至2维
pca = PCA(n_components=2)
vectors_2d = pca.fit_transform(vectors)

# 将单词和向量绘制在二维空间中
plt.scatter(vectors_2d[:, 0], vectors_2d[:, 1])
for i, word in enumerate(words):
    plt.annotate(
        word,
        xy=(vectors_2d[i, 0], vectors_2d[i, 1]),
        xytext=(5, 2),
        textcoords="offset points",
    )

plt.show()

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值