【问题标题】:Cut one part of an autoencoder (pytorch)剪切自动编码器的一部分(pytorch)
【发布时间】:2021-11-08 14:29:30
【问题描述】:

我在 PyTorch 中有一个简单的自动编码器架构,我训练它来进行特征压缩和重建。我的目标是使用自动编码器的潜在空间来减少我的数据的初始维度并在测试阶段对其进行压缩。 要执行此操作,我只需将测试数据传递给我的编码器,而不是整个自动编码器。你知道如何做到这一点吗? model = Autoencoder.encoder() 之类的东西? 我的完整架构如下:

class Autoencoder(nn.Module):
    def __init__(self, n_features):
        super(Autoencoder, self).__init__()
        
        self.n_sensors = n_sensors
        
        self.encoder = nn.Sequential(
            nn.Linear(self.n_features, 1),
            nn.ReLU(True))
        
        
        self.decoder = nn.Sequential(
            nn.Linear(1, self.n_features),
            nn.ReLU(True))

    def forward(self, x):
        x = self.encoder(x)
        x = self.decoder(x)
        return x

【问题讨论】:

  • 我认为你可以做到 (model = Autoencoder.encoder())。

标签: python pytorch autoencoder


【解决方案1】:

根据你的模型定义,你可以直接在编码器子模块上调用:

class Autoencoder(nn.Module):
  def __init__(self, n_features):
      super(Autoencoder, self).__init__()
      
      self.n_features = n_features
      
      self.encoder = nn.Sequential(
          nn.Linear(self.n_features, 1),
          nn.ReLU(True))
      
      self.decoder = nn.Sequential(
          nn.Linear(1, self.n_features),
          nn.ReLU(True))

  def forward(self, x):
      x = self.encoder(x)
      x = self.decoder(x)
      return x

请记住,您首先需要初始化模型:

>>> ae = Autoencoder(n_features=10)
>>> x = torch.empty(16, 10)
>>> ae.encoder(x).shape
(16, 1)

【讨论】:

  • 感谢您的回答。就我而言,我训练了整个自动编码器(即编码器 + 解码器),然后我只想在测试模式下使用编码器,使用我训练过的模型的权重。如何将这些权重加载到ae.encoder(x)
  • 如果你已经初始化了,你可以调用encoder属性,是的。
猜你喜欢
  • 2021-06-14
  • 1970-01-01
  • 2019-06-29
  • 2013-02-25
  • 2019-12-10
  • 2021-03-16
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多