简体   繁体   中英

Keras Variational Autoencoder Stopped Working Despite Clean Model Summary?

I have been utilizing the below Variational Autoencoder for a while, but it recently just stopped working after a recent environment reset. I have searched through the documentation and recent change logs from tensorflow (but I cant seem to find a good release history anywhere that goes back past a month), but did not find any changes in the basic definition of any of these functions. Additionally, I am getting the very dry error code which is not very descriptive or helpful.

AssertionError: in user code:

AssertionError: Could not compute output Tensor("sequential/dense_1/Sigmoid:0", shape=(None, 110), dtype=float32)

The error just seems to be pointing to the last line, because it cannot really be more descriptive. However, when looking at the model summary, the model seems to be recognizing all layers and the associated build.

vae.summary()
Model: "functional_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 110)]        0                                            
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 64)           7104        input_1[0][0]                    
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 2)            130         dense_2[0][0]                    
__________________________________________________________________________________________________
dense_4 (Dense)                 (None, 2)            130         dense_2[0][0]                    
__________________________________________________________________________________________________
kl_divergence_layer (KLDivergen [(None, 2), (None, 2 0           dense_3[0][0]                    
                                                                 dense_4[0][0]                    
__________________________________________________________________________________________________
lambda (Lambda)                 (None, 2)            0           kl_divergence_layer[0][1]        
__________________________________________________________________________________________________
input_2 (InputLayer)            [(None, 2)]          0                                            
__________________________________________________________________________________________________
multiply (Multiply)             (None, 2)            0           lambda[0][0]                     
                                                                 input_2[0][0]                    
__________________________________________________________________________________________________
add (Add)                       (None, 2)            0           kl_divergence_layer[0][0]        
                                                                 multiply[0][0]                   
__________________________________________________________________________________________________
sequential (Sequential)         (None, 110)          7342        add[0][0]                        
==================================================================================================
Total params: 14,706
Trainable params: 14,706
Non-trainable params: 0
__________________________________________________________________________________________________

As a result, I have no idea what is wrong. Any ideas? Below is sample data with the Vae.

import pandas as pd
from sklearn.datasets import make_blobs 
from sklearn.preprocessing import MinMaxScaler

import keras.backend as K
import tensorflow as tf

from keras.layers import Input, Dense, Lambda, Layer, Add, Multiply
from keras.models import Model, Sequential
from keras.callbacks import EarlyStopping, LearningRateScheduler
from keras.objectives import binary_crossentropy


x, labels = make_blobs(n_samples=150000, n_features=110,  centers=16, cluster_std=4.0)
scaler = MinMaxScaler()
x = scaler.fit_transform(x)
x = pd.DataFrame(x)

train = x.sample(frac = 0.8)
train_indexs = train.index.values
test = x[~x.index.isin(train_indexs)]
print(train.shape, test.shape)

def nll(y_true, y_pred):
    """ Negative log likelihood (Bernoulli). """
    return K.sum(K.binary_crossentropy(y_true, y_pred), axis = -1)

class KLDivergenceLayer(Layer):
    """ Identity transform layer that adds KL divergence
    to the final model loss.
    """
    def __init__(self, *args, **kwargs):
        self.is_placeholder = True
        super(KLDivergenceLayer, self).__init__(*args, **kwargs)

    def call(self, inputs):

        mu, log_var = inputs
                            #changing form sum to mean
        kl_batch = - .5 * K.sum(1 + log_var -
                                K.square(mu) -
                                K.exp(log_var), axis=-1)

        self.add_loss(K.mean(kl_batch), inputs=inputs)

        return inputs

#################### VAE ##############


latent_dim = 2

decoder = Sequential([
    Dense(64, input_dim=latent_dim, activation='relu'),
    Dense(x.shape[1], activation='sigmoid')
])

data = Input(shape=(x.shape[1],))
h = Dense(64, activation='relu')(data)

z_mu = Dense(latent_dim)(h)
z_log_var = Dense(latent_dim)(h)

#applies KLDivergence here?
z_mu, z_log_var = KLDivergenceLayer()([z_mu, z_log_var])

#generates random samples
z_sigma = Lambda(lambda t: K.exp(.5*t))(z_log_var)
eps = Input(tensor=K.random_normal(stddev=1.0,
                                   shape=(K.shape(data)[0], latent_dim)))
z_eps = Multiply()([z_sigma, eps])
z = Add()([z_mu, z_eps])

#apply decoder
x_pred = decoder(z)

# defines final model
vae = Model(inputs=[data, eps], outputs=x_pred)
vae.compile(optimizer='rmsprop', loss=nll)

#runs model
vae.fit(train, train, shuffle = True, epochs = 1000, 
        batch_size = 512, validation_data = (test, test), 
        callbacks = [EarlyStopping(patience=50)])

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/17ugeVin4yOlSD3fBvX4jQNJ2WT7nmlnz?usp=sharing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM