pytorch中的一些方法用法集锦

一、pack_padded_sequence和pad_packed_sequence

pack_padded_sequence和pad_packed_sequence在 看源码的时候看到的,不懂啥意思,特意冲浪了番,记录于此。
https://www.cnblogs.com/sbj123456789/p/9834018.html
https://www.cnblogs.com/jermmyhsu/p/10020308.html
这两个网址给出了详细的前因后果

import torch
import torch.nn as nn
a = torch.randn(3, 4, 5)
print(a)
print("***********************")
length = [4, 2, 1]
embedded = nn.utils.rnn.pack_padded_sequence(a, length, batch_first=True)
print(embedded)
rnn = nn.RNN(5, 3, 1, batch_first=True, bidirectional=True)
output, hidden = rnn(embedded)
print("%%%%%%%%%%%%%%%%%%%%%%%%")
print(output)
print("$$$$$$$$$$$$$$$$$$$$$$$$4")
output, _ = nn.utils.rnn.pad_packed_sequence(output, batch_first=True)
# output自动将前向与后向拼接了
print(output)

二、masked_fill()

mask值为1的位置处用value填充, 特别需要注意的是mask的tensor类型是ByteTensor

import torch
input_tensor = torch.FloatTensor([[[1,2, 3, 3],[3,4,5, 5]],[[6,7, 7,7],[8,9,10,10]], [[1,1,1,1], [2,2,2,2]]])
mask = torch.ByteTensor([[[1],[0]],[[1],[1]],[[0],[1]]])
print(input_tensor)
print("##############")
print(mask)
print("%%%%%%%%%%%%%%%%%%%%%%%%%%")
cc =input_tensor.masked_fill(mask, 100)
print(cc)

代码运行结果如下:

/home/fang/anaconda3/envs/gcn/bin/python3.6 /home/fang/myprojects/gcn-over-pruned-trees/example.py
tensor([[[ 1.,  2.,  3.,  3.],
         [ 3.,  4.,  5.,  5.]],

        [[ 6.,  7.,  7.,  7.],
         [ 8.,  9., 10., 10.]],

        [[ 1.,  1.,  1.,  1.],
         [ 2.,  2.,  2.,  2.]]])
##############
tensor([[[1],
         [0]],

        [[1],
         [1]],

        [[0],
         [1]]], dtype=torch.uint8)
%%%%%%%%%%%%%%%%%%%%%%%%%%
tensor([[[100., 100., 100., 100.],
         [  3.,   4.,   5.,   5.]],

        [[100., 100., 100., 100.],
         [100., 100., 100., 100.]],

        [[  1.,   1.,   1.,   1.],
         [100., 100., 100., 100.]]])

Process finished with exit code 0

三、nn.ModuleList 和 nn.Sequential

https://blog.csdn.net/e01528/article/details/84397174

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>