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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章