pyTorch 中的反向传播考虑哪些参数?
我有一个名为 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() 方法的帮助,还是需要单独调用它们?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
反向传播算法使用链条规则来计算损失功能的衍生功能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.