N.N. Bach

Generating Music in the Syle of J.S. Bach Using LSTM Nets


Introduction

This notebook attempts to generate monophonic music in the style of Johann Sebastian Bach. It will do so by utilizing Long short-term memory (LSTM) RNN's.

The input of this network will be a single MIDI file, and the output will be a new MIDI file, generated by the network, in a style similar to the input piece.

Below is a brief overview of this process:

Once the MIDI is generated, it is converted to a wav file via wildmidi.

Imports

In this notebook music21, an open-source music library written by MIT will be used to read and create MIDI files. Keras will be used to process the MIDI data and generate new pieces of music. Additionaly matplotlib will be used to plot various graphs.

Below these elements are imported:

In [3]:
import os
from music21 import converter, corpus, instrument, midi, note, chord, pitch, stream
import numpy as np
import keras
from keras import models
from keras import layers
from keras.utils.np_utils import to_categorical 
from matplotlib import pyplot
import IPython.display as ipd
Using TensorFlow backend.

Load the data

Firstly, a MIDI file is read, converting the contents to a music21 "stream" object. This converts the binary data of the MIDI file into an easy readable format. Due to a lack of computational resources, only one piece at a time will be used in this notebook.

For this notebook, Bach's Contrapunctus I from The Art of Fugue will be used:

In [2]:
def readMidi(filename):
    mf = midi.MidiFile()
    mf.open(filename)
    mf.read()
    mf.close()
    return midi.translate.midiFileToStream(mf)

midiTrack = readMidi("bach.mid")

Visualise the piece

The extracted MIDI data consists of a stream of note data, each containing multiple "parts". A part represents a single monophonic pattern of notes. Below frequencies of each note of each stream are extracted, and plotted in a visualisation. We begin by extracting the freqency data from the piece:

In [3]:
def createFreqArray(track):
    returnArray = []
    for i in range(0, len(track.parts)):
        tmpArray = []
        section = track.parts[i].flat.notes  
        for thisNote in section:
            if isinstance(thisNote, note.Note):
                pitch = str(thisNote.pitch.frequency)
                tmpArray.append(pitch)
        returnArray.append(tmpArray)
    return returnArray

freqPattern = createFreqArray(midiTrack)

Then proceed to plot these frequencies onto a graph:

In [4]:
def visualiseFreqArray(array):
    for i in range(len(array)):
        x = range(len(array[i]))
        y = array[i]
        pyplot.scatter(x, y, alpha=0.5)
    pyplot.title("Visualisation of piece")
    pyplot.ylabel("Frequency (hz)")
    pyplot.xlabel("Step")
    pyplot.show()
    
visualiseFreqArray(freqPattern)

Create note and note duration arrays

Sequences of note names and the duration of each note are extracted into arrays in order to further examine these parameters. In order to obtain every note in the piece of music, a function is needed that loops through each part, then, loops through each note of the part. Since we are only going to be generating monophonic music, each part's notes is concatenated to a long array, read to be processed.

Two functions are created, one for extracting each feature (Note and duration) from the MIDI stream:

In [5]:
# For extracting notes
def createNoteArray(track):
    returnArray = []
    for i in range(0, len(track.parts)):
        section = track.parts[i].flat.notes  
        for thisNote in section:
            if isinstance(thisNote, note.Note):
                pitch = str(thisNote.pitch)
                returnArray.append(pitch)
    return returnArray

# For extracting durataion data
def createDurationArray(track):
    returnArray = []
    for i in range(len(track.parts)):
        section = track.parts[i].flat.notes  
        for thisNote in section:
            if isinstance(thisNote, note.Note):
                dur = str(thisNote.duration.quarterLength)
                returnArray.append(dur)
    return returnArray

notePattern = createNoteArray(midiTrack)
durationPattern = createDurationArray(midiTrack)

One hot encode the data

In order to feed the data into the network it needs to be encoded. This is done via one-hot encoding, for both the note and duration data:

In [6]:
noteDict = {}
durationDict = {}
encodedNote = []
encodedDuration = []

def encodeNote(data, dict):
    returnCode = 0
    if data not in dict:
        returnCode = len(dict)
        dict[data] = returnCode
    else:
        returnCode = dict[data]
    return returnCode

# Encode note data
for rawNote in notePattern:
    encodedNote.append(encodeNote(rawNote, noteDict))

# Encode duration data
for rawDur in durationPattern:
    encodedDuration.append(encodeNote(rawDur, durationDict))

Split the data

In order to train the network a dataset must be created from the encoded data. This dataset will consist of two parts: The training data and the training labels.

In order to create the training data patterns of note and duration of $n$ length are taken from the encoded data, with a stride of one each time. The label data is simply the note or duration which directly follows the pattern. This can be seen below:

In [7]:
def createData(encodedData, dictLen):
    sequenceLen = dictLen
    x = []
    y = []
    for i in range(0, len(encodedData) - sequenceLen):
        thisX = encodedData[i:i + sequenceLen]
        x.append(thisX)
        thisY = encodedData[i + sequenceLen]
        y.append(thisY)
    x = np.reshape(x, (len(x), sequenceLen, 1))
    y = to_categorical(y)
    return x, y


xNote, yNote = createData(encodedNote, len(noteDict))
xDuration, yDuration = createData(encodedDuration, len(durationDict))

Create the models

Two models need to be created. Multiclass classification will be performed on two sets of data: Note values, and note durations. As both of these are sequence data which rely heavily on previous values, a LSTM layer is used to first process the data. Next, the output will be fed into a dense layer which outputs a prediction of which catagory the pattern input should corrispond with. Since this is multiclass classifcation the catagorical crossentropy loss function shall be used:

In [8]:
# Model for notes
noteModel = keras.models.Sequential()
noteModel.add(layers.LSTM(128, input_shape=(xNote.shape[1], xNote.shape[2])))
noteModel.add(layers.Dense(len(yNote[0]), activation='softmax'))
noteModel.compile(loss='categorical_crossentropy', optimizer='rmsprop')

# Model for note duration
durationModel = keras.models.Sequential()
durationModel.add(layers.LSTM(128, input_shape=(xDuration.shape[1], xDuration.shape[2])))
durationModel.add(layers.Dense(len(yDuration[0]), activation='softmax'))
durationModel.compile(loss='categorical_crossentropy', optimizer='rmsprop')

Train

The two models need to be trained. They are both trained for 250 epochs and their history is recorded.

In [9]:
print("==Training note model==")
noteModelHistory = noteModel.fit(xNote, yNote, epochs=250)
print("==Training duration model==")
durationModelHistory = durationModel.fit(xDuration, yDuration, epochs=250)
==Training note model==
Epoch 1/250
858/858 [==============================] - 5s 6ms/step - loss: 3.2931
Epoch 2/250
858/858 [==============================] - 4s 4ms/step - loss: 2.9366
Epoch 3/250
858/858 [==============================] - 4s 4ms/step - loss: 2.8253
Epoch 4/250
858/858 [==============================] - 4s 4ms/step - loss: 2.7674
Epoch 5/250
858/858 [==============================] - 4s 4ms/step - loss: 2.6963
Epoch 6/250
858/858 [==============================] - 4s 4ms/step - loss: 2.6589
Epoch 7/250
858/858 [==============================] - 4s 4ms/step - loss: 2.6276
Epoch 8/250
858/858 [==============================] - 4s 4ms/step - loss: 2.6295
Epoch 9/250
858/858 [==============================] - 4s 4ms/step - loss: 2.5885
Epoch 10/250
858/858 [==============================] - 4s 4ms/step - loss: 2.5634
Epoch 11/250
858/858 [==============================] - 4s 4ms/step - loss: 2.5522
Epoch 12/250
858/858 [==============================] - 4s 4ms/step - loss: 2.5287
Epoch 13/250
858/858 [==============================] - 4s 4ms/step - loss: 2.5017
Epoch 14/250
858/858 [==============================] - 4s 4ms/step - loss: 2.4847
Epoch 15/250
858/858 [==============================] - 4s 4ms/step - loss: 2.4750
Epoch 16/250
858/858 [==============================] - 4s 4ms/step - loss: 2.4677
Epoch 17/250
858/858 [==============================] - 4s 4ms/step - loss: 2.4392
Epoch 18/250
858/858 [==============================] - 4s 4ms/step - loss: 2.4269
Epoch 19/250
858/858 [==============================] - 4s 4ms/step - loss: 2.3983
Epoch 20/250
858/858 [==============================] - 4s 4ms/step - loss: 2.3951
Epoch 21/250
858/858 [==============================] - 4s 4ms/step - loss: 2.3655
Epoch 22/250
858/858 [==============================] - 3s 4ms/step - loss: 2.3472
Epoch 23/250
858/858 [==============================] - 4s 4ms/step - loss: 2.3401
Epoch 24/250
858/858 [==============================] - 4s 4ms/step - loss: 2.3443
Epoch 25/250
858/858 [==============================] - 4s 4ms/step - loss: 2.3094
Epoch 26/250
858/858 [==============================] - 4s 4ms/step - loss: 2.3051
Epoch 27/250
858/858 [==============================] - 4s 4ms/step - loss: 2.2934
Epoch 28/250
858/858 [==============================] - 4s 4ms/step - loss: 2.2721
Epoch 29/250
858/858 [==============================] - 4s 4ms/step - loss: 2.2658
Epoch 30/250
858/858 [==============================] - 4s 4ms/step - loss: 2.2429
Epoch 31/250
858/858 [==============================] - 4s 4ms/step - loss: 2.2484
Epoch 32/250
858/858 [==============================] - 4s 4ms/step - loss: 2.2163
Epoch 33/250
858/858 [==============================] - 4s 4ms/step - loss: 2.2129
Epoch 34/250
858/858 [==============================] - 4s 4ms/step - loss: 2.1878
Epoch 35/250
858/858 [==============================] - 4s 4ms/step - loss: 2.1969
Epoch 36/250
858/858 [==============================] - 4s 4ms/step - loss: 2.1804
Epoch 37/250
858/858 [==============================] - 4s 4ms/step - loss: 2.1696
Epoch 38/250
858/858 [==============================] - 4s 4ms/step - loss: 2.1603
Epoch 39/250
858/858 [==============================] - 4s 4ms/step - loss: 2.1391
Epoch 40/250
858/858 [==============================] - 4s 4ms/step - loss: 2.1348
Epoch 41/250
858/858 [==============================] - 4s 4ms/step - loss: 2.1208
Epoch 42/250
858/858 [==============================] - 4s 4ms/step - loss: 2.1010
Epoch 43/250
858/858 [==============================] - 4s 4ms/step - loss: 2.0978
Epoch 44/250
858/858 [==============================] - 4s 4ms/step - loss: 2.0674
Epoch 45/250
858/858 [==============================] - 4s 4ms/step - loss: 2.0749
Epoch 46/250
858/858 [==============================] - 4s 4ms/step - loss: 2.0567
Epoch 47/250
858/858 [==============================] - 4s 4ms/step - loss: 2.0360
Epoch 48/250
858/858 [==============================] - 4s 4ms/step - loss: 2.0211
Epoch 49/250
858/858 [==============================] - 4s 4ms/step - loss: 2.0099
Epoch 50/250
858/858 [==============================] - 4s 4ms/step - loss: 2.0144
Epoch 51/250
858/858 [==============================] - 4s 4ms/step - loss: 1.9904
Epoch 52/250
858/858 [==============================] - 4s 4ms/step - loss: 1.9710
Epoch 53/250
858/858 [==============================] - 4s 4ms/step - loss: 1.9611
Epoch 54/250
858/858 [==============================] - 4s 4ms/step - loss: 1.9713
Epoch 55/250
858/858 [==============================] - 4s 4ms/step - loss: 1.9537
Epoch 56/250
858/858 [==============================] - 4s 4ms/step - loss: 1.9217
Epoch 57/250
858/858 [==============================] - 4s 4ms/step - loss: 1.9180
Epoch 58/250
858/858 [==============================] - 4s 4ms/step - loss: 1.9073
Epoch 59/250
858/858 [==============================] - 4s 4ms/step - loss: 1.8866
Epoch 60/250
858/858 [==============================] - 4s 4ms/step - loss: 1.8890
Epoch 61/250
858/858 [==============================] - 4s 4ms/step - loss: 1.8737
Epoch 62/250
858/858 [==============================] - 4s 4ms/step - loss: 1.8592
Epoch 63/250
858/858 [==============================] - 4s 4ms/step - loss: 1.8577
Epoch 64/250
858/858 [==============================] - 4s 4ms/step - loss: 1.8213
Epoch 65/250
858/858 [==============================] - 4s 4ms/step - loss: 1.8152
Epoch 66/250
858/858 [==============================] - 4s 4ms/step - loss: 1.8136
Epoch 67/250
858/858 [==============================] - 4s 4ms/step - loss: 1.8014
Epoch 68/250
858/858 [==============================] - 4s 4ms/step - loss: 1.7838
Epoch 69/250
858/858 [==============================] - 4s 4ms/step - loss: 1.7794
Epoch 70/250
858/858 [==============================] - 4s 4ms/step - loss: 1.7625
Epoch 71/250
858/858 [==============================] - 4s 4ms/step - loss: 1.7435
Epoch 72/250
858/858 [==============================] - 4s 4ms/step - loss: 1.7372
Epoch 73/250
858/858 [==============================] - 4s 4ms/step - loss: 1.7306
Epoch 74/250
858/858 [==============================] - 4s 4ms/step - loss: 1.7120
Epoch 75/250
858/858 [==============================] - 4s 4ms/step - loss: 1.7039
Epoch 76/250
858/858 [==============================] - 4s 4ms/step - loss: 1.7069
Epoch 77/250
858/858 [==============================] - 4s 4ms/step - loss: 1.6718
Epoch 78/250
858/858 [==============================] - 4s 4ms/step - loss: 1.6751
Epoch 79/250
858/858 [==============================] - 4s 4ms/step - loss: 1.6558
Epoch 80/250
858/858 [==============================] - 4s 4ms/step - loss: 1.6354
Epoch 81/250
858/858 [==============================] - 4s 4ms/step - loss: 1.6255
Epoch 82/250
858/858 [==============================] - 4s 4ms/step - loss: 1.6169
Epoch 83/250
858/858 [==============================] - 4s 4ms/step - loss: 1.6141
Epoch 84/250
858/858 [==============================] - 4s 4ms/step - loss: 1.5964
Epoch 85/250
858/858 [==============================] - 4s 4ms/step - loss: 1.5874
Epoch 86/250
858/858 [==============================] - 4s 4ms/step - loss: 1.5757
Epoch 87/250
858/858 [==============================] - 4s 4ms/step - loss: 1.5899
Epoch 88/250
858/858 [==============================] - 4s 4ms/step - loss: 1.5289
Epoch 89/250
858/858 [==============================] - 4s 4ms/step - loss: 1.5676
Epoch 90/250
858/858 [==============================] - 4s 4ms/step - loss: 1.5236
Epoch 91/250
858/858 [==============================] - 4s 4ms/step - loss: 1.5063
Epoch 92/250
858/858 [==============================] - 4s 4ms/step - loss: 1.5100
Epoch 93/250
858/858 [==============================] - 4s 4ms/step - loss: 1.4959
Epoch 94/250
858/858 [==============================] - 4s 4ms/step - loss: 1.4756
Epoch 95/250
858/858 [==============================] - 4s 4ms/step - loss: 1.4837
Epoch 96/250
858/858 [==============================] - 4s 4ms/step - loss: 1.4556
Epoch 97/250
858/858 [==============================] - 4s 4ms/step - loss: 1.4580
Epoch 98/250
858/858 [==============================] - 4s 4ms/step - loss: 1.4638
Epoch 99/250
858/858 [==============================] - 3s 4ms/step - loss: 1.4258
Epoch 100/250
858/858 [==============================] - 3s 4ms/step - loss: 1.4297
Epoch 101/250
858/858 [==============================] - 3s 4ms/step - loss: 1.4148
Epoch 102/250
858/858 [==============================] - 4s 4ms/step - loss: 1.4204
Epoch 103/250
858/858 [==============================] - 3s 4ms/step - loss: 1.3828
Epoch 104/250
858/858 [==============================] - 3s 4ms/step - loss: 1.3720
Epoch 105/250
858/858 [==============================] - 3s 4ms/step - loss: 1.3853
Epoch 106/250
858/858 [==============================] - 4s 4ms/step - loss: 1.3560
Epoch 107/250
858/858 [==============================] - 3s 4ms/step - loss: 1.3277
Epoch 108/250
858/858 [==============================] - 3s 4ms/step - loss: 1.3226
Epoch 109/250
858/858 [==============================] - 3s 4ms/step - loss: 1.3156
Epoch 110/250
858/858 [==============================] - 3s 4ms/step - loss: 1.3000
Epoch 111/250
858/858 [==============================] - 3s 4ms/step - loss: 1.2922
Epoch 112/250
858/858 [==============================] - 3s 4ms/step - loss: 1.2750
Epoch 113/250
858/858 [==============================] - 3s 4ms/step - loss: 1.2724
Epoch 114/250
858/858 [==============================] - 3s 4ms/step - loss: 1.2794
Epoch 115/250
858/858 [==============================] - 3s 4ms/step - loss: 1.2391
Epoch 116/250
858/858 [==============================] - 3s 4ms/step - loss: 1.2494
Epoch 117/250
858/858 [==============================] - 3s 4ms/step - loss: 1.2282
Epoch 118/250
858/858 [==============================] - 3s 4ms/step - loss: 1.2225
Epoch 119/250
858/858 [==============================] - 3s 4ms/step - loss: 1.2011
Epoch 120/250
858/858 [==============================] - 3s 4ms/step - loss: 1.1939
Epoch 121/250
858/858 [==============================] - 4s 4ms/step - loss: 1.2029
Epoch 122/250
858/858 [==============================] - 3s 4ms/step - loss: 1.1826
Epoch 123/250
858/858 [==============================] - 3s 4ms/step - loss: 1.1784
Epoch 124/250
858/858 [==============================] - 3s 4ms/step - loss: 1.1678
Epoch 125/250
858/858 [==============================] - 3s 4ms/step - loss: 1.1436
Epoch 126/250
858/858 [==============================] - 4s 4ms/step - loss: 1.1421
Epoch 127/250
858/858 [==============================] - 3s 4ms/step - loss: 1.1219
Epoch 128/250
858/858 [==============================] - 3s 4ms/step - loss: 1.1229
Epoch 129/250
858/858 [==============================] - 3s 4ms/step - loss: 1.1114
Epoch 130/250
858/858 [==============================] - 3s 4ms/step - loss: 1.1074
Epoch 131/250
858/858 [==============================] - 3s 4ms/step - loss: 1.0810
Epoch 132/250
858/858 [==============================] - 3s 4ms/step - loss: 1.0691
Epoch 133/250
858/858 [==============================] - 3s 4ms/step - loss: 1.0685
Epoch 134/250
858/858 [==============================] - 3s 4ms/step - loss: 1.0448
Epoch 135/250
858/858 [==============================] - 4s 4ms/step - loss: 1.0716
Epoch 136/250
858/858 [==============================] - 4s 4ms/step - loss: 1.0276
Epoch 137/250
858/858 [==============================] - 3s 4ms/step - loss: 1.0249
Epoch 138/250
858/858 [==============================] - 3s 4ms/step - loss: 1.0528
Epoch 139/250
858/858 [==============================] - 4s 4ms/step - loss: 0.9947
Epoch 140/250
858/858 [==============================] - 3s 4ms/step - loss: 1.0018
Epoch 141/250
858/858 [==============================] - 3s 4ms/step - loss: 1.0025
Epoch 142/250
858/858 [==============================] - 3s 4ms/step - loss: 1.0224
Epoch 143/250
858/858 [==============================] - 3s 4ms/step - loss: 0.9469
Epoch 144/250
858/858 [==============================] - 3s 4ms/step - loss: 0.9827
Epoch 145/250
858/858 [==============================] - 3s 4ms/step - loss: 0.9515
Epoch 146/250
858/858 [==============================] - 3s 4ms/step - loss: 0.9278
Epoch 147/250
858/858 [==============================] - 3s 4ms/step - loss: 0.9502
Epoch 148/250
858/858 [==============================] - 3s 4ms/step - loss: 0.9168
Epoch 149/250
858/858 [==============================] - 3s 4ms/step - loss: 0.9358
Epoch 150/250
858/858 [==============================] - 3s 4ms/step - loss: 0.9067
Epoch 151/250
858/858 [==============================] - 3s 4ms/step - loss: 0.9169
Epoch 152/250
858/858 [==============================] - 4s 4ms/step - loss: 0.8923
Epoch 153/250
858/858 [==============================] - 4s 4ms/step - loss: 0.8877
Epoch 154/250
858/858 [==============================] - 3s 4ms/step - loss: 0.8701
Epoch 155/250
858/858 [==============================] - 4s 4ms/step - loss: 0.8775
Epoch 156/250
858/858 [==============================] - 3s 4ms/step - loss: 0.8705
Epoch 157/250
858/858 [==============================] - 3s 4ms/step - loss: 0.8466
Epoch 158/250
858/858 [==============================] - 4s 4ms/step - loss: 0.8551
Epoch 159/250
858/858 [==============================] - 4s 4ms/step - loss: 0.8223
Epoch 160/250
858/858 [==============================] - 3s 4ms/step - loss: 0.8321
Epoch 161/250
858/858 [==============================] - 4s 4ms/step - loss: 0.8523
Epoch 162/250
858/858 [==============================] - 4s 4ms/step - loss: 0.8461
Epoch 163/250
858/858 [==============================] - 4s 4ms/step - loss: 0.8271
Epoch 164/250
858/858 [==============================] - 4s 4ms/step - loss: 0.8036
Epoch 165/250
858/858 [==============================] - 3s 4ms/step - loss: 0.8158
Epoch 166/250
858/858 [==============================] - 4s 4ms/step - loss: 0.8006
Epoch 167/250
858/858 [==============================] - 3s 4ms/step - loss: 0.7928
Epoch 168/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7739
Epoch 169/250
858/858 [==============================] - 3s 4ms/step - loss: 0.7598
Epoch 170/250
858/858 [==============================] - 4s 5ms/step - loss: 0.7807
Epoch 171/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7622
Epoch 172/250
858/858 [==============================] - 4s 5ms/step - loss: 0.7435
Epoch 173/250
858/858 [==============================] - 4s 5ms/step - loss: 0.7558
Epoch 174/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7484
Epoch 175/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7361
Epoch 176/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7410
Epoch 177/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7179
Epoch 178/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7116
Epoch 179/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6958
Epoch 180/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7096
Epoch 181/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7017
Epoch 182/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7102
Epoch 183/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6960
Epoch 184/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6877
Epoch 185/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6972
Epoch 186/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6780
Epoch 187/250
858/858 [==============================] - 4s 4ms/step - loss: 0.7340
Epoch 188/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6448
Epoch 189/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6623
Epoch 190/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6551
Epoch 191/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6598
Epoch 192/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6275
Epoch 193/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6790
Epoch 194/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6570
Epoch 195/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6294
Epoch 196/250
858/858 [==============================] - 3s 4ms/step - loss: 0.6333
Epoch 197/250
858/858 [==============================] - 3s 4ms/step - loss: 0.6060
Epoch 198/250
858/858 [==============================] - 3s 4ms/step - loss: 0.6559
Epoch 199/250
858/858 [==============================] - 3s 4ms/step - loss: 0.6299
Epoch 200/250
858/858 [==============================] - 3s 4ms/step - loss: 0.6084
Epoch 201/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5981
Epoch 202/250
858/858 [==============================] - 3s 4ms/step - loss: 0.6231
Epoch 203/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5644
Epoch 204/250
858/858 [==============================] - 4s 4ms/step - loss: 0.6006
Epoch 205/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5896
Epoch 206/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5725
Epoch 207/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5748
Epoch 208/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5833
Epoch 209/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5727
Epoch 210/250
858/858 [==============================] - 3s 4ms/step - loss: 0.6126
Epoch 211/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5573
Epoch 212/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5788
Epoch 213/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5703
Epoch 214/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5267
Epoch 215/250
858/858 [==============================] - 4s 4ms/step - loss: 0.5866
Epoch 216/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5395
Epoch 217/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5419
Epoch 218/250
858/858 [==============================] - 4s 4ms/step - loss: 0.5268
Epoch 219/250
858/858 [==============================] - 4s 5ms/step - loss: 0.6220
Epoch 220/250
858/858 [==============================] - 4s 5ms/step - loss: 0.5250
Epoch 221/250
858/858 [==============================] - 4s 4ms/step - loss: 0.5172
Epoch 222/250
858/858 [==============================] - 4s 4ms/step - loss: 0.5649
Epoch 223/250
858/858 [==============================] - 4s 4ms/step - loss: 0.5137
Epoch 224/250
858/858 [==============================] - 4s 4ms/step - loss: 0.5463
Epoch 225/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4923
Epoch 226/250
858/858 [==============================] - 4s 4ms/step - loss: 0.5246
Epoch 227/250
858/858 [==============================] - 4s 4ms/step - loss: 0.5035
Epoch 228/250
858/858 [==============================] - 4s 4ms/step - loss: 0.5221
Epoch 229/250
858/858 [==============================] - 3s 4ms/step - loss: 0.4909
Epoch 230/250
858/858 [==============================] - 3s 4ms/step - loss: 0.5029
Epoch 231/250
858/858 [==============================] - 4s 4ms/step - loss: 0.5508
Epoch 232/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4966
Epoch 233/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4793
Epoch 234/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4995
Epoch 235/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4965
Epoch 236/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4745
Epoch 237/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4851
Epoch 238/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4745
Epoch 239/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4745
Epoch 240/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4791
Epoch 241/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4810
Epoch 242/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4717
Epoch 243/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4574
Epoch 244/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4629
Epoch 245/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4959
Epoch 246/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4671
Epoch 247/250
858/858 [==============================] - 3s 4ms/step - loss: 0.4426
Epoch 248/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4481
Epoch 249/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4800
Epoch 250/250
858/858 [==============================] - 4s 4ms/step - loss: 0.4575
==Training duration model==
Epoch 1/250
886/886 [==============================] - 3s 3ms/step - loss: 1.6084
Epoch 2/250
886/886 [==============================] - 1s 2ms/step - loss: 1.4396
Epoch 3/250
886/886 [==============================] - 1s 2ms/step - loss: 1.4152
Epoch 4/250
886/886 [==============================] - 1s 2ms/step - loss: 1.3898
Epoch 5/250
886/886 [==============================] - 1s 2ms/step - loss: 1.3714
Epoch 6/250
886/886 [==============================] - 1s 2ms/step - loss: 1.3644
Epoch 7/250
886/886 [==============================] - 1s 2ms/step - loss: 1.3538
Epoch 8/250
886/886 [==============================] - 1s 2ms/step - loss: 1.3297
Epoch 9/250
886/886 [==============================] - 1s 2ms/step - loss: 1.3317
Epoch 10/250
886/886 [==============================] - 2s 2ms/step - loss: 1.3194
Epoch 11/250
886/886 [==============================] - 1s 2ms/step - loss: 1.2955
Epoch 12/250
886/886 [==============================] - 2s 2ms/step - loss: 1.2898
Epoch 13/250
886/886 [==============================] - 1s 2ms/step - loss: 1.2761
Epoch 14/250
886/886 [==============================] - 2s 2ms/step - loss: 1.2620
Epoch 15/250
886/886 [==============================] - 1s 2ms/step - loss: 1.2588
Epoch 16/250
886/886 [==============================] - 2s 2ms/step - loss: 1.2551
Epoch 17/250
886/886 [==============================] - 1s 2ms/step - loss: 1.2492
Epoch 18/250
886/886 [==============================] - 2s 2ms/step - loss: 1.2350
Epoch 19/250
886/886 [==============================] - 2s 2ms/step - loss: 1.2293
Epoch 20/250
886/886 [==============================] - 1s 2ms/step - loss: 1.2319
Epoch 21/250
886/886 [==============================] - 1s 2ms/step - loss: 1.2222
Epoch 22/250
886/886 [==============================] - 2s 2ms/step - loss: 1.2209
Epoch 23/250
886/886 [==============================] - 2s 2ms/step - loss: 1.2159
Epoch 24/250
886/886 [==============================] - 1s 2ms/step - loss: 1.2118
Epoch 25/250
886/886 [==============================] - 1s 2ms/step - loss: 1.2034
Epoch 26/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1988
Epoch 27/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1907
Epoch 28/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1852
Epoch 29/250
886/886 [==============================] - 1s 2ms/step - loss: 1.1818
Epoch 30/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1742
Epoch 31/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1732
Epoch 32/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1635
Epoch 33/250
886/886 [==============================] - 1s 2ms/step - loss: 1.1598
Epoch 34/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1484
Epoch 35/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1426
Epoch 36/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1431
Epoch 37/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1331
Epoch 38/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1276
Epoch 39/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1177
Epoch 40/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1125
Epoch 41/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1114
Epoch 42/250
886/886 [==============================] - 2s 2ms/step - loss: 1.1063
Epoch 43/250
886/886 [==============================] - 2s 2ms/step - loss: 1.0949
Epoch 44/250
886/886 [==============================] - 1s 2ms/step - loss: 1.0873
Epoch 45/250
886/886 [==============================] - 1s 2ms/step - loss: 1.0815
Epoch 46/250
886/886 [==============================] - 1s 2ms/step - loss: 1.0781
Epoch 47/250
886/886 [==============================] - 1s 1ms/step - loss: 1.0673
Epoch 48/250
886/886 [==============================] - 1s 2ms/step - loss: 1.0606
Epoch 49/250
886/886 [==============================] - 1s 2ms/step - loss: 1.0603
Epoch 50/250
886/886 [==============================] - 1s 2ms/step - loss: 1.0471
Epoch 51/250
886/886 [==============================] - 1s 2ms/step - loss: 1.0457
Epoch 52/250
886/886 [==============================] - 1s 2ms/step - loss: 1.0369
Epoch 53/250
886/886 [==============================] - 1s 2ms/step - loss: 1.0257
Epoch 54/250
886/886 [==============================] - 1s 2ms/step - loss: 1.0234
Epoch 55/250
886/886 [==============================] - 2s 2ms/step - loss: 1.0144
Epoch 56/250
886/886 [==============================] - 2s 2ms/step - loss: 1.0043
Epoch 57/250
886/886 [==============================] - 2s 2ms/step - loss: 1.0062
Epoch 58/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9982
Epoch 59/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9850
Epoch 60/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9817
Epoch 61/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9777
Epoch 62/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9638
Epoch 63/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9597
Epoch 64/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9475
Epoch 65/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9459
Epoch 66/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9393
Epoch 67/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9250
Epoch 68/250
886/886 [==============================] - 2s 2ms/step - loss: 0.9185
Epoch 69/250
886/886 [==============================] - 1s 2ms/step - loss: 0.9134
Epoch 70/250
886/886 [==============================] - 2s 2ms/step - loss: 0.8977
Epoch 71/250
886/886 [==============================] - 2s 2ms/step - loss: 0.8985
Epoch 72/250
886/886 [==============================] - 2s 2ms/step - loss: 0.8912
Epoch 73/250
886/886 [==============================] - 2s 2ms/step - loss: 0.8897
Epoch 74/250
886/886 [==============================] - 2s 2ms/step - loss: 0.8688
Epoch 75/250
886/886 [==============================] - 2s 2ms/step - loss: 0.8669
Epoch 76/250
886/886 [==============================] - 2s 2ms/step - loss: 0.8570
Epoch 77/250
886/886 [==============================] - 2s 2ms/step - loss: 0.8484
Epoch 78/250
886/886 [==============================] - 2s 2ms/step - loss: 0.8377
Epoch 79/250
886/886 [==============================] - 2s 2ms/step - loss: 0.8414
Epoch 80/250
886/886 [==============================] - 1s 2ms/step - loss: 0.8185
Epoch 81/250
886/886 [==============================] - 1s 2ms/step - loss: 0.8158
Epoch 82/250
886/886 [==============================] - 1s 2ms/step - loss: 0.8070
Epoch 83/250
886/886 [==============================] - 2s 2ms/step - loss: 0.7877
Epoch 84/250
886/886 [==============================] - 2s 2ms/step - loss: 0.7876
Epoch 85/250
886/886 [==============================] - 2s 2ms/step - loss: 0.7785
Epoch 86/250
886/886 [==============================] - 1s 2ms/step - loss: 0.7638
Epoch 87/250
886/886 [==============================] - 1s 2ms/step - loss: 0.7608
Epoch 88/250
886/886 [==============================] - 2s 2ms/step - loss: 0.7508
Epoch 89/250
886/886 [==============================] - 1s 2ms/step - loss: 0.7332
Epoch 90/250
886/886 [==============================] - 2s 2ms/step - loss: 0.7289
Epoch 91/250
886/886 [==============================] - 2s 2ms/step - loss: 0.7179
Epoch 92/250
886/886 [==============================] - 2s 2ms/step - loss: 0.7104
Epoch 93/250
886/886 [==============================] - 2s 2ms/step - loss: 0.6975
Epoch 94/250
886/886 [==============================] - 1s 2ms/step - loss: 0.6833
Epoch 95/250
886/886 [==============================] - 1s 2ms/step - loss: 0.6791
Epoch 96/250
886/886 [==============================] - 1s 2ms/step - loss: 0.6734
Epoch 97/250
886/886 [==============================] - 2s 2ms/step - loss: 0.6582
Epoch 98/250
886/886 [==============================] - 1s 2ms/step - loss: 0.6486
Epoch 99/250
886/886 [==============================] - 2s 2ms/step - loss: 0.6408
Epoch 100/250
886/886 [==============================] - 2s 2ms/step - loss: 0.6230
Epoch 101/250
886/886 [==============================] - 2s 2ms/step - loss: 0.6141
Epoch 102/250
886/886 [==============================] - 1s 2ms/step - loss: 0.6100
Epoch 103/250
886/886 [==============================] - 2s 2ms/step - loss: 0.5906
Epoch 104/250
886/886 [==============================] - 1s 2ms/step - loss: 0.5782
Epoch 105/250
886/886 [==============================] - 2s 2ms/step - loss: 0.5710
Epoch 106/250
886/886 [==============================] - 2s 2ms/step - loss: 0.5667
Epoch 107/250
886/886 [==============================] - 2s 2ms/step - loss: 0.5542
Epoch 108/250
886/886 [==============================] - 2s 2ms/step - loss: 0.5367
Epoch 109/250
886/886 [==============================] - 2s 2ms/step - loss: 0.5384
Epoch 110/250
886/886 [==============================] - 1s 2ms/step - loss: 0.5213
Epoch 111/250
886/886 [==============================] - 2s 2ms/step - loss: 0.5127
Epoch 112/250
886/886 [==============================] - 2s 2ms/step - loss: 0.5017
Epoch 113/250
886/886 [==============================] - 2s 2ms/step - loss: 0.4790
Epoch 114/250
886/886 [==============================] - 2s 2ms/step - loss: 0.4793
Epoch 115/250
886/886 [==============================] - 2s 2ms/step - loss: 0.4631
Epoch 116/250
886/886 [==============================] - 2s 2ms/step - loss: 0.4554
Epoch 117/250
886/886 [==============================] - 2s 2ms/step - loss: 0.4428
Epoch 118/250
886/886 [==============================] - 2s 2ms/step - loss: 0.4300
Epoch 119/250
886/886 [==============================] - 2s 2ms/step - loss: 0.4210
Epoch 120/250
886/886 [==============================] - 2s 2ms/step - loss: 0.4149
Epoch 121/250
886/886 [==============================] - 2s 2ms/step - loss: 0.3958
Epoch 122/250
886/886 [==============================] - 2s 2ms/step - loss: 0.3930
Epoch 123/250
886/886 [==============================] - 2s 2ms/step - loss: 0.3768
Epoch 124/250
886/886 [==============================] - 1s 2ms/step - loss: 0.3674
Epoch 125/250
886/886 [==============================] - 2s 2ms/step - loss: 0.3628
Epoch 126/250
886/886 [==============================] - 2s 2ms/step - loss: 0.3504
Epoch 127/250
886/886 [==============================] - 2s 2ms/step - loss: 0.3414
Epoch 128/250
886/886 [==============================] - 2s 2ms/step - loss: 0.3276
Epoch 129/250
886/886 [==============================] - 2s 2ms/step - loss: 0.3264
Epoch 130/250
886/886 [==============================] - 2s 2ms/step - loss: 0.3147
Epoch 131/250
886/886 [==============================] - 2s 2ms/step - loss: 0.2939
Epoch 132/250
886/886 [==============================] - 2s 2ms/step - loss: 0.2981
Epoch 133/250
886/886 [==============================] - 2s 2ms/step - loss: 0.2795
Epoch 134/250
886/886 [==============================] - 2s 2ms/step - loss: 0.2743
Epoch 135/250
886/886 [==============================] - 1s 2ms/step - loss: 0.2658
Epoch 136/250
886/886 [==============================] - 2s 2ms/step - loss: 0.2751
Epoch 137/250
886/886 [==============================] - 2s 2ms/step - loss: 0.2625
Epoch 138/250
886/886 [==============================] - 2s 2ms/step - loss: 0.2474
Epoch 139/250
886/886 [==============================] - 2s 2ms/step - loss: 0.2337
Epoch 140/250
886/886 [==============================] - 2s 2ms/step - loss: 0.2346
Epoch 141/250
886/886 [==============================] - 2s 2ms/step - loss: 0.2186
Epoch 142/250
886/886 [==============================] - 1s 2ms/step - loss: 0.2211
Epoch 143/250
886/886 [==============================] - 1s 2ms/step - loss: 0.2122
Epoch 144/250
886/886 [==============================] - 1s 2ms/step - loss: 0.2012
Epoch 145/250
886/886 [==============================] - 1s 2ms/step - loss: 0.2078
Epoch 146/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1984
Epoch 147/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1902
Epoch 148/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1818
Epoch 149/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1734
Epoch 150/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1833
Epoch 151/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1546
Epoch 152/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1669
Epoch 153/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1619
Epoch 154/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1588
Epoch 155/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1508
Epoch 156/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1379
Epoch 157/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1434
Epoch 158/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1304
Epoch 159/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1311
Epoch 160/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1394
Epoch 161/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1232
Epoch 162/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1247
Epoch 163/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1150
Epoch 164/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1285
Epoch 165/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1156
Epoch 166/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1112
Epoch 167/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1027
Epoch 168/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1076
Epoch 169/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1060
Epoch 170/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1129
Epoch 171/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1059
Epoch 172/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1066
Epoch 173/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0897
Epoch 174/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1016
Epoch 175/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1022
Epoch 176/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0925
Epoch 177/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1028
Epoch 178/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0837
Epoch 179/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0866
Epoch 180/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0884
Epoch 181/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0814
Epoch 182/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0912
Epoch 183/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0752
Epoch 184/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0855
Epoch 185/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0719
Epoch 186/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0799
Epoch 187/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0722
Epoch 188/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0794
Epoch 189/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0664
Epoch 190/250
886/886 [==============================] - 1s 2ms/step - loss: 0.1116
Epoch 191/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0697
Epoch 192/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0680
Epoch 193/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0650
Epoch 194/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0802
Epoch 195/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0676
Epoch 196/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0678
Epoch 197/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0650
Epoch 198/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0650
Epoch 199/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0653
Epoch 200/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0553
Epoch 201/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0655
Epoch 202/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0778
Epoch 203/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0552
Epoch 204/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0564
Epoch 205/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0621
Epoch 206/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0644
Epoch 207/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0666
Epoch 208/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0561
Epoch 209/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0521
Epoch 210/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0617
Epoch 211/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0506
Epoch 212/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0588
Epoch 213/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0557
Epoch 214/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0773
Epoch 215/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0516
Epoch 216/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0639
Epoch 217/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0488
Epoch 218/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0690
Epoch 219/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0486
Epoch 220/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0721
Epoch 221/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0459
Epoch 222/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0718
Epoch 223/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0443
Epoch 224/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0498
Epoch 225/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0517
Epoch 226/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0470
Epoch 227/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0550
Epoch 228/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0509
Epoch 229/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0413
Epoch 230/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0632
Epoch 231/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0438
Epoch 232/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0503
Epoch 233/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0638
Epoch 234/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0414
Epoch 235/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0658
Epoch 236/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0542
Epoch 237/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0418
Epoch 238/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0531
Epoch 239/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0421
Epoch 240/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0602
Epoch 241/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0524
Epoch 242/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0423
Epoch 243/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0922
Epoch 244/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0417
Epoch 245/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0439
Epoch 246/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0741
Epoch 247/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0437
Epoch 248/250
886/886 [==============================] - 2s 2ms/step - loss: 0.0411
Epoch 249/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0676
Epoch 250/250
886/886 [==============================] - 1s 2ms/step - loss: 0.0364

Check loss

The loss of each model is plotted in order to see how the training has progressed:

In [10]:
def plotLoss(history, name):
    pyplot.plot(history.history['loss'])
    pyplot.title(name + " loss per epoch")
    pyplot.ylabel('loss')
    pyplot.xlabel('epoch')
    pyplot.legend(['train', 'validation'], loc='upper right')
    pyplot.show()
    
plotLoss(noteModelHistory, "Note model")
plotLoss(durationModelHistory, "Duration model")

Create prediction

The network has now reached a point where predictions can be made, and in turn, new music genearted.

In [28]:
def predictFeature(feature, model, length):
    # Take a random sequence from the training data
    tempPrediction = feature[np.random.randint(0, len(feature)-1)]
    generated = []
    for i in range(200):
        # Make a prediction
        prediction_input = np.reshape(tempPrediction, (1, len(tempPrediction), 1))
        guess = model.predict(prediction_input, verbose=0)
        result = np.argmax(guess)
        # Add the new prediced feature and snip the temp array
        tempPrediction = np.append(tempPrediction, result)
        tempPrediction = tempPrediction[1:]
        # Append prediction to return array
        generated.append(result)
    return generated
        
notePredictionsRaw = predictFeature(xNote, noteModel, 200)
durationPredictionsRaw = predictFeature(xDuration, durationModel, 200)

Reverse encoding

In order to retrieve the name of the notes which have been predicted by the network, reversal of the one-hot-encoding is needed. Below two functions are created, one for reversing the one hot encoded dictionaries, and one which loops through each note in the predictions, reversing the encoding:

In [29]:
def reverseDict(thisDict):
    return {v: k for k, v in thisDict.items()}

def decodePredictions(predictions, thisDict):
    decodedPredictions = []
    invDict = reverseDict(thisDict)
    for thisPrediction in predictions:
        decodedPredictions.append(invDict[thisPrediction])
    return decodedPredictions

notePredictions = decodePredictions(notePredictionsRaw, noteDict)
durationPredictions = decodePredictions(durationPredictionsRaw, durationDict)

Check the score

Now the generated notes and durations can be printed out in order to ensure no strange results have been created:

In [30]:
print(notePredictions)
print(durationPredictions)
['C#5', 'D5', 'B4', 'C5', 'B-4', 'A4', 'A4', 'D5', 'F5', 'E5', 'G5', 'F5', 'E5', 'D5', 'F5', 'E5', 'F5', 'D5', 'E5', 'D5', 'C5', 'B4', 'C5', 'A5', 'G5', 'F#5', 'G5', 'B-5', 'C#5', 'D5', 'D5', 'B4', 'C#5', 'D5', 'C5', 'B-4', 'A4', 'D4', 'G4', 'A4', 'C5', 'B-4', 'C5', 'E-5', 'D5', 'F#5', 'A5', 'G5', 'A5', 'B-5', 'C#5', 'D5', 'D4', 'A4', 'F4', 'D4', 'C#4', 'D4', 'E4', 'F4', 'G4', 'F4', 'E4', 'D4', 'E4', 'F4', 'G4', 'A4', 'A3', 'B3', 'C4', 'A3', 'F4', 'B3', 'E4', 'F4', 'E4', 'D4', 'E4', 'F#4', 'G4', 'F4', 'E4', 'D4', 'E4', 'F4', 'D4', 'G4', 'G4', 'G4', 'E4', 'D4', 'C#4', 'D4', 'G4', 'C4', 'F4', 'E4', 'F4', 'G#3', 'E4', 'D4', 'C4', 'D4', 'F4', 'E4', 'D4', 'C4', 'D4', 'A4', 'F4', 'D4', 'C#4', 'D4', 'D4', 'F4', 'G4', 'F4', 'E4', 'D4', 'G4', 'E4', 'F4', 'E4', 'A4', 'F#4', 'G4', 'F#4', 'D5', 'G#4', 'A4', 'A3', 'C4', 'A3', 'D4', 'F4', 'E4', 'D4', 'C4', 'A4', 'D4', 'E4', 'A4', 'G4', 'F4', 'E4', 'D4', 'D4', 'G4', 'G4', 'C4', 'A3', 'B-3', 'A3', 'F3', 'G3', 'C4', 'A3', 'B3', 'E4', 'C#4', 'D4', 'G4', 'A4', 'B-4', 'A4', 'F4', 'G4', 'A4', 'B-4', 'A4', 'G4', 'F4', 'D4', 'A4', 'G4', 'F#4', 'D4', 'G4', 'E4', 'F4', 'D4', 'G4', 'E4', 'A4', 'F4', 'B-4', 'G4', 'A4', 'F4', 'G4', 'F4', 'D4', 'A4', 'D5', 'C#5', 'B4', 'C#5', 'D5', 'A4']
['1.0', '0.5', '0.5', '1.0', '1.0', '2.0', '1.0', '0.5', '2.0', '0.5', '0.5', '1.5', '0.5', '1.5', '0.5', '2.0', '1.0', '2.0', '0.5', '0.5', '2.5', '0.5', '1.0', '2.5', '0.5', '1.0', '2.5', '0.5', '1.0', '2.0', '1.0', '1.0', '3.0', '0.5', '0.5', '1.0', '2.0', '2.0', '2.0', '2.0', '2.0', '1.0', '1.0', '2.5', '0.5', '0.5', '0.5', '1.0', '3.0', '2.0', '2.0', '2.0', '2.0', '2.0', '1.0', '1.0', '2.5', '0.5', '0.5', '0.5', '2.0', '0.5', '0.5', '0.5', '0.5', '1.5', '0.5', '1.5', '0.5', '0.5', '0.5', '0.5', '0.5', '0.5', '0.5', '2.0', '3.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0', '0.5', '0.5', '1.0']

Generate Score

The next step is to convert the two arrays into a music21 score in order to eventually convert them into MIDI. Below a function takes in the generated notes and durations and outputs a score. An organ voice is chosen for the piece:

In [31]:
def generateScore(genNotes, genDurs):
    generatedScore = stream.Score()
    mainPart = stream.Part()
    for i in range(len(genNotes)):
        thisNote = note.Note(genNotes[i])
        thisNote.duration.quarterLength=float(genDurs[i])
        mainPart.append(thisNote)
    mainPart.insert(0, instrument.Organ())
    generatedScore.insert(0, mainPart)
    return generatedScore

generatedScore = generateScore(notePredictions, durationPredictions)

Visualise Score

Before writing the score to file, first it is visualised to ensure conversion occured correctly:

In [32]:
generatedFreqArray = createFreqArray(generatedScore)
visualiseFreqArray(generatedFreqArray)

Write Score to MIDI file

And finally the score is written to the file generated.mid:

In [33]:
generatedScore.write("midi", "generated.mid")
Out[33]:
'generated.mid'

Conversion to wav, then mp3

In order to play the midi file it needs to be converted to wav, then finally mp3. Ways of doing this vary depending on platform. On Linux, the following command was used:

wildmidi generated.mid --wavout=generated.wav

then

yes | ffmpeg -i generated.wav generated.mp3

(with yes preventing ffmpeg expecting input from the terminal)

and finally removing the wav file

rm generated.wav

In [6]:
def midiToWav(filename):
    cmd = "wildmidi "+filename+".mid --wavout="+filename+".wav"
    print(os.popen(cmd).read())
    cmd = "yes | ffmpeg -i "+filename+".wav "+filename+".mp3"
    print(os.popen(cmd).read())
    cmd = "rm "+filename+".wav"
    print(os.popen(cmd).read())
    
midiToWav("generated")
WildMidi 0.4.0 Open Source Midi Sequencer
Copyright (C) WildMIDI Developers 2001-2016

WildMidi comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome to redistribute it under
the terms and conditions of the GNU General Public License version 3.
For more information see COPYING

Report bugs to https://github.com/Mindwerks/wildmidi/issues
WildMIDI homepage is at http://www.mindwerks.net/projects/wildmidi/

Initializing Sound System
Initializing libWildMidi 0.4.0

 +  Volume up        e  Better resampling    n  Next Midi
 -  Volume down      l  Log volume           q  Quit
 ,  1sec Seek Back   r  Reverb               .  1sec Seek Forward
 m  save as midi     p  Pause On/Off


Playing generated.mid 
[Approx  1m 28s Total]
Finishing and closing wav output




Results

(If the piece won't play, rerun the python code. Ocasionally the player will display 9:17:59, this is just a glitch and should be ignored, alternativly the wav files are available in the notebook directory)

Final result

Below is the final piece converted into wav format:

In [7]:
ipd.Audio('generated.mp3') 
Out[7]: