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):
JavaScript
x
6
1
from keras import backend as K
2
3
from keras.layers import Input, Dense, Lambda, Layer, Add, Multiply
4
from keras.models import Model, Sequential
5
from keras.datasets import mnist
6
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:
JavaScript
1
35
35
1
decoder = Sequential([
2
Dense(intermediate_dim, input_dim=latent_dim, activation='relu'),
3
Dense(original_dim, activation='sigmoid')
4
])
5
6
x = Input(shape=(original_dim,))
7
h = Dense(intermediate_dim, activation='relu')(x)
8
9
z_mu = Dense(latent_dim)(h)
10
z_log_var = Dense(latent_dim)(h)
11
12
z_mu, z_log_var = KLDivergenceLayer()([z_mu, z_log_var])
13
z_sigma = Lambda(lambda t: K.exp(.5*t))(z_log_var)
14
15
eps = Input(tensor=K.random_normal(stddev=epsilon_std,
16
shape=(K.shape(x)[0], latent_dim)))
17
z_eps = Multiply()([z_sigma, eps])
18
z = Add()([z_mu, z_eps])
19
20
x_pred = decoder(z)
21
22
vae = Model(inputs=[x, eps], outputs=x_pred)
23
vae.compile(optimizer='rmsprop', loss=bn)
24
25
# train the VAE on MNIST digits
26
(x_train, y_train), (x_test, y_test) = mnist.load_data()
27
x_train = x_train.reshape(-1, original_dim) / 255.
28
x_test = x_test.reshape(-1, original_dim) / 255.
29
30
vae.fit(x_train, x_train,
31
shuffle=True,
32
epochs=epochs,
33
batch_size=batch_size,
34
validation_data=(x_test, x_test))
35
and the error I get back to me is as follows:
JavaScript
1
2
1
ValueError: Layer model expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(100, 784) dtype=float32>]
2
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:
JavaScript
1
2
1
eps = Lambda(lambda t: K.random_normal(stddev=1.0, shape=(K.shape(t)[0], latent_dim)))(z_log_var)
2
here u can find the running notebook:
https://colab.research.google.com/drive/1fLmfwCA_TOeOlXiiSabp4C4UKq73ofEN?usp=sharing