Advertisement
mario119

Untitled

Mar 4th, 2020
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. # VAE model = encoder(+sampling) + decoder
  2. # build encoder model
  3. def encoder_model(inputs):
  4.     x1 = Dense(intermediate_dim_1, activation='relu')(inputs)
  5.     x2 = Dense(intermediate_dim_2, activation='relu')(x1)  
  6.     x3 = Dense(intermediate_dim_3, activation='relu')(x2)    
  7.     x4 = Dense(intermediate_dim_4, activation='relu')(x3)    
  8.     z_mean_encoded = Dense(latent_dim, name='z_mean')(x4)
  9.     z_log_var_encoded = Dense(latent_dim, name='z_log_var')(x4)
  10.  
  11.     # instantiate encoder model
  12.     encoder = Model(inputs, [z_mean_encoded, z_log_var_encoded], name='encoder')
  13.     return encoder, z_mean_encoded, z_log_var_encoded
  14.  
  15.  
  16. # build decoder model
  17. def decoder_model():
  18.     latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
  19.     x4 = Dense(intermediate_dim_4, activation='relu')(latent_inputs)
  20.     x3 = Dense(intermediate_dim_3, activation='relu')(x4)
  21.     x2 = Dense(intermediate_dim_2, activation='relu')(x3)
  22.     x1 = Dense(intermediate_dim_1, activation='relu')(x2)
  23.     outputs = Dense(original_dim)(x1)
  24.  
  25.     # instantiate decoder model
  26.     decoder = Model(latent_inputs, outputs, name='decoder')
  27.     return decoder
  28.  
  29.  
  30. def sampling(args):
  31.     """Reparameterization trick by sampling fr an isotropic unit Gaussian.
  32.    # Arguments:
  33.        args (tensor): mean and log of variance of Q(z|X)
  34.    # Returns:
  35.        z (tensor): sampled latent vector
  36.    """
  37.     z_mean, z_log_var = args
  38.     batch = K.shape(z_mean)[0]
  39.     dim = K.int_shape(z_mean)[1] # Returns the shape of tensor or variable as a tuple of int or None entries.
  40.     # by default, random_normal has mean=0 and std=1.0
  41.     epsilon = K.random_normal(shape=(batch, dim))
  42.     return z_mean + K.exp(0.5 * z_log_var) * epsilon
  43.  
  44. def save_model(model,fName):    
  45.     ff = h5py.File(fName,'w')
  46.     ww = model.get_weights()
  47.     for i in range(len(ww)):
  48.         ff.create_dataset('ww'+str(i),data=ww[i])
  49.     ff.close()
  50.     print(fName,'saved')
  51.  
  52. def save_sclr(sclr,fName):
  53.     joblib.dump(sclr,fName)
  54.     print(fName,'saved')
  55.    
  56. def save_x_sclr(sclr,fName):
  57.     with open(fName,'w') as f:
  58.         f.write(str(sclr))
  59.     print(fName,'saved')  
  60.  
  61.  
  62.  
  63. if __name__ == '__main__':
  64.    
  65.      # tensorboard = TensorBoard(log_dir = "logs/{}".format(time()))
  66.  
  67.     x_trn,x_val,y_trn,y_val = train_test_split(Cp_inputs, X_all, test_size=0.2,shuffle=True,random_state=0)
  68.     original_dim = x_trn.shape[1]
  69.     x_trn = np.reshape(x_trn, [-1, original_dim])
  70.     x_val = np.reshape(x_val, [-1, original_dim])
  71. #     x_trn = x_trn.astype('float32')
  72. #     x_val = x_val.astype('float32')
  73.  
  74.     input_shape = (original_dim, )
  75.     inputs = Input(shape=input_shape, name='encoder_input')
  76.     # Define Intermediate Layer Dimension and Latent layer Dimension
  77.     intermediate_dim_1 = 128
  78.     intermediate_dim_2 = 256
  79.     intermediate_dim_3 = 128
  80.     intermediate_dim_4 = 64
  81.     latent_dim = 3
  82.     # Define batch_size / epochs
  83.     epochs = 2000
  84.     batch_size = 128
  85.    
  86.     encoder, z_mean_encoded, z_log_var_encoded = encoder_model(inputs)
  87.  
  88.     # use reparameterization trick to push the sampling out as input
  89.     # note that "output_shape" isn't necessary with the TensorFlow backend
  90.     z_sampled = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean_encoded, z_log_var_encoded]) # Reparameterization Trick
  91.    
  92.     decoder = decoder_model()
  93.     # instantiate VAE model
  94.     outputs = decoder(z_sampled) # z_sampled = sampled z from [z_mean_encoded and z_log_var_encoded]
  95.     vae = Model(inputs, outputs, name='vae_mlp')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement