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即可得出嵌入向量
torch.nn.Embedding的更多相关文章
- torch.nn.Embedding理解
Pytorch官网的解释是:一个保存了固定字典和大小的简单查找表.这个模块常用来保存词嵌入和用下标检索它们.模块的输入是一个下标的列表,输出是对应的词嵌入. torch.nn.Embedding(nu ...
- PyTorch官方中文文档:torch.nn
torch.nn Parameters class torch.nn.Parameter() 艾伯特(http://www.aibbt.com/)国内第一家人工智能门户,微信公众号:aibbtcom ...
- pytorch nn.Embedding
pytorch nn.Embeddingclass torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_no ...
- Pytorch的默认初始化分布 nn.Embedding.weight初始化分布
一.nn.Embedding.weight初始化分布 nn.Embedding.weight随机初始化方式是标准正态分布 ,即均值$\mu=0$,方差$\sigma=1$的正态分布. 论据1——查看 ...
- pytorch中文文档-torch.nn.init常用函数-待添加
参考:https://pytorch.org/docs/stable/nn.html torch.nn.init.constant_(tensor, val) 使用参数val的值填满输入tensor ...
- pytorch中文文档-torch.nn常用函数-待添加-明天继续
https://pytorch.org/docs/stable/nn.html 1)卷积层 class torch.nn.Conv2d(in_channels, out_channels, kerne ...
- torch.nn.functional中softmax的作用及其参数说明
参考:https://pytorch-cn.readthedocs.io/zh/latest/package_references/functional/#_1 class torch.nn.Soft ...
- pytorch梯度裁剪(Clipping Gradient):torch.nn.utils.clip_grad_norm
torch.nn.utils.clip_grad_norm(parameters, max_norm, norm_type=2) 1.梯度裁剪原理(http://blog.csdn.net/qq_29 ...
- torch.nn.CrossEntropyLoss
class torch.nn.CrossEntropyLoss(weight=None, size_average=True, ignore_index=-100, reduce=True) 我这里没 ...
随机推荐
- Unitek的USB3.0 TF卡读卡器
淘宝买了个Unitek的usb3.0读卡器, 用来换掉之前用了很久sks的sub2读卡器, 收到之后在Ubuntu下先测了一下, 发现识别出来的是usb2.1 lsusb -D /dev/bus/us ...
- [转]你可能不知道的五个强大HTML5 API
一.全屏 // 找到适合浏览器的全屏方法 function launchFullScreen(element) { if(element.requestFullScreen) { element.re ...
- 【Python】 解析Python中的运算符
Python中的运算符相比较于传统的C/C++差别不是很大,主要是一些个别的运算符上的差别.包括:算术.比较.赋值.位.逻辑.成员.身份等.它们的优先级: 符号 说明 ** 指数(最高优先级) ~,+ ...
- TCP连接数过多问题
在一次生产上线后,发现使用的 8086 端口相关的 TCP 连接数竟然多大 6K+ ,有时候甚至会逼近 1w ,这个数量对于一个只是在内部使用的监控系统来说, 无论如何都是无法接受的, 于是开 ...
- PCB特征阻抗计算
见教程:链接:https://pan.baidu.com/s/1V4UbEoKfMD1bilwu-Qwdyg 密码:ml6t
- python线程池(threadpool)模块使用笔记 .python 线程池使用推荐
一.安装与简介 pip install threadpool pool = ThreadPool(poolsize) requests = makeRequests(some_callable, li ...
- Deepin 系统下安装VMware并激活
1.打开深度商店:搜索VMware,并下载安装. 2.打开启动器:点击VMware-install. 3.填写管理员密码. 4.下一步,完成安装. 5.打开VMware Workstation,输入密 ...
- error $GOPATH: no library found in $GOPATH: rdkafka
安装confluent-kafka-go出错时,请使用brew安装pkg-config,手动go get安装的pkg-config还不行... 错误提示: go get -u github.com/c ...
- [原创]Cadence Allegro小技巧之解决Out of date shapes问题
Allegro报错“Dynamic shapes are out of date; please update them. Check for out of date shapes in Setup ...
- 使用 pt-online-schema-change 实现在线DDL
问题背景 平时进行修改表的结构,更改字段,新增字段,更改字段名称一般都是通过ALTER TABLE 语法进行修改的.对于小表或者并发访问不是很大的情况是OK.但是如果是在线大表,那就很麻烦.由于表数 ...