Created
November 20, 2019 03:44
-
-
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
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 | |
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