pytorch masked_fill

原文链接: https://blog.csdn.net/qq_40210472/article/details/88826821
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.csdn.net/qq_40210472/article/details/88826821

你可能感兴趣的:(pytorch,masked_fill)