【发布时间】:2019-05-01 20:43:23
【问题描述】:
我试图在 PyTorch 中屏蔽(强制为零)特定的权重值。我试图掩盖的权重在def __init__
class LSTM_MASK(nn.Module):
def __init__(self, options, inp_dim):
super(LSTM_MASK, self).__init__()
....
self.wfx = nn.Linear(input_dim, curernt_output, bias=add_bias)
掩码也在def __init__中定义为
self.mask_use = torch.Tensor(curernt_output, input_dim)
掩码是一个常数,.requires_grad_() 是False 的掩码参数。现在在课程的def forward 部分中,我尝试在线性运算完成之前对权重参数和掩码进行元素乘法
def forward(self, x):
....
self.wfx.weight = self.wfx.weight * self.mask_use
wfx_out = self.wfx(x)
我收到一条错误消息:
self.wfx.weight = self.wfx.weight * self.mask_use
File "/home/xyz/anaconda2/lib/python2.7/site-packages/torch/nn/modules/module.py", line 537, in __setattr__
.format(torch.typename(value), name))
TypeError: cannot assign 'torch.cuda.FloatTensor' as parameter 'weight' (torch.nn.Parameter or None expected)
但是当我使用.type() 检查这两个参数时,它们都显示为torch.cuda.FloatTensor。我不知道为什么这里有错误。
【问题讨论】: