【发布时间】:2022-01-16 22:57:11
【问题描述】:
我正在尝试为 Invertible trainable LeakyReLu 编写一个类,其中模型在每次迭代中修改negative_slope,
class InvertibleLeakyReLU(nn.Module):
def __init__(self, negative_slope):
super(InvertibleLeakyReLU, self).__init__()
self.negative_slope = torch.tensor(negative_slope, requires_grad=True)
def forward(self, input, logdet = 0, reverse = False):
if reverse == True:
input = torch.where(input>=0.0, input, input *(1/self.negative_slope))
log = - torch.where(input >= 0.0, torch.zeros_like(input), torch.ones_like(input) * math.log(self.negative_slope))
logdet = (sum(log, dim=[1, 2, 3]) +logdet).mean()
return input, logdet
else:
input = torch.where(input>=0.0, input, input *(self.negative_slope))
log = torch.where(input >= 0.0, torch.zeros_like(input), torch.ones_like(input) * math.log(self.negative_slope))
logdet = (sum(log, dim=[1, 2, 3]) +logdet).mean()
return input, logdet
但是我设置了requires_grad=True,负斜率不会更新。还有其他需要修改的地方吗?
【问题讨论】:
-
您可能希望使用
nn.Parameter而不是torch.Tensor(pytorch.org/docs/stable/generated/…)。参数被添加到参数列表中并将被更新。
标签: python deep-learning pytorch tensor activation-function