pytorch中的一些方法用法集锦

mac2026-06-14  14

目录

一、pack_padded_sequence和pad_packed_sequence二、masked_fill()三、nn.ModuleList 和 nn.Sequential

一、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

最新回复(0)