简体   繁体   English

可学习的 LeakyReLU 激活 function 和 Pytorch

[英]Learnable LeakyReLU activation function with Pytorch

I'm trying to write a class for Invertible trainable LeakyReLu in which the model modifies the negative_slope in each iteration,我正在尝试为可逆可训练 LeakyReLu 编写 class ,其中 model 在每次迭代中修改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 

However I set requires_grad=True , the negative slope wouldn't update.但是我设置requires_grad=True ,负斜率不会更新。 Are there any other points that I must modify?还有其他需要修改的地方吗?

Does your optimizer know it should update InvertibleLeakyReLU.negative_slope ?您的优化器是否知道它应该更新InvertibleLeakyReLU.negative_slope
My guess is - no:我的猜测是 - 不:
self.negative_slope is not defined as nn.Parameter , and therefore, by default, when you initialize your optimizer with model.parameters() negative_slope is not one of the optimization parameters. self.negative_slope未定义为nn.Parameter ,因此,默认情况下,当您使用model.parameters()初始化优化器时, negative_slope不是优化参数之一。

You can either define negative_slope as a nn.Parameter :您可以将negative_slope定义为nn.Parameter

self.negative_slope = nn.Parameter(data=torch.tensor(negative_slope), requires_grad=True)

Or, explicitly pass negative_slope from all InvertibleLeakyReLU in your model to the optimizer.或者,将 model 中所有InvertibleLeakyReLU中的negative_slope显式传递给优化器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于“未知的激活函数:LeakyReLU”而无法加载模型 - Unable to load_model due to 'unknown activation_function: LeakyReLU' 如何在 pytorch 中使用可学习的参数,限制在 0 和 1 之间? - How to use a learnable parameter in pytorch, constrained between 0 and 1? 如何在 PyTorch 中返回一个自定义激活 function 的可训练参数? - How to return of one trainable parameters of custom activation function in PyTorch? 如果 abs(x)>T 作为激活 function 在 pytorch 中实现 x=T - Implement x=T if abs(x)>T as an activation function in pytorch Pytorch 自定义激活函数? - Pytorch custom activation functions? 如何向 pytorch 中的网络 output 通道之一添加可学习偏差 - How to add a learnable bias to one of the network output channel in pytorch pytorch 模块如何在其属性中从模块中收集可学习的参数? - How does pytorch Module collect learnable parameters from modules in its attributes? 计算PyTorch中的激活图空间梯度 - Calculation of activation maps spatial gradients in PyTorch 如何更改 Pytorch 预训练模块中的激活层? - How to change activation layer in Pytorch pretrained module? 如何使用参数实现当前的pytorch激活函数? - How to implement current pytorch activation functions with parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM