Transformer中数据维度的变化和mask

会用到的一些函数

nn.Embedding()padding_idx

a = torch.LongTensor([0,1,2,3,4])
emb = nn.Embedding(10,5,padding_idx=0)
emb(a)

## output:
# tensor([[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
#         [ 1.2378,  0.2666,  0.3143, -0.4785, -0.0261],
#         [ 0.9385,  1.8889, -2.1237,  0.9485, -0.5656],
#         [-0.6171,  0.3276, -0.5347,  0.1167, -0.7167],
#         [-0.5722,  1.7916, -2.8614,  0.1669,  1.2874]],
#        grad_fn=<EmbeddingBackward>)

在这个句子中,0是<pad>所对应的索引,padding_idx=0时,这里不做embedding处理。

unsqueeze(1) 增加维度

a = torch.LongTensor([1,2,3])
print(a.shape)
a = a.unsqueeze(1)
print(a.shape)

# torch.Size([3])
# torch.Size([3, 1])

np.triu创建一个上三角矩阵,右上角为1,左下角为0

print(np.triu(np.ones((1, 8, 8)), k=1).astype('uint8'))
# k=1即从第二列(索引为1)开始;
# [[[0 1 1 1 1 1 1 1]
#   [0 0 1 1 1 1 1 1]
#   [0 0 0 1 1 1 1 1]
#   [0 0 0 0 1 1 1 1]
#   [0 0 0 0 0 1 1 1]
#   [0 0 0 0 0 0 1 1]
#   [0 0 0 0 0 0 0 1]
#   [0 0 0 0 0 0 0 0]]]

torch.Tensor中查找某个特定值的所有位置

value = 1
print((test_tensor==1).nonzero())

masked_fill用法

a = torch.LongTensor([[1,2,3],[4,5,6]])
masking = torch.LongTensor([[1,1,1],[0,0,0]])
masking = (masking==0) #get the booling value, if equal to 0, then set it to True
a.masked_fill(masking,value=8)
# tensor([[1, 2, 3],
#         [8, 8, 8]])

Layernorm

image-20210110101327483
a = torch.rand((2,50,1,64))
###layernorm的形状即是输入张量的除第一位以外的形状
layer_norm = nn.LayerNorm(a.size()[1:],eps=1e-6)
layer_norm(a)
image-20210107200905532

torch.eq

>>> torch.eq(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
tensor([[ True, False],
        [False, True]])

Encoder

Input: batch of sentences with max length = 50

embedding_layer + positional_embedding

[batch_size,max_len,1,embed_size]

在词向量层中,我们需要把<pad>对应的index设置为0。在第一次传入Multi-head attention之前,我们可以直接在词向量层中将其设置为0;

Encoder block(xN stacks)

Input size: [batch_size,max_length,1,embed_size]

Multi-head attention

将输入的输入做了上述操作之后,得到Q,K,V三个矩阵,在进行softmax()之前,由于我们不希望看到<pad>位置上对应的内容,用masked_fill,将所有原<pad>位置的数设置为-inf. 这样softmax()对应的结果就会是0.

接下来详细说明Multi_head中的维度变化:

假设我

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值