Advertisement
mario119

AutoEncoder_v1

Feb 27th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. # build encoder model
  2. def encoder_model(inputs):
  3.     x1 = Dense(intermediate_dim_1, activation='relu')(inputs)
  4.     x2 = Dense(intermediate_dim_2, activation='relu')(x1)  
  5.     x3 = Dense(intermediate_dim_3, activation='relu')(x2)    
  6.     encoded = Dense(latent_dim)(x3)    
  7.     encoder = Model(inputs, encoded, name='encoder')
  8.     return encoder
  9.  
  10. # build decoder model
  11. def decoder_model():
  12.     latent_inputs = Input(shape=(latent_dim,), name='z_space')
  13.     x3 = Dense(intermediate_dim_3, activation='relu')(latent_inputs)
  14.     x2 = Dense(intermediate_dim_2, activation='relu')(x3)
  15.     x1 = Dense(intermediate_dim_1, activation='relu')(x2)
  16.     decoded = Dense(original_dim)(x1)
  17.  
  18.     # instantiate decoder model
  19.     decoder = Model(latent_inputs, decoded, name='decoder')
  20.     return decoder
  21.  
  22.  
  23.  
  24. if __name__ == '__main__':
  25.    
  26.  
  27.     x_trn,x_val,y_trn,y_val = train_test_split(Cp_inputs, X_all, test_size=0.2,shuffle=True,random_state=0)
  28.     original_dim = x_trn.shape[1]
  29.     x_trn = np.reshape(x_trn, [-1, original_dim])
  30.     x_val = np.reshape(x_val, [-1, original_dim])
  31.  
  32.  
  33.     input_shape = (original_dim, )
  34.     inputs = Input(shape=input_shape, name='encoder_input')
  35.  
  36.     # Define Intermediate Layer Dimension and Latent layer Dimension
  37.     intermediate_dim_1 = 256
  38.     intermediate_dim_2 = 128
  39.     intermediate_dim_3 = 64
  40.     latent_dim = 3
  41.    
  42.     # Define batch_size / epochs
  43.     epochs = 200
  44.     batch_size = 128
  45.    
  46.     encoder = encoder_model(inputs)
  47.     decoder = decoder_model()
  48.     # instantiate VAE model
  49.     outputs = decoder(encoder(inputs))
  50.     ae = Model(inputs, outputs, name='ae_mlp')
  51.  
  52.     reconstruction_loss = mse(inputs, outputs)
  53.    
  54. #     reconstruction_loss = binary_crossentropy(inputs, outputs)
  55.     ae.add_loss(reconstruction_loss)
  56.     opt = tf.keras.optimizers.Adam(lr=0.001)
  57.     ae.compile(optimizer=opt, loss='mse')
  58.     history = ae.fit(x_trn, x_trn, epochs=epochs, batch_size=batch_size, validation_data=(x_val, x_val))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement