此VAE模型无法编译



我正在构建一个VAE,我见过一些架构我制作了一个编码器模型,一个解码器模型,然后VAE将它们连接起来但是在编译VAE时,它给了我一个错误

def sampling(args):
    z_mean, z_log_var = args
    batch = K.shape(z_mean)[0]
    dim = K.int_shape(z_mean)[1]
    # by default, random_normal has mean = 0 and std = 1.0
    epsilon = K.random_normal(shape=(batch, dim))
    sample = z_mean + K.exp(0.5 * z_log_var) * epsilon
    return sample
#z_zample is a lambda layer sampling from mean and log_sigma
encoder = Model(vae_input, z_sample)
#the decoder whit it's own output
decoder = Model(latent_input, y)
#then the vae
vae = Model(vae_input, decoder(encoder(vae_input)))
vae.summary()
Layer (type)                 Output Shape              Param #   
=================================================================
input_27 (InputLayer)        (None, 256, 256, 3)       0         
_________________________________________________________________
model_25 (Model)             (None, 256)               537755232 
_________________________________________________________________
model_29 (Model)             (None, 256, 256, 3)       537754179 
=================================================================
Total params: 1,075,509,411
Trainable params: 1,075,509,411
Non-trainable params: 0

似乎一切都在这里连接起来。

将损耗添加到自动编码器:

def vae_loss(x, x_decoded_mean):
    kl_loss = - 0.5 * K.sum(1. + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)   
    kl_loss = K.mean(kl_loss)
    loss = (  0.5 * math.log(2 * math.pi)
            + 0.5 * K.log(_x_decoded_var + var_epsilon)
            + 0.5 * K.square(x - x_decoded_mean) / (_x_decoded_var + var_epsilon))
    loss = K.sum(loss, axis=-1)
    loss = K.mean(loss)
    return loss + kl_loss

vae.add_loss(vae_loss)
vae.compile(optimizer='adam')

。这是我得到的错误

<ipython-input-113-99395114942f> in <module>()
     14 
     15 vae.add_loss(vae_loss)
---> 16 vae.compile(optimizer='adam')
     17 vae.summary()
C:ProgrammingAnacondalibsite-packageskerasenginetraining.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
    358             # and other layer-specific losses.
    359             for loss_tensor in self.losses:
--> 360                 total_loss += loss_tensor
    361 
    362         # List of same size as output_names.
TypeError: unsupported operand type(s) for +=: 'float' and 'function'

> add_loss(( 方法需要一个张量。您应该传递调用 vae_loss(( 方法的结果。

vae.add_loss(vae_loss())

相关内容

  • 没有找到相关文章

最新更新