简体   繁体   English

如何在训练自动编码器(回调)期间将 keras 中的输入随机设置为零?

[英]How to randomly set inputs to zero in keras during training autoencoder (callback)?

I am training 2 autoencoders with 2 separate input paths jointly and I would like to randomly set one of the input paths to zero.我正在用 2 个单独的输入路径联合训练 2 个自动编码器,我想将其中一个输入路径随机设置为零。

I use tensorflow with keras backend (functional API).我将 tensorflow 与 keras 后端(功能 API)一起使用。

I am computing a joint loss (sum of two losses) for backpropagation.我正在计算反向传播的联合损失(两个损失的总和)。

A -> A' & B ->B' A -> A' & B -> B'

loss => l2(A,A')+l2(B,B')损失 => l2(A,A')+l2(B,B')

networks taking A and B are connected in latent space.采用 A 和 B 的网络在潜在空间中连接。 I would like to randomly set A or B to zero and compute the loss only on the corresponding path, meaning if input path A is set to zero loss be computed only by using outputs of only path B and vice versa;我想将 A 或 B 随机设置为零并仅在相应路径上计算损失,这意味着如果输入路径 A 设置为零损失,则仅通过仅使用路径 B 的输出来计算损失;反之亦然; eg:例如:

0 -> A' & B ->B' 0 -> A' & B -> B'

loss: l2(B,B')损失:l2(B,B')

How do I randomly set input path to zero?如何将输入路径随机设置为零? How do I write a callback which does this?如何编写执行此操作的回调?

You can set an input to 0 simply:您可以简单地将输入设置为0

A = A*random.choice([0,1])

This code can be used inside a loss function此代码可以在损失函数中使用

Maybe try the following:也许尝试以下方法:

import random
def decision(probability):
  return random.random() < probability

Define a method that makes a random decision based on a certain probability x and make your loss calculation depend on this decision.定义一种基于特定概率 x 做出随机决策的方法,并使您的损失计算取决于该决策。

if current_epoch == random.choice(epochs):

  keep_mask = tf.ones_like(A.input, dtype=float32)
  throw_mask = tf.zeros_like(A.input, dtype=float32)

  if decision(probability=0.5):
      total_loss = tf.reduce_sum(reconstruction_loss_a * keep_mask
                               + reconstruction_loss_b * throw_mask)
  else:
      total_loss = tf.reduce_sum(reconstruction_loss_a*throw_mask 
                               + reconstruction_loss_b*keep_mask)    
else:
  total_loss = tf.reduce_sum(reconstruction_loss_a + reconstruction_loss_b)
      
  

I assume that you do not want to set one of the paths to zero every time you update your model parameters, as then there is a risk that one or even both models will not be sufficiently trained.我假设您不希望每次更新模型参数时都将其中一个路径设置为零,因为这样可能会导致一个甚至两个模型都无法得到充分训练。 Also note that I use the input of A to create zero_like and one_like tensors as I assume that both inputs have the same shape;另请注意,我使用A的输入来创建zero_likeone_like张量,因为我假设两个输入具有相同的形状; if this is not the case, it can easily be adjusted.如果不是这种情况,则可以轻松调整。

Depending on what your goal is, you may also consider replacing your input of A or B with a random tensor eg tf.random.normal based on a random decision.根据您的目标是什么,您还可以考虑用随机张量替换AB输入,例如基于随机决策的tf.random.normal This creates noise in your model, which may be desirable, as your model would be forced to look into the latent space to try reconstruct your original input.这会在您的模型中产生噪音,这可能是可取的,因为您的模型将被迫查看潜在空间以尝试重建您的原始输入。 This means precisely that you still calculate your reconstruction loss with A.input and A.output , but in reality your model never received the A.input , but rather the random tensor.这恰恰意味着您仍然使用A.inputA.output计算重建损失,但实际上您的模型从未收到A.input ,而是随机张量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM