Created
December 20, 2018 17:12
-
-
Save loretoparisi/58baf95fa9832d90b1cbc35540e6c185 to your computer and use it in GitHub Desktop.
Keras Listener Network LSTM Autoencoder - https://towardsdatascience.com/human-like-machine-hearing-with-ai-2-3-f9fab903b20a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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