import torch.nn.functional as F
import numpy as np
a = torch.Tensor([1,2,3,4])
a = a.masked_fill(mask = torch.ByteTensor([1,1,0,0]), value=-np.inf)
print(a)
b = F.softmax(a)
print(b)
tensor([-inf, -inf, 3., 4.])
D:\pycharm\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py:8: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
start_new_thread = thread.start_new_thread
tensor([0.0000, 0.0000, 0.2689, 0.7311])
常见报错:
Expected object of scalar type Byte but got scalar type Long for argument #2 'mask'
原因,mask = torch.LongTensor()
解决方法:mask = torch.ByteTensor()
mask必须是一个 ByteTensor 而且shape必须和 a一样 并且元素只能是 0或1 。
函数作用:将 mask中为1的元素所在的索引,在a中相同的索引处替换为 value ,mask value必须同为tensor
原文链接:https://blog.youkuaiyun.com/qq_40210472/article/details/88826821
本文详细解析了PyTorch中masked_fill函数的使用方法及常见错误处理,包括如何正确创建mask并应用于张量,同时展示了如何利用该函数进行特定元素的替换操作。
906

被折叠的 条评论
为什么被折叠?



