自然语言中的常用的构建词向量方法,将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的更多相关文章

  1. torch.nn.Embedding理解

    Pytorch官网的解释是:一个保存了固定字典和大小的简单查找表.这个模块常用来保存词嵌入和用下标检索它们.模块的输入是一个下标的列表,输出是对应的词嵌入. torch.nn.Embedding(nu ...

  2. PyTorch官方中文文档:torch.nn

    torch.nn Parameters class torch.nn.Parameter() 艾伯特(http://www.aibbt.com/)国内第一家人工智能门户,微信公众号:aibbtcom ...

  3. pytorch nn.Embedding

    pytorch nn.Embeddingclass torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_no ...

  4. Pytorch的默认初始化分布 nn.Embedding.weight初始化分布

    一.nn.Embedding.weight初始化分布 nn.Embedding.weight随机初始化方式是标准正态分布  ,即均值$\mu=0$,方差$\sigma=1$的正态分布. 论据1——查看 ...

  5. pytorch中文文档-torch.nn.init常用函数-待添加

    参考:https://pytorch.org/docs/stable/nn.html torch.nn.init.constant_(tensor, val) 使用参数val的值填满输入tensor ...

  6. pytorch中文文档-torch.nn常用函数-待添加-明天继续

    https://pytorch.org/docs/stable/nn.html 1)卷积层 class torch.nn.Conv2d(in_channels, out_channels, kerne ...

  7. torch.nn.functional中softmax的作用及其参数说明

    参考:https://pytorch-cn.readthedocs.io/zh/latest/package_references/functional/#_1 class torch.nn.Soft ...

  8. 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 ...

  9. torch.nn.CrossEntropyLoss

    class torch.nn.CrossEntropyLoss(weight=None, size_average=True, ignore_index=-100, reduce=True) 我这里没 ...

随机推荐

  1. html5学习笔记——基础

    一:Canvas <canvas> 标签只是图形容器,图形的绘制需要用JS来定义. 1:绘制与填充 stroke():绘制,空心. fillXX():填充,实心. 2:绘制线条 var c ...

  2. (原)tensorflow使用eager在mnist上训练的简单例子

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9989586.html 代码网址: https://github.com/darkknightzh/t ...

  3. 词向量可视化--[tensorflow , python]

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ ---------------------------------- ...

  4. 关于TF(词频) 和TF-IDF(词频-逆向文件频率 )的理解

    ##TF-IDF TF(词频):  假定存在一份有N个词的文件A,其中‘明星‘这个词出现的次数为T.那么 TF = T/N; 所以表示为: 某一个词在某一个文件中出现的频率. TF-IDF(词频-逆向 ...

  5. PHP微信开发之模板消息回复

    参考:http://www.jb51.net/article/87269.htm 代码: <?php //$ac = file_get_contents('https://api.weixin. ...

  6. mysql 线程等待时间,解决sleep进程过多的办法

    如果你没有修改过MySQL的配置,缺省情况下,wait_timeout的初始值是28800.   wait_timeout 过大有弊端,其体现就是MySQL里大量的SLEEP进程无法及时释放,拖累系统 ...

  7. linux驱动(续)

    网络通信 --> IO多路复用之select.poll.epoll详解 IO多路复用之select.poll.epoll详解      目前支持I/O多路复用的系统调用有 select,psel ...

  8. Fedora 25-64位操作系统中安装配置Hyperledger Fabric过程

    安装过程参照Hyperledger Fabric的官方文档,文档地址:http://hyperledger-fabric.readthedocs.io/en/latest/prereqs.html 0 ...

  9. blender split mesh

    https://www.youtube.com/watch?v=yFpxQxEWNc4

  10. vs2013cs页面的代码太长,除了方法,没有折叠,如何处理

    VS再带一款插件,工具->扩展和更新,然后选择"联机",在搜索框搜索C# outline 2013,然后安装重启vs即可