0
$\begingroup$

I am training a VAE in Keras on the colab. The issue that I am encountering is that after the first epoch the loss is 0 and the validation loss is 0 (but the model didn't learn anything).
this is the code:

-- coding: utf-8 --

"""VAE HW Mariano.ipynb

Automatically generated by Collaboratory.

Original file is located at https://colab.research.google.com/drive/15LW0pd2oIDrFv8ELQS_f0mGZyKu7qBs1 """

"""### VAE Cifar 10"""

from keras import layers
from keras.layers import Conv2D, MaxPool2D, Flatten, Dense
from keras.layers import Dropout
from keras import regularizers

from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

type(x_train)

input_shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3])
original_dim=x_train.shape[1]*x_train.shape[2]
latent_dim = 12
scale_down=0.25

import keras

#encoder architecture
encoder_input = keras.Input(shape=input_shape)

cx=layers.Conv2D(filters=64*scale_down, 
                kernel_size=(3, 3),
                activation='relu',
                padding='same')(encoder_input)
cx=layers.Conv2D(filters=64*scale_down, 
                kernel_size=(3, 3),
                activation='relu',
                input_shape=(32, 32, 3),padding='same')(cx)

cx=layers.MaxPool2D(2,2)(cx)
cx=layers.Dropout(0.2)(cx)

cx=layers.Conv2D(filters=64*scale_down, 
                kernel_size=(3, 3),
                activation='relu',padding='same')(cx)
cx=layers.Conv2D(filters=64*scale_down, 
                kernel_size=(3, 3),
                activation='relu',padding='same')(cx)


cx=layers.MaxPool2D(2,2)(cx)
cx=layers.Dropout(0.2)(cx)

cx=layers.Conv2D(filters=128*scale_down,
                kernel_size=(3, 3),
                activation='relu',padding='same')(cx)
cx=layers.Conv2D(filters=128*scale_down,
                kernel_size=(3, 3),
                activation='relu',padding='same')(cx)

cx=layers.MaxPool2D(2,2)(cx)
cx=layers.Dropout(0.2)(cx)

x=layers.Flatten()(cx)

z_mean=layers.Dense(latent_dim, activation='relu', name = 'z_mean')(x) #I removed the softmax layer
z_log_sigma=layers.Dense(latent_dim, activation='relu',name = 'z_sd' )(x)

from keras import backend as K #what is that...

def sampling(args):
    z_mean, z_log_sigma = args
    epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim),
                              mean=0., stddev=0.1)
    return z_mean + K.exp(z_log_sigma) * epsilon

z = layers.Lambda(sampling)([z_mean, z_log_sigma])

# Create encoder
encoder = keras.Model(encoder_input, [z_mean, z_log_sigma, z], name='encoder')

encoder.summary()

# Get Conv2D shape for Conv2DTranspose operation in decoder
conv_shape = K.int_shape(cx)

# Create decoder
from keras.layers import Conv2DTranspose, Reshape

latent_inputs = keras.Input(shape=(latent_dim, ), name='z_sampling') #shape=(latent_dim,) or shape=late_dim?

x = layers.Dense(conv_shape[1] * conv_shape[2] * conv_shape[3], activation='relu')(latent_inputs)

x     = Reshape((conv_shape[1], conv_shape[2], conv_shape[3]))(x)

x=layers.Conv2DTranspose(filters=128*scale_down,
                kernel_size=(3, 3),
                strides=2,
                activation='relu',padding='same')(x)#(latent_inputs)
x=layers.Conv2DTranspose(filters=128*scale_down,
                kernel_size=(3, 3),
                strides=2,
                activation='relu',padding='same')(x)

x=layers.Conv2DTranspose(filters=64*scale_down, 
                kernel_size=(3, 3),
                strides=2,
                activation='relu',padding='same')(x)
x=layers.Conv2DTranspose(filters=64*scale_down, 
                kernel_size=(3, 3),
                activation='relu',padding='same')(x)

x=layers.Conv2DTranspose(filters=64*scale_down, 
                kernel_size=(3, 3),
                activation='relu',
                padding='same')(x)
x=layers.Conv2DTranspose(filters=64*scale_down, 
                kernel_size=(3, 3),
                activation='relu',
                input_shape=input_shape,padding='same')(x)

outputs = layers.Conv2D(filters=3, kernel_size=3, activation='sigmoid', padding='same', name='decoder_output')(x) #Dense(128, activation='relu')

from keras import Model
decoder = Model(latent_inputs, outputs, name='decoder')
decoder.summary()

# instantiate VAE model
outputs = decoder(encoder(encoder_input)[2]) 
vae = keras.Model(encoder_input, outputs, name='vae_mlp')

vae.summary()

#loss
#look at the shape of the input and the output 
def val_loss_func(encoder_input, outputs):
  reconstruction_loss = keras.losses.binary_crossentropy(encoder_input, outputs)
  reconstruction_loss *= original_dim
  kl_loss = 1 + z_log_sigma - K.square(z_mean) - K.exp(z_log_sigma)
  kl_loss = K.sum(kl_loss, axis=-1)
  kl_loss *= -0.5
  vae_loss = K.mean(reconstruction_loss + kl_loss)
  return vae_loss

from keras import optimizers
opt = optimizers.Adam(learning_rate=0.0001)

vae.compile(optimizer=opt, loss=val_loss_func)

vae.summary()


from tensorflow import config #import tensorflow as tf
config.run_functions_eagerly(True)

x_train=x_train[1:5000,:,:,:]

vae.fit(x_train, x_train,
        epochs=1,
        batch_size=8,
        validation_data=(x_test, x_test))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.