Music Generation LSTM not learning (Keras)
$begingroup$
I am trying to train a RNN in keras to produce music but I am having difficulty training it. The loss seems to remain fairly high and constant despite me changing the hidden size and number of layers and when it does sometimes train it often outputs the same note for every input, outputting a song of just one note repeated. Is this a problem with my data or how can I fix my model? The notes are extracted from the midi file and then one hot encoded.
def read_midi(path,trans='C'):
songs = glob(path)
notes =
offsets =
durations =
for file in songs:
print(file)
try:
midi = converter.parse(file)
except:
continue
parts = instrument.partitionByInstrument(midi)
if parts is not None:
notes_to_parse = parts.parts[0].recurse()
else:
notes_to_parse = midi.flat.notes
for element in notes_to_parse:
if isinstance(element,note.Note):
notes.append(str(element.pitch))
offsets.append(element.offset)
durations.append(element.quarterLength)
elif isinstance(element,chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
offsets.append(element.offset)
durations.append(element.quarterLength)
return notes,offsets,durations
In this case I am just using the pitch just to see if I can get the RNN to train but I also planned to use the duration and change in offset as another feature by one-hot encoding combinations of the 3. The sequences are generated as follows
pitchnames = sorted(list(set(data)))
seq_length = 100
classes = len(pitchnames)
num_class = {i:ch for i,ch in enumerate(pitchnames)}
class_num = {ch:i for i,ch in enumerate(pitchnames)}
input_data = [class_num[x] for x in data]
input_temp =
output_temp=
for i in range(0,len(input_data)-seq_length,1):
input_temp.append(input_data[i:i+seq_length])
output_temp.append(input_data[i+seq_length])
sequences = len(output_temp)
x = np.reshape(input_temp,(sequences,seq_length,1))
x -= int(np.mean(x))
x = x/classes
y = keras.utils.to_categorical(output_temp)
assert classes == y[0].shape[0]
music_model = Sequential()
music_model.add(CuDNNLSTM(512,input_shape=(seq_length,1),return_sequences=True))
music_model.add(Dropout(0.5))
music_model.add(CuDNNLSTM(512))
music_model.add(Dense(256))
music_model.add(Activation('relu'))
music_model.add(Dropout(0.4))
music_model.add(Dense(classes))
music_model.add(Activation('softmax'))
music_model.compile(loss='categorical_crossentropy',optimizer='RMSprop')
filename = './ML/keras_rnn/music_weights.{epoch:02d}-{loss:.2f}.hdf5'
callbacks = [ModelCheckpoint(filename,monitor='loss',save_best_only=True)]
music_model.fit(x,y,callbacks = callbacks,epochs=1000,batch_size=64)
The loss seems to stop decreasing at around 5.03 and doesn't decrease further over the epochs. Any help would be greatly appreciated
python keras rnn
$endgroup$
add a comment |
$begingroup$
I am trying to train a RNN in keras to produce music but I am having difficulty training it. The loss seems to remain fairly high and constant despite me changing the hidden size and number of layers and when it does sometimes train it often outputs the same note for every input, outputting a song of just one note repeated. Is this a problem with my data or how can I fix my model? The notes are extracted from the midi file and then one hot encoded.
def read_midi(path,trans='C'):
songs = glob(path)
notes =
offsets =
durations =
for file in songs:
print(file)
try:
midi = converter.parse(file)
except:
continue
parts = instrument.partitionByInstrument(midi)
if parts is not None:
notes_to_parse = parts.parts[0].recurse()
else:
notes_to_parse = midi.flat.notes
for element in notes_to_parse:
if isinstance(element,note.Note):
notes.append(str(element.pitch))
offsets.append(element.offset)
durations.append(element.quarterLength)
elif isinstance(element,chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
offsets.append(element.offset)
durations.append(element.quarterLength)
return notes,offsets,durations
In this case I am just using the pitch just to see if I can get the RNN to train but I also planned to use the duration and change in offset as another feature by one-hot encoding combinations of the 3. The sequences are generated as follows
pitchnames = sorted(list(set(data)))
seq_length = 100
classes = len(pitchnames)
num_class = {i:ch for i,ch in enumerate(pitchnames)}
class_num = {ch:i for i,ch in enumerate(pitchnames)}
input_data = [class_num[x] for x in data]
input_temp =
output_temp=
for i in range(0,len(input_data)-seq_length,1):
input_temp.append(input_data[i:i+seq_length])
output_temp.append(input_data[i+seq_length])
sequences = len(output_temp)
x = np.reshape(input_temp,(sequences,seq_length,1))
x -= int(np.mean(x))
x = x/classes
y = keras.utils.to_categorical(output_temp)
assert classes == y[0].shape[0]
music_model = Sequential()
music_model.add(CuDNNLSTM(512,input_shape=(seq_length,1),return_sequences=True))
music_model.add(Dropout(0.5))
music_model.add(CuDNNLSTM(512))
music_model.add(Dense(256))
music_model.add(Activation('relu'))
music_model.add(Dropout(0.4))
music_model.add(Dense(classes))
music_model.add(Activation('softmax'))
music_model.compile(loss='categorical_crossentropy',optimizer='RMSprop')
filename = './ML/keras_rnn/music_weights.{epoch:02d}-{loss:.2f}.hdf5'
callbacks = [ModelCheckpoint(filename,monitor='loss',save_best_only=True)]
music_model.fit(x,y,callbacks = callbacks,epochs=1000,batch_size=64)
The loss seems to stop decreasing at around 5.03 and doesn't decrease further over the epochs. Any help would be greatly appreciated
python keras rnn
$endgroup$
add a comment |
$begingroup$
I am trying to train a RNN in keras to produce music but I am having difficulty training it. The loss seems to remain fairly high and constant despite me changing the hidden size and number of layers and when it does sometimes train it often outputs the same note for every input, outputting a song of just one note repeated. Is this a problem with my data or how can I fix my model? The notes are extracted from the midi file and then one hot encoded.
def read_midi(path,trans='C'):
songs = glob(path)
notes =
offsets =
durations =
for file in songs:
print(file)
try:
midi = converter.parse(file)
except:
continue
parts = instrument.partitionByInstrument(midi)
if parts is not None:
notes_to_parse = parts.parts[0].recurse()
else:
notes_to_parse = midi.flat.notes
for element in notes_to_parse:
if isinstance(element,note.Note):
notes.append(str(element.pitch))
offsets.append(element.offset)
durations.append(element.quarterLength)
elif isinstance(element,chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
offsets.append(element.offset)
durations.append(element.quarterLength)
return notes,offsets,durations
In this case I am just using the pitch just to see if I can get the RNN to train but I also planned to use the duration and change in offset as another feature by one-hot encoding combinations of the 3. The sequences are generated as follows
pitchnames = sorted(list(set(data)))
seq_length = 100
classes = len(pitchnames)
num_class = {i:ch for i,ch in enumerate(pitchnames)}
class_num = {ch:i for i,ch in enumerate(pitchnames)}
input_data = [class_num[x] for x in data]
input_temp =
output_temp=
for i in range(0,len(input_data)-seq_length,1):
input_temp.append(input_data[i:i+seq_length])
output_temp.append(input_data[i+seq_length])
sequences = len(output_temp)
x = np.reshape(input_temp,(sequences,seq_length,1))
x -= int(np.mean(x))
x = x/classes
y = keras.utils.to_categorical(output_temp)
assert classes == y[0].shape[0]
music_model = Sequential()
music_model.add(CuDNNLSTM(512,input_shape=(seq_length,1),return_sequences=True))
music_model.add(Dropout(0.5))
music_model.add(CuDNNLSTM(512))
music_model.add(Dense(256))
music_model.add(Activation('relu'))
music_model.add(Dropout(0.4))
music_model.add(Dense(classes))
music_model.add(Activation('softmax'))
music_model.compile(loss='categorical_crossentropy',optimizer='RMSprop')
filename = './ML/keras_rnn/music_weights.{epoch:02d}-{loss:.2f}.hdf5'
callbacks = [ModelCheckpoint(filename,monitor='loss',save_best_only=True)]
music_model.fit(x,y,callbacks = callbacks,epochs=1000,batch_size=64)
The loss seems to stop decreasing at around 5.03 and doesn't decrease further over the epochs. Any help would be greatly appreciated
python keras rnn
$endgroup$
I am trying to train a RNN in keras to produce music but I am having difficulty training it. The loss seems to remain fairly high and constant despite me changing the hidden size and number of layers and when it does sometimes train it often outputs the same note for every input, outputting a song of just one note repeated. Is this a problem with my data or how can I fix my model? The notes are extracted from the midi file and then one hot encoded.
def read_midi(path,trans='C'):
songs = glob(path)
notes =
offsets =
durations =
for file in songs:
print(file)
try:
midi = converter.parse(file)
except:
continue
parts = instrument.partitionByInstrument(midi)
if parts is not None:
notes_to_parse = parts.parts[0].recurse()
else:
notes_to_parse = midi.flat.notes
for element in notes_to_parse:
if isinstance(element,note.Note):
notes.append(str(element.pitch))
offsets.append(element.offset)
durations.append(element.quarterLength)
elif isinstance(element,chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
offsets.append(element.offset)
durations.append(element.quarterLength)
return notes,offsets,durations
In this case I am just using the pitch just to see if I can get the RNN to train but I also planned to use the duration and change in offset as another feature by one-hot encoding combinations of the 3. The sequences are generated as follows
pitchnames = sorted(list(set(data)))
seq_length = 100
classes = len(pitchnames)
num_class = {i:ch for i,ch in enumerate(pitchnames)}
class_num = {ch:i for i,ch in enumerate(pitchnames)}
input_data = [class_num[x] for x in data]
input_temp =
output_temp=
for i in range(0,len(input_data)-seq_length,1):
input_temp.append(input_data[i:i+seq_length])
output_temp.append(input_data[i+seq_length])
sequences = len(output_temp)
x = np.reshape(input_temp,(sequences,seq_length,1))
x -= int(np.mean(x))
x = x/classes
y = keras.utils.to_categorical(output_temp)
assert classes == y[0].shape[0]
music_model = Sequential()
music_model.add(CuDNNLSTM(512,input_shape=(seq_length,1),return_sequences=True))
music_model.add(Dropout(0.5))
music_model.add(CuDNNLSTM(512))
music_model.add(Dense(256))
music_model.add(Activation('relu'))
music_model.add(Dropout(0.4))
music_model.add(Dense(classes))
music_model.add(Activation('softmax'))
music_model.compile(loss='categorical_crossentropy',optimizer='RMSprop')
filename = './ML/keras_rnn/music_weights.{epoch:02d}-{loss:.2f}.hdf5'
callbacks = [ModelCheckpoint(filename,monitor='loss',save_best_only=True)]
music_model.fit(x,y,callbacks = callbacks,epochs=1000,batch_size=64)
The loss seems to stop decreasing at around 5.03 and doesn't decrease further over the epochs. Any help would be greatly appreciated
python keras rnn
python keras rnn
asked yesterday
treutmtreutm
225
225
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f46422%2fmusic-generation-lstm-not-learning-keras%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f46422%2fmusic-generation-lstm-not-learning-keras%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown