I am creating a vae model over the mnist dataset with hopes of plotting the loss function against the epochs. However I am experiencing some issues and was unable to find a solution online. During my imports I have the following imports (just to give some context to the code):
from keras import backend as K from keras.layers import Input, Dense, Lambda, Layer, Add, Multiply from keras.models import Model, Sequential from keras.datasets import mnist
I have also created a function called bn for my own loss calculations and the class KLDivergenceLayer that adds a KL divergence layer to the final model loss.
Code:
decoder = Sequential([ Dense(intermediate_dim, input_dim=latent_dim, activation='relu'), Dense(original_dim, activation='sigmoid') ]) x = Input(shape=(original_dim,)) h = Dense(intermediate_dim, activation='relu')(x) z_mu = Dense(latent_dim)(h) z_log_var = Dense(latent_dim)(h) z_mu, z_log_var = KLDivergenceLayer()([z_mu, z_log_var]) z_sigma = Lambda(lambda t: K.exp(.5*t))(z_log_var) eps = Input(tensor=K.random_normal(stddev=epsilon_std, shape=(K.shape(x)[0], latent_dim))) z_eps = Multiply()([z_sigma, eps]) z = Add()([z_mu, z_eps]) x_pred = decoder(z) vae = Model(inputs=[x, eps], outputs=x_pred) vae.compile(optimizer='rmsprop', loss=bn) # train the VAE on MNIST digits (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape(-1, original_dim) / 255. x_test = x_test.reshape(-1, original_dim) / 255. vae.fit(x_train, x_train, shuffle=True, epochs=epochs, batch_size=batch_size, validation_data=(x_test, x_test))
and the error I get back to me is as follows:
ValueError: Layer model expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(100, 784) dtype=float32>]
Advertisement
Answer
The problem is in your eps layer. it’s not a model input
you can substitute it with a simple layer like this one:
eps = Lambda(lambda t: K.random_normal(stddev=1.0, shape=(K.shape(t)[0], latent_dim)))(z_log_var)
here u can find the running notebook:
https://colab.research.google.com/drive/1fLmfwCA_TOeOlXiiSabp4C4UKq73ofEN?usp=sharing