09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Autoencoders

x_train_noisy = x_train + noise

noise = np.random.normal(loc=0.5, scale=0.5, size=x_test.shape)

x_test_noisy = x_test + noise

4. We use the same encoder, decoder, and autoencoder classes as defined in the

Vanilla autoencoders section:

# Encoder

class Encoder(K.layers.Layer):

def __init__(self, hidden_dim):

super(Encoder, self).__init__()

self.hidden_layer = K.layers.Dense(units=hidden_dim,

activation=tf.nn.relu)

def call(self, input_features):

activation = self.hidden_layer(input_features)

return activation

# Decoder

class Decoder(K.layers.Layer):

def __init__(self, hidden_dim, original_dim):

super(Decoder, self).__init__()

self.output_layer = K.layers.Dense(units=original_dim,

activation=tf.nn.relu)

def call(self, encoded):

activation = self.output_layer(encoded)

return activation

class Autoencoder(K.Model):

def __init__(self, hidden_dim, original_dim):

super(Autoencoder, self).__init__()

self.loss = []

self.encoder = Encoder(hidden_dim=hidden_dim)

self.decoder = Decoder(hidden_dim=hidden_dim, original_

dim=original_dim)

def call(self, input_features):

encoded = self.encoder(input_features)

reconstructed = self.decoder(encoded)

return reconstructed

5. Next we create the model and define the loss and optimizers to be used.

Notice that this time instead of writing the custom training loop we are

using the easier Keras inbuilt compile() and fit() methods:

model = Autoencoder(hidden_dim=hidden_dim, original_dim=original_

dim)

model.compile(loss='mse', optimizer='adam')

loss = model.fit(x_train_noisy,

[ 358 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!