主页 > 人工智能  > 

文本嵌入层

文本嵌入层
1、代码演示 embedding = nn.Embedding(10,3) print(embedding) input = torch.LongTensor([[1,2,3,4],[4,3,2,9]]) embedding(input)

2、构建Embeddings类来实现文本嵌入层 # 构建Embedding类来实现文本嵌入层 class Embeddings(nn.Module): def __init__(self,d_model,vocab): """ :param d_model: 词嵌入的维度 :param vocab: 词表的大小 """ super(Embeddings,self).__init__() self.lut = nn.Embedding(vocab,d_model) self.d_model = d_model def forward(self,x): """ :param x: 因为Embedding层是首层,所以代表输入给模型的文本通过词汇映射后的张量 :return: """ return self.lut(x) * math.sqrt(self.d_model) x = Variable(torch.LongTensor([[100,2,42,508],[491,998,1,221]])) emb = Embeddings(512,1000) embr = emb(x) print(embr.shape) # torch.Size([2, 4, 512]) print(embr) print(embr[0][0].shape) # torch.Size([512])

标签:

文本嵌入层由讯客互联人工智能栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“文本嵌入层