nathanhubens / Autoencoders

Implementation of simple autoencoders networks with Keras
MIT License
234 stars 89 forks source link

Unable to train model in Variational Autoencoders.ipynb #3

Open hellomurphy opened 3 years ago

hellomurphy commented 3 years ago

I have tried running this line of code in the Variational Autoencoders markdown.

history = vae.fit(X_train, X_train,shuffle=True, epochs=epochs, batch_size=batch_size, validation_data=(X_test, X_test))

But found an error showing the result as follows

TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

handenurc commented 2 years ago

I got exactly the same error. Any help is appreciated.

sunwucheng commented 1 year ago

I met the same error. After referring to https://github.com/bojone/vae/blob/master/vae_keras_cnn.py It seems that I solved the problem by replacing

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

vae = Model(x, x_decoded_mean)
vae.compile(optimizer='rmsprop', loss = vae_loss)
vae.summary()

with

vae = Model(x, x_decoded_mean)

xent_loss = original_dim * 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)

vae.add_loss(vae_loss)
vae.compile(optimizer='rmsprop')
vae.summary()