Keras: VAE example, run TypeError: compile() takes at least 3 arguments (2 given)

Created on 31 Jan 2018  路  2Comments  路  Source: keras-team/keras

https://github.com/keras-team/keras/blob/85f011df5a5c0fcf1f01b39eca338eb6b7e58401/examples/variational_autoencoder.py#L58

I take a look at keras/engine/training.py, and find that compile function do not need 3 arguments, so may be it's my version fault?

I simply change the line of code to

vae.compile(loss=None, optimizer='rmsprop')

Will this change do some harm or strange behavior?

My packages are listed below:
Python=2.7.13
Keras=2.1.2
tensorflow==1.4.1
tensorflow-gpu==1.4.1

Most helpful comment

@doujiang-zheng I removed the error by editing this:

vae.compile(optimizer='rmsprop', loss=None)
The loss is set to be None, because we are already adding the loss in the line:
vae.add_loss(vae_loss).
Hope this helps

All 2 comments

There is a Keras blog post that references the code (2016), however the implementation is slightly different. The loss function implementation is different. I changed to this:

# Compute VAE loss
def vae_loss(x, x_decoded_mean):
    xent_loss = original_dim * metrics.binary_crossentropy(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)
    vae_loss = K.mean(xent_loss + kl_loss)
    return vae_loss

vae.compile(optimizer='rmsprop', loss=vae_loss)

EDIT: Updated with return from custom loss; seems to be compiling without issue now. Some other errors; look solvable.

@doujiang-zheng I removed the error by editing this:

vae.compile(optimizer='rmsprop', loss=None)
The loss is set to be None, because we are already adding the loss in the line:
vae.add_loss(vae_loss).
Hope this helps

Was this page helpful?
0 / 5 - 0 ratings

Related issues

snakeztc picture snakeztc  路  3Comments

harishkrishnav picture harishkrishnav  路  3Comments

somewacko picture somewacko  路  3Comments

zygmuntz picture zygmuntz  路  3Comments

KeironO picture KeironO  路  3Comments