pytorch实现word embedding: torch.nn.Embedding
pytorch中实现词嵌入的模块是torch.nn.Embedding(m,n),其中m是单词总数,n是单词的特征属性数目。
例一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import torch from torch import nn embedding = nn.Embedding( 10 , 3 ) #总共有10个单词,每个单词表示为3个维度特征。此行程序将创建一个可查询的表, #表中包含一个10*3的张量,张量中的数值是随机初始化的,每一行代表一个单词。 #在神经网络的训练过程中,张量中的数值也将得到训练,每次迭代都将更新。 input = torch.LongTensor([ 5 ]) #假如我想取出第6个单词的词向量。 word_vector = embedding( input ) #这样就取出第6个单词的词向量了。input必须是Longtensor型的张量。 print ( '5th word embedding is:\n {}' . format (word_vector)) inputs = torch.LongTensor([ 0 , 2 , 9 ]) #假如我想取出第多个单词的词向量,比如第1、3、10个单词的词向量。 words_vector = embedding(inputs) #这样就取出多个单词的词向量了。 print ( "words' embedding are:\n {}" . format (words_vector)) |
值得注意的是,程序第9行的word_vector是一个张量,size为([1, 3]),1表示只取出一个单词的词向量;3表示词向量的长度是3,即有3个特征。同样的,程序中的words_vector的size为([3, 3])。如果input的size是([n]),则输出的size为([n, dim]),dim表示特征数目;如果input的size是([b,n]),则输出的size为([b,n, dim]),b表示batchsize。
例二
例一程序中embedding的词嵌入张量是随机初始化的,如果从已经训练好的词向量中获取,则采用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import torch from torch import nn import numpy as np #已训练好的词向量,大小为10*3 pretrained_weight = np.array([[ 1.33869969 , - 0.16198419 , 1.65357883 ], [ 1.55555752 , 2.25915442 , - 1.7898287 ], [ - 0.31996402 , 1.12052144 , 0.76315345 ], [ 0.03442668 , 0.03288871 , - 0.46975346 ], [ 0.36835297 , 1.47698013 , 0.26740319 ], [ 2.21786653 , - 0.64531753 , - 0.57987762 ], [ - 1.82413935 , 0.44493353 , - 1.54555689 ], [ 0.44076455 , 0.28416441 , 0.38695892 ], [ - 0.18101847 , 0.13821242 , 0.28120627 ], [ 0.99084866 , - 0.03077417 , 1.08613474 ]]) embedding = nn.Embedding( 10 , 3 ) #随机初始化词向量 embedding.weight.data.copy_(torch.from_numpy(pretrained_weight)) #把随机初始化的词向量替换为已训练好的词向量 print (embedding.weight.data) |
输出结果:
tensor([[ 1.3387 , - 0.1620 , 1.6536 ], [ 1.5556 , 2.2592 , - 1.7898 ], [ - 0.3200 , 1.1205 , 0.7632 ], [ 0.0344 , 0.0329 , - 0.4698 ], [ 0.3684 , 1.4770 , 0.2674 ], [ 2.2179 , - 0.6453 , - 0.5799 ], [ - 1.8241 , 0.4449 , - 1.5456 ], [ 0.4408 , 0.2842 , 0.3870 ], [ - 0.1810 , 0.1382 , 0.2812 ], [ 0.9908 , - 0.0308 , 1.0861 ]]) |
例三
把‘I earn the trust and respect of those around through my commitment every single day’这句话做词嵌入。
1 2 3 4 5 6 7 8 9 10 | import torch from torch import nn words = 'I earn the trust and respect of those around through my commitment every single day' .split() words = set (words) #去掉重复的单词 word_to_idx = {word: i for i, word in enumerate (words)} #把每个单词都编号,因为在embedding中取出词向量时只能用数字 embedding = nn.Embedding( len (words), 8 ) #每个词向量长度为8,即有8个特征 trust_idx = torch.LongTensor([word_to_idx[ 'trust' ]]) #假如我想取出单词trust的词向量 trust_vector = embedding(trust_idx) print (trust_vector) |
输出结果为:
tensor([[ 0.8197 , 0.1230 , 2.7905 , 1.0388 , - 0.5418 , - 1.4987 , 0.6195 , - 1.6211 ]], grad_fn = <EmbeddingBackward>) |
参考资料:
分类:
Deep Learning
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通