Created
November 20, 2019 03:40
-
-
Save jasonsalas/793a451a2c0a4f7fa6766bc29c4c0544 to your computer and use it in GitHub Desktop.
Generate Depeche Mode lyrics automatically with an LSTM neural network
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
import numpy as np | |
import sys | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, LSTM | |
from keras.callbacks import ModelCheckpoint | |
from keras.utils import to_categorical | |
filename = 'dm_lyrics.txt' | |
raw_text = open(filename, 'r', encoding='utf-8').read() | |
raw_text = raw_text.lower() | |
# character-to-integer mapping | |
chars = sorted(list(set(raw_text))) | |
char_to_int = dict((c, i) for i, c in enumerate(chars)) | |
n_chars = len(raw_text) | |
n_vocab = len(chars) | |
print('total characters: ', n_chars) | |
print('total vocabulary: ', n_vocab) | |
# prepare the dataset of input to output pairs encoded as integers | |
seq_length = 100 | |
dataX = [] | |
dataY = [] | |
for i in range(0, n_chars - seq_length, 1): | |
seq_in = raw_text[i:i+seq_length] | |
seq_out = raw_text[i+seq_length] | |
dataX.append([char_to_int[char] for char in seq_in]) | |
dataY.append(char_to_int[seq_out]) | |
n_patterns = len(dataX) | |
print('total patterns: ', n_patterns) | |
''' | |
sequence input format expected by the LSTM-based neural network: | |
[samples, timesteps, features] | |
''' | |
# reshape X to the expected input format | |
X = np.reshape(dataX, (n_patterns, seq_length, 1)) | |
# normalization | |
X = X / float(n_vocab) | |
# one-hot encoding of the output | |
y = to_categorical(dataY) | |
print(X.shape, y.shape) | |
# define the LSTM model | |
model = Sequential() | |
model.add(LSTM(256, input_shape=(X.shape[1], X.shape[2]), return_sequences=True)) | |
model.add(Dropout(0.2)) | |
model.add(LSTM(256)) | |
model.add(Dropout(0.2)) | |
model.add(Dense(y.shape[1], activation='softmax')) | |
model.compile(loss='categorical_crossentropy', optimizer='adam') | |
model.summary() | |
filepath = 'weights_improvement_{epoch:02d}_{loss:.4f}.h5' | |
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min') | |
model.fit(X, y, epochs=3, batch_size=128, callbacks=[checkpoint]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment