a=torch.tensor([[[5,5,5,5], [6,6,6,6], [7,7,7,7]], [[1,1,1,1],[2,2,2,2],[3,3,3,3]]])
mask = torch.ByteTensor([[[1],[1],[0]],[[0],[1],[1]]])
print('a.size()\n',a.size())
print('mask.size()\n',mask.size())
输出:
a.size()
torch.Size([2, 3, 4])
mask.size()
torch.Size([2, 3, 1])
使用:
代码
import torch
a=torch.tensor([[[5,5,5,5], [6,6,6,6], [7,7,7,7]], [[1,1,1,1],[2,2,2,2],[3,3,3,3]]])
print(a)
print(a.size())
print("#############################################3")
mask = torch.ByteTensor([[[1],[1],[0]],[[0],[1],[1]]])
print(mask.size())
b = a.masked_fill(mask==0, value=torch.tensor(-1e9))
print(b)
print(b.size())
输出:
tensor([[[5, 5, 5, 5],
[6, 6, 6, 6],
[7, 7, 7, 7]],
[[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]]])
torch.Size([2, 3, 4])
#############################################3
torch.Size([2, 3, 1])
tensor([[[ 5, 5, 5, 5],
[ 6, 6, 6, 6],
[-1000000000, -1000000000, -1000000000, -1000000000]],
[[-1000000000, -1000000000, -1000000000, -1000000000],
[ 2, 2, 2, 2],
[ 3, 3, 3, 3]]])
torch.Size([2, 3, 4])
代码:
import torch
a=torch.tensor([[[5,5,5,5], [6,6,6,6], [7,7,7,7]], [[1,1,1,1],[2,2,2,2],[3,3,3,3]]])
print(a)
print(a.size())
print("#############################################3")
mask = torch.ByteTensor([[[1],[1],[0]],[[0],[1],[1]]])
print(mask.size())
b = a.masked_fill(mask, value=torch.tensor(-1e9))
print(b)
print(b.size())
输出:
tensor([[[5, 5, 5, 5],
[6, 6, 6, 6],
[7, 7, 7, 7]],
[[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]]])
torch.Size([2, 3, 4])
#############################################3
torch.Size([2, 3, 1])
tensor([[[-1000000000, -1000000000, -1000000000, -1000000000],
[-1000000000, -1000000000, -1000000000, -1000000000],
[ 7, 7, 7, 7]],
[[ 1, 1, 1, 1],
[-1000000000, -1000000000, -1000000000, -1000000000],
[-1000000000, -1000000000, -1000000000, -1000000000]]])
torch.Size([2, 3, 4])
参考连接:
https://zhuanlan.zhihu.com/p/151783950