<jupyter_start><jupyter_text>Flexible VAE latent features generator<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
from sklearn.datasets import load_iris
import numpy as np
import pandas as pd
from keras.layers import Dense, Input, Lambda
from keras.models import Model,Sequential
from keras.optimizers import Adam
from keras.layers.merge import Concatenate
from keras import backend as K
from keras import metrics
funnel[:-1]
original_dim = 4
funnel=(9,5,3)

mod_e = Sequential(name='encoder')
mod_e.add(Dense(funnel[0], input_dim=original_dim))
for i in funnel[1:]:
    mod_e.add(Dense(i, activation='elu'))
                
mod_e.summary()
z_mean = mod_e.output
z_log_var = mod_e.output

# batch_size = batch
# epochs = epoch
epsilon_std = 0.1

def sampling(x,grps):
    z_mean, z_log_var = grps
    epsilon = K.random_normal(shape=(K.shape(z_mean)[0], funnel[-1]), mean=0.,
                              stddev=epsilon_std)
    return z_mean + K.exp(z_log_var / 2) * epsilon

z = Lambda(sampling, output_shape=(funnel[-1],),arguments={'grps':[z_mean, z_log_var]})
    
mod_d = Sequential(name='decoder')
for i,v in enumerate(reversed(funnel[:-1])):
    if i == 0:
        mod_d.add(Dense(v, input_dim=funnel[-1],activation='elu'))
    else:
        mod_d.add(Dense(v, activation='elu'))
# mod_d = Model(inputs=inp,outputs=z)
mod_d.add(Dense(original_dim, activation='elu'))
mod_d.summary()

mod = Sequential()
mod.add(mod_e)
mod.add(z)
mod.add(mod_d)
mod.summary()

def vae_loss(y_true, y_pred):
#         x = K.flatten(x)
#         x_decoded_mean = K.flatten(x_decoded_mean)
    print(y_true,y_pred)    
    xent_loss = original_dim * metrics.mean_squared_error(y_true, y_pred)
    kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
    return xent_loss + kl_loss
mod.compile(optimizer='adam', loss=vae_loss)

mod.fit(df.iloc[:,:4].values)
def vae_latent_feature(data,components='auto',funnel=(2.5,9),
                           batch=200,epoch = 250,epsilon=1.0,opt= None,
                           validation=False, plot_it=False):
    
    original_dim = data.shape[1]
    
    batch_size = batch
    epochs = epoch
    epsilon_std = epsilon
    
    # making encoder
    mod_e = Sequential(name='encoder')
    mod_e.add(Dense(funnel[0], input_dim=original_dim))
    for i in funnel[1:]:
        mod_e.add(Dense(i, activation='elu'))

    mod_e.summary()
    z_mean = mod_e.output
    z_log_var = mod_e.output

    def sampling(x,grps):
        z_mean, z_log_var = grps
        epsilon = K.random_normal(shape=(K.shape(z_mean)[0], funnel[-1]), mean=0.,
                                  stddev=epsilon_std)
        return z_mean + K.exp(z_log_var / 2) * epsilon
    
    # making sampling layer
    z = Lambda(sampling, output_shape=(funnel[-1],),arguments={'grps':[z_mean, z_log_var]})
    
    #making decoder
    mod_d = Sequential(name='decoder')
    for i,v in enumerate(reversed(funnel[:-1])):
        if i == 0:
            mod_d.add(Dense(v, input_dim=funnel[-1],activation='elu'))
        else:
            mod_d.add(Dense(v, activation='elu'))
    mod_d.add(Dense(original_dim, activation='elu'))
    mod_d.summary()
    
    
    # instantiate VAE model
    vae = Sequential()
    vae.add(mod_e)
    vae.add(z)
    vae.add(mod_d)

    # Compute VAE loss
#     xent_loss = original_dim * metrics.mean_squared_error(mod_e.get_layer(index=1).input, mod_d.get_output_at(len(mod_d.layers)-1))
#     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)
    
    def vae_loss(y_true, y_pred):
#         x = K.flatten(x)
#         x_decoded_mean = K.flatten(x_decoded_mean)
        xent_loss = original_dim * metrics.mean_squared_error(y_true, y_pred)
        kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
        return xent_loss + kl_loss

#     vae.add_loss(vae_loss)
    if opt is None:
        opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=True)
    vae.compile(optimizer='adam', loss=vae_loss)
    vae.summary()
    
    # fitting our generated features on the data
    vae.fit(data,
#             shuffle=True,
            epochs=50,
            batch_size=10
    #         validation_data=(x_test, None)
           )
    
    # Encoder model for feature generation
    encoder = mod_e

    # display a 2D plot of the digit classes in the latent space
    tr = encoder.predict(data, batch_size=batch_size)
    if plot_it:
        print('\nPlotting features!\n')
        # Plotting with SNS
        sns.set(style="ticks")
        df_plot = pd.DataFrame(tr)
        g = sns.PairGrid(df_plot)
        g = g.map_upper(plt.scatter)
        g = g.map_lower(sns.kdeplot, cmap="Blues_d")
        g = g.map_diag(sns.kdeplot, lw=3, legend=False)
        g
        
    return tr, latent_dim, encoder

data = load_iris()
df  = pd.concat([pd.DataFrame(data.data),pd.DataFrame(data.target)],axis = 1)
data.feature_names.append('target')
df.columns = data.feature_names
print(df.head())
df.describe()
vae_latent_feature(data=df.iloc[:,0:4],funnel=(7,2),batch=10)
print(plt.style.available)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13304721030042918, 'fraction_numerical': 0.014928158238477329, 'mean_word_length': 3.123076923076923, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '465394', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2015, 'n_words': 361, 'path': 'misc - work/Vae Function.ipynb', 'repo_name': 'jkapila/paper-codebase', 'size': '5359'}