pyTorch 中的反向传播考虑哪些参数?

发布于 2025-01-20 13:38:06 字数 1552 浏览 2 评论 0 原文

我有一个名为 Parent 的父模块,其中包含 2 个子组件。子模块在父组件下面定义。

class Parent(nn.Module):

  def __init__(self,in_features,z_dim, img_dim):
        super().__init__()
        self.my_child1 = Child1 (z_dim, img_dim)
        self.my_child2 = Child2 (in_features)
      

  def forward(self,input):
         input=self.my_child1(input)
         input=self.my_child2(input)    
         return input
  
  def forward1(self,input):
         input=self.my_child1(input)
         return input
         
  def forward2(self,input):
         input=self.my_child2(input)
         return input


class Child2(nn.Module):
    def __init__(self, in_features):
        super().__init__()
        self.child2 = nn.Sequential(
            nn.Linear(in_features, 128),
            nn.LeakyReLU(0.01),
            nn.Linear(128, 1),
            nn.Sigmoid(),
        )

    def forward(self, x):
        return self.child2(x)


class Child1(nn.Module):
    def __init__(self, z_dim, img_dim):
        super().__init__()
        self.child1 = nn.Sequential(
            nn.Linear(z_dim, 256),
            nn.LeakyReLU(0.01),
            nn.Linear(256, img_dim),
            nn.Tanh(), 
        )

    def forward(self, x):
        return self.child1(x)

criterion=nn.BCELoss()
model=Parent(in_features,z_dim, img_dim)
output1=model.forward(noise)
loss=criterion(output1,torch.ones_like(output1))
loss.backward()

现在,当调用loss.backward()时,反向传播针对哪些参数进行? (child1/child2或两者的参数?)

如果需要在任意一个子网络上进行反向传播怎么办?我可以从父模块中获取forward1() 或forward2() 方法的帮助,还是需要单独调用它们?

I have a parent module named as Parent and it has 2 child components packed in it. The child modules are defined below the parent component.

class Parent(nn.Module):

  def __init__(self,in_features,z_dim, img_dim):
        super().__init__()
        self.my_child1 = Child1 (z_dim, img_dim)
        self.my_child2 = Child2 (in_features)
      

  def forward(self,input):
         input=self.my_child1(input)
         input=self.my_child2(input)    
         return input
  
  def forward1(self,input):
         input=self.my_child1(input)
         return input
         
  def forward2(self,input):
         input=self.my_child2(input)
         return input


class Child2(nn.Module):
    def __init__(self, in_features):
        super().__init__()
        self.child2 = nn.Sequential(
            nn.Linear(in_features, 128),
            nn.LeakyReLU(0.01),
            nn.Linear(128, 1),
            nn.Sigmoid(),
        )

    def forward(self, x):
        return self.child2(x)


class Child1(nn.Module):
    def __init__(self, z_dim, img_dim):
        super().__init__()
        self.child1 = nn.Sequential(
            nn.Linear(z_dim, 256),
            nn.LeakyReLU(0.01),
            nn.Linear(256, img_dim),
            nn.Tanh(), 
        )

    def forward(self, x):
        return self.child1(x)

criterion=nn.BCELoss()
model=Parent(in_features,z_dim, img_dim)
output1=model.forward(noise)
loss=criterion(output1,torch.ones_like(output1))
loss.backward()

Now when loss.backward() is called, backpropagation is conducted with respect to which parameters? (Parameters of child1/child2 or both?)

What should I do if I need to conduct backpropagation on any of the child network? Could I take help of forward1() or forward2() method from the Parent module or do I need to call them seperately?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海少女心 2025-01-27 13:38:06

反向传播算法使用链条规则来计算损失功能的衍生功能wrt wrt wrt wrt wrt和参数。这取决于您以及如何初始化 Optimizer.Step(),确定哪些参数将被更改。

The backpropagation algorithm uses the chain rule to compute the derivatives of the loss function w.r.t all inputs and parameters. It is up to you and how you initialize your optimizer to determine which parameters are going to be changed when calling optimizer.step() based on the gradients estimated using the backpropagation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文