Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Created December 20, 2018 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loretoparisi/58baf95fa9832d90b1cbc35540e6c185 to your computer and use it in GitHub Desktop.
Save loretoparisi/58baf95fa9832d90b1cbc35540e6c185 to your computer and use it in GitHub Desktop.
from keras.models import Model
from keras.layers import Input, LSTM, RepeatVector
def prepare_listener(timesteps,
input_dim,
latent_dim,
optimizer_type,
loss_type):
"""Prepares Seq2Seq autoencoder model
Args:
:param timesteps: The number of timesteps in sequence
:param input_dim: The dimensions of the input
:param latent_dim: The latent dimensionality of LSTM
:param optimizer_type: The type of optimizer to use
:param loss_type: The type of loss to use
Returns:
Autoencoder model, Encoder model
"""
inputs = Input(shape=(timesteps, input_dim))
encoded = LSTM(int(input_dim / 2),
activation="relu",
return_sequences=True)(inputs)
encoded = LSTM(latent_dim,
activation="relu",
return_sequences=False)(encoded)
decoded = RepeatVector(timesteps)(encoded)
decoded = LSTM(int(input_dim / 2),
activation="relu",
return_sequences=True)(decoded)
decoded = LSTM(input_dim,
return_sequences=True)(decoded)
autoencoder = Model(inputs, decoded)
encoder = Model(inputs, encoded)
autoencoder.compile(optimizer=optimizer_type,
loss=loss_type,
metrics=['acc'])
return autoencoder, encoder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment