Skip to content

Instantly share code, notes, and snippets.

@jasonsalas
Created November 20, 2019 03:44
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 jasonsalas/3796be8a2c26727524be4012580c812d to your computer and use it in GitHub Desktop.
Save jasonsalas/3796be8a2c26727524be4012580c812d to your computer and use it in GitHub Desktop.
Using a trained LSTM-based neural network model to generate Depeche Mode-style lyrics
import numpy as np
import sys
from keras.models import Sequential
filename = 'trained_depechemode_lyrics_model.h5'
model = Sequential()
model.load_weights(filename)
model.compile(loss='categorical_crossentropy', optimizer='adam')
int_to_char = dict((i, c) for i, c in enumerate(chars))
''' make predictions '''
# pick a random seed value to start
# with a single character
start = np.random.randint(0, len(dataX)-1)
pattern = dataX[start] # see https://gist.github.com/jasonsalas/793a451a2c0a4f7fa6766bc29c4c0544 for this variable
print('seed: ')
print("\"", ''.join([int_to_char[value] for value in pattern]), "\"")
# generate characters
# characters form words
# words form sentences
# sentences form stanzas
# stanzas form verses and choruses
print('\n\n\n****[START SYNTHETIC LYRICS]****\n\n\n')
for i in range(250):
x = np.reshape(pattern, (1, len(pattern), 1))
x = x / float(n_vocab)
prediction = model.predict(x, verbose=0)
index = np.argmax(prediction)
result = int_to_char[index]
seq_in = [int_to_char[value] for value in pattern]
sys.stdout.write(result)
pattern.append(index)
pattern = pattern[1:len(pattern)]
print('\n\n\n****[FIN]****')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment