取pytorch中特定层的输出



我已经在Pytorch中实现了一个自动编码器,并希望从指定的编码层提取表示(输出)。这种设置类似于使用Keras中使用的子模型进行预测。

然而,在Pytorch中实现类似的东西看起来有点挑战性。我尝试向前钩子解释了如何从PyTorch模型的特定层的输出?和https://pytorch.org/tutorials/beginner/former_torchies/nnft_tutorial.html但是没有用

你能帮我从一个特定的层得到输出吗?

我把我的代码附在下面:

class Autoencoder(torch.nn.Module):
# Now defining the encoding and decoding layers.
def __init__(self):
super().__init__()   
self.enc1 = torch.nn.Linear(in_features = 784, out_features = 256)
self.enc2 = torch.nn.Linear(in_features = 256, out_features = 128)
self.enc3 = torch.nn.Linear(in_features = 128, out_features = 64)
self.enc4 = torch.nn.Linear(in_features = 64, out_features = 32)
self.enc5 = torch.nn.Linear(in_features = 32, out_features = 16)
self.dec1 = torch.nn.Linear(in_features = 16, out_features = 32)
self.dec2 = torch.nn.Linear(in_features = 32, out_features = 64)
self.dec3 = torch.nn.Linear(in_features = 64, out_features = 128)
self.dec4 = torch.nn.Linear(in_features = 128, out_features = 256)
self.dec5 = torch.nn.Linear(in_features = 256, out_features = 784)
# Now defining the forward propagation step
def forward(self,x):
x = F.relu(self.enc1(x))
x = F.relu(self.enc2(x))
x = F.relu(self.enc3(x))
x = F.relu(self.enc4(x))
x = F.relu(self.enc5(x))
x = F.relu(self.dec1(x))
x = F.relu(self.dec2(x))
x = F.relu(self.dec3(x))
x = F.relu(self.dec4(x))
x = F.relu(self.dec5(x))

return x
autoencoder_network = Autoencoder()

我必须从标记为enc1, enc2的编码器层中获取输出。, enc5。

最简单的方法是显式返回所需的激活:

def forward(self,x):
e1 = F.relu(self.enc1(x))
e2 = F.relu(self.enc2(e1))
e3 = F.relu(self.enc3(e2))
e4 = F.relu(self.enc4(e3))
e5 = F.relu(self.enc5(e4))
x = F.relu(self.dec1(e5))
x = F.relu(self.dec2(x))
x = F.relu(self.dec3(x))
x = F.relu(self.dec4(x))
x = F.relu(self.dec5(x))

return x, e1, e2, e3, e4, e5

你可以定义一个全局字典,比如activations = {},然后在forward函数中给它赋值,比如activations['enc1'] = x.clone().detach(),等等

相关内容

  • 没有找到相关文章

最新更新