不要在 PyTorch 中包含梯度计算操作

时间:2021-01-15 18:42:40

标签: pytorch autograd

我有一个自定义层。让图层称为“高斯”

class Gaussian(nn.Module):
  def __init__():  
    super(Gaussian, self).__init__()
 
 #@torch.no_grad     
  def forward(self, x):
    _r = np.random.randint(0, x.shape[0], x.shape[0]) 
    _sample = x[_r] 
    _d = (_sample - x)
    _number = int(self.k * x.shape[0])
    x[1: _number] = x[1: _number] + (self.n * _d[1: _number]).detach()

    return x

上面的类将如下使用:

cnn_model = nn.Sequential(nn.Conv2d(1, 32, 5), Gaussian(), nn.ReLU(), nn.Conv2d(32, 32, 5))

如果 x 是输入,我希望 x 的梯度排除高斯模块中存在的操作,但包括神经网络其他层的计算(nn.Conv2d 等).

最后,我的目标是使用 Gaussian 模块进行计算,但该计算不应包含在梯度计算中。

我尝试执行以下操作:

  1. 使用了@torch.no_grad 上面的高斯前向方法

  2. 在 Gaussian 模块中的每个操作之后使用 detach:

    x[1: _number] = x[1: _number] + (self.n * _d[1: _number]).detach() 和其他操作类似

  3. 在 forward 方法中使用 y = x.detach()。对 y 执行操作,然后 x.data = y

以上方法是否正确?

P.S:问题已编辑

1 个答案:

答案 0 :(得分:1)

当有参数需要优化时,梯度计算才有意义。

如果你的模块没有任何参数,那么不会存储梯度,因为没有参数可以关联它。

相关问题