torch.nn.Embedding

自然语言中的常用的构建词向量方法,将id化后的语料库,映射到低维稠密的向量空间中,pytorch 中的使用如下:

import torch
import torch.utils.data as Data
import torch.nn as nn

import torch.nn.functional as F
from torch.autograd import Variable

word_to_id = {'hello':0, 'world':1}
embeds = nn.Embedding(2, 10)
hello_idx = torch.LongTensor([word_to_id['hello']])
# hello_idx = Variable(hello_idx)
hello_embed = embeds(hello_idx)
print(hello_embed)

if __name__ == '__main__':
    pass

输出:

需要注意的几点:

1)id化后的数据需要查表构建词向量时,idx必须是Long型的tensor

2)查表操作embeds即可得出嵌入向量

原文地址:https://www.cnblogs.com/demo-deng/p/10634561.html