Pytorch 词嵌入word_embedding1初识

 

torch.nn.Embedding(num_embeddings, embedding_dim, 
                   padding_idx=None, max_norm=None, 
                   norm_type=2, scale_grad_by_freq=False, 
                   sparse=False)

参数所表示的含义:

          num_embeddings (int) :嵌入字典的大小

          embedding_dim (int) :每个嵌入向量的大小

          padding_idx (int, optional) : 如果提供的话,输出遇到此下标时用零填充

         max_norm (float, optional) :如果提供的话,会重新归一化词嵌入,使它们的范数小于提供的值。

          norm_type (float, optional) :对于max_norm选项计算p范数时的p

          scale_grad_by_freq (boolean, optional) :如果提供的话,会根据字典中单词频率缩放梯度注意:没有指定训练好的词向量时,           embedding会自动生成一个随机的词向量。

 

示例:

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

#-----------定义你的数据-----------
###每个单词需要用一个数字去表示,如'hello'用0来表示###########
word2id = {'hello': 0, 'world': 1}

#---------开始创建初始词向量--------
#nn.Embedding(vocab_size, embedding_dim),
# vocab_size为词的个数,embedding_dim为词向量的长度
#如果有1000个词,每个词希望是100维,表示为nn.Embedding(1000, 100)
#这里2表示有2个词,5表示5维度,
embeds = nn.Embedding(2, 5)


#---下面几行的代码是为了访问每一个词的词向量-----------
#访问nn.Embedding里面定义的元素,
# 由于word embeding算是神经网络里面的参数,所以需要定义Variable。
#下面这行代码意思就是可以通过torch.LongTensor([0])直接构建一个Tensor
hello_idx = torch.LongTensor([word2id['hello']])
print(hello_idx)
#得到一个Variable,它的值是hello这个词的index,也就是0。
hello_idx = Variable(hello_idx)
print(hello_idx)



#得到word embedding里面关于hello这个词的初始词向量,
hello_embed = embeds(hello_idx)
print(hello_embed)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值