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即可得出嵌入向量


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM