【问题标题】:Chainer AutoencoderChainer 自动编码器
【发布时间】:2019-04-24 05:02:59
【问题描述】:

我正在尝试编写一个普通的自动编码器来压缩 13 个图像。但是我收到以下错误:

ValueError: 不再支持 train 参数。使用chainer.using_config

图像的形状是 (21,28,3)。

filelist = 'ex1.png', 'ex2.png',...11 other images
x = np.array([np.array(Image.open(fname)) for fname in filelist])
xs = x.astype('float32')/255.

class Autoencoder(Chain):
  def __init__(self, activation=F.relu):
    super().__init__()
    with self.init_scope():
  # encoder part
      self.l1 = L.Linear(1764,800)
      self.l2 = L.Linear(800,300)
  # decoder part
      self.l3 = L.Linear(300,800)
      self.l4 = L.Linear(800,1764)
      self.activation = activation

  def forward(self,x):
      h = self.encode(x)
      x_recon = self.decode(h)
      return x_recon

  def __call__(self,x):
      x_recon = self.forward(x)
      loss = F.mean_squared_error(h, x)
      return loss

  def encode(self, x, train=True):
      h = F.dropout(self.activation(self.l1(x)), train=train)
      return self.activation(self.l2(x))

  def decode(self, h, train=True):
      h = self.activation(self.l3(h))
      return self.l4(x)

n_epoch = 5
batch_size = 2
model = Autoencoder()

optimizer = optimizers.SGD(lr=0.05).setup(model)
train_iter = iterators.SerialIterator(xs,batch_size)
valid_iter = iterators.SerialIterator(xs,batch_size)

updater = training.StandardUpdater(train_iter,optimizer)
trainer = training.Trainer(updater,(n_epoch,"epoch"),out="result")

from chainer.training import extensions
trainer.extend(extensions.Evaluator(valid_iter, model, device=gpu_id))

trainer.run()

问题是因为模型中的节点数量还是其他原因?

【问题讨论】:

    标签: image-processing autoencoder chainer


    【解决方案1】:

    您需要编写“解码器”部分。

    当你取mean_squared_error损失时,h和x的形状必须相同。 AutoEncoder 会将原始x 编码到小空间(100-dim)h,但之后我们需要通过添加解码器部分从这个h 重构x'。 然后可以在这个重构的x'上计算损失。

    例如如下(抱歉我没有测试运行)

    • 适用于 Chainer v2~

    train 参数由 global configs 处理,因此您不需要在 dropout 函数中使用 train 参数。

    class Autoencoder(Chain):
      def __init__(self, activation=F.relu):
        super().__init__()
        with self.init_scope():
          # encoder part
          self.l1 = L.Linear(1308608,500)
          self.l2 = L.Linear(500,100)
          # decoder part
          self.l3 = L.Linear(100,500)
          self.l4 = L.Linear(500,1308608)
      self.activation = activation
    
      def forward(self,x):
          h = self.encode(x)
          x_recon = self.decode(h)
          return x_recon
    
      def __call__(self,x):
          x_recon = self.forward(x)
          loss = F.mean_squared_error(h, x)
          return loss
    
      def encode(self, x):
          h = F.dropout(self.activation(self.l1(x)))
          return self.activation(self.l2(x))
    
      def decode(self, h, train=True):
          h = self.activation(self.l3(h))
          return self.l4(x)
    
    • 适用于 Chainer v1
    class Autoencoder(Chain):
      def __init__(self, activation=F.relu):
        super().__init__()
        with self.init_scope():
          # encoder part
          self.l1 = L.Linear(1308608,500)
          self.l2 = L.Linear(500,100)
          # decoder part
          self.l3 = L.Linear(100,500)
          self.l4 = L.Linear(500,1308608)
      self.activation = activation
    
      def forward(self,x):
          h = self.encode(x)
          x_recon = self.decode(h)
          return x_recon
    
      def __call__(self,x):
          x_recon = self.forward(x)
          loss = F.mean_squared_error(h, x)
          return loss
    
      def encode(self, x, train=True):
          h = F.dropout(self.activation(self.l1(x)), train=train)
          return self.activation(self.l2(x))
    
      def decode(self, h, train=True):
          h = self.activation(self.l3(h))
          return self.l4(x)
    

    您也可以参考官方的 Variational Auto Encoder 示例进行下一步:

    【讨论】:

    • 感谢您在模型部分指出问题。以及如何在此处定义activation = relu?
    • 再次感谢..但是最初的问题仍然存在...现在我尝试使用更小尺寸的图像..28*21 = 588 和形状 (21,28,3)。错误:
    • 您可以编辑您的问题以在您的问题底部添加下一个错误吗? (请不要删除原始问题,只需添加下一个错误)。
    • 我再次更新了我的答案。看来您使用的是较新的 Chainer 版本,在这种情况下您不需要 train 参数。
    猜你喜欢
    • 1970-01-01
    • 2017-07-19
    • 2017-11-22
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多