使用 Pytorch 提取自动编码器隐藏层的特征 [英] Extracting features of the hidden layer of an autoencoder using Pytorch

查看:108
本文介绍了使用 Pytorch 提取自动编码器隐藏层的特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注本教程来训练自动编码器.

I am following this tutorial to train an autoencoder.

培训进行得很顺利.接下来,我有兴趣从隐藏层(编码器和解码器之间)提取特征.

The training has gone well. Next, I am interested to extract features from the hidden layer (between the encoder and decoder).

我该怎么做?

推荐答案

最简洁、最直接的方法是添加用于创建部分输出的方法——这甚至可以在经过训练的模型上进行后验.

The cleanest and most straight-forward way would be to add methods for creating partial outputs -- this can be even be done a posteriori on a trained model.

from torch import Tensor

class AE(nn.Module):
    def __init__(self, **kwargs):
        ...

    def encode(self, features: Tensor) -> Tensor:
        h = torch.relu(self.encoder_hidden_layer(features))
        return torch.relu(self.encoder_output_layer(h))

    def decode(self, encoded: Tensor) -> Tensor:
        h = torch.relu(self.decoder_hidden_layer(encoded))
        return torch.relu(self.decoder_output_layer(h))

    def forward(self, features: Tensor) -> Tensor:
        encoded = self.encode(features)
        return self.decode(encoded)

您现在可以通过使用相应的输入张量调用 encode 来查询编码器隐藏状态的模型.

You can now query the model for encoder hidden states by simply calling encode with the corresponding input tensor.

如果您不想向基类添加任何方法(我不明白为什么),您也可以编写一个外部函数:

If you'd rather not add any methods to the base class (I don't see why), you could alternatively write an external function:

def get_encoder_state(model: AE, features: Tensor) -> Tensor:
   return torch.relu(model.encoder_output_layer(torch.relu(model.encoder_hidden_layer(features))))

这篇关于使用 Pytorch 提取自动编码器隐藏层的特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆