

{"id":106940,"date":"2021-12-24T10:27:07","date_gmt":"2021-12-24T04:57:07","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=106940"},"modified":"2026-06-01T14:35:35","modified_gmt":"2026-06-01T09:05:35","slug":"automatic-music-generation-lstm-deep-learning","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/","title":{"rendered":"Automatic Music Generation Project using Deep Learning"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:299,&quot;href&quot;:&quot;https:\\\/\\\/www.kaggle.com\\\/soumikrakshit\\\/classical-music-midi&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20230418023434\\\/http:\\\/\\\/www.kaggle.com\\\/soumikrakshit\\\/classical-music-midi&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-08 01:57:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-11 05:20:30&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-14 10:43:39&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-18 01:36:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-21 16:53:47&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-25 08:15:27&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-28 10:07:31&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-31 18:11:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-04 04:40:51&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-07 08:12:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-12 06:00:07&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-15 09:39:20&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-19 05:46:27&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-25 04:22:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-29 12:11:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-01 16:52:40&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-08 07:28:01&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-12 17:14:51&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-16 02:21:20&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-19 12:08:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-23 02:47:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-26 16:03:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-03 08:42:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-07 09:42:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-10 11:17:09&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-14 05:30:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-17 06:47:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-21 18:15:31&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-25 05:00:51&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-28 09:43:12&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-31 10:07:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-05 19:53:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-09 12:04:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-14 12:23:19&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-18 16:23:33&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-21 21:56:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-25 09:39:09&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-28 21:23:23&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-02 13:13:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-05 16:37:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-13 18:53:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-18 13:47:41&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-21 15:43:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-25 03:50:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-28 20:33:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-02 05:25:53&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-02 05:25:53&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:300,&quot;href&quot;:&quot;http:\\\/\\\/www.piano-midi.de&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250804003912\\\/http:\\\/\\\/piano-midi.de\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-08 01:57:10&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-11 05:20:30&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-14 10:43:39&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-18 01:36:48&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-21 16:53:56&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-25 08:15:27&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-28 10:07:51&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-31 18:11:50&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-04 04:40:51&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-07 08:12:04&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-12 06:00:07&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-15 09:39:21&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-19 05:46:28&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-25 04:22:07&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-01-29 12:11:24&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-02-01 16:52:40&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-02-08 07:28:02&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-02-12 17:14:52&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-02-16 02:21:21&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-02-19 12:08:44&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-02-23 02:47:05&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-02-26 16:03:08&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-03-03 08:42:06&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-07 09:42:54&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-03-10 11:17:11&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-03-14 05:30:29&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-03-17 06:47:35&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-03-21 18:15:42&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-03-25 05:00:54&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-03-28 09:43:19&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-31 10:07:09&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-04-05 19:53:54&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-04-09 12:04:24&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-04-14 12:23:23&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-04-18 16:23:31&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-21 21:56:33&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-04-25 09:39:13&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-04-28 21:23:24&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-05-02 13:13:07&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-05-05 16:37:38&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-05-13 18:54:01&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-05-18 13:47:45&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-05-21 15:43:26&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-05-25 03:50:37&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-05-28 20:33:13&quot;,&quot;http_code&quot;:418},{&quot;date&quot;:&quot;2026-06-02 05:26:00&quot;,&quot;http_code&quot;:418}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-02 05:26:00&quot;,&quot;http_code&quot;:418},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:2637,&quot;href&quot;:&quot;https:\\\/\\\/drive.google.com\\\/file\\\/d\\\/1uVwWiPhzHx3x554eUzNa8jIYcN1DQ5js\\\/view?usp=drive_link&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>Automatic Music Generation is a process where a system composes short pieces of music using different parameters like pitch interval, notes, chords, tempo, etc. In this Project we are going to use Piano Instrument with the following terms:<\/p>\n<ul>\n<li>Note: This is a sound produced by a single key.<\/li>\n<li>Chords: The combination of 2 or more notes is called a chord.<\/li>\n<li>Octave: The distance between two notes is stated as an octave in a piano. It is specifically the gap between the two notes that share the same letter name.<\/li>\n<\/ul>\n<h3>About this Project :<\/h3>\n<p>In this project, we will be creating an Automatic Music Generation model using LSTM. We will fetch notes from all music files which will then be fed into the model for prediction. Then finally we will create a MIDI file using these predicted notes.<\/p>\n<h3>LSTM for Automatic Music Generation :<\/h3>\n<p>Long Short Term Memory(LSTM) is a type of RNN (Recurrent Neural Network) that solves some scenarios where RNN failed. LSTM solves Long-Term dependency problem of RNN i.e. RNN networks store data of previous output in a memory for a very short period of time.<\/p>\n<p>LSTM also solves the problem of Vanishing Gradient i.e. in order to get the best result, the model tries to minimize the loss after every time step by calculating loss with respect to some weights. After reaching a certain period, this weight becomes so less that it approximates to zero or vanishes and the model stops training.<\/p>\n<h3>Drawback of LSTM:<\/h3>\n<p>LSTM requires lots of resources and time to get trained for real world applications. Randomly initializing different weights makes LSTM networks behave similar to that of feed forward neural networks. Therefore they require small weight initialization instead.<\/p>\n<p>Our model will be a Many-to-one sequence model as there will be one output for every sequence of input notes after each timesteps.<\/p>\n<p>Input to the LSTM model will be the amplitude(A) of these notes which are recorded at different intervals of time which computes hidden vectors and passes to the next layer for the next timestep(t).<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/lstm-architecture.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-106955\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/lstm-architecture.webp\" alt=\"lstm architecture\" width=\"756\" height=\"427\" \/><\/a><\/p>\n<h3>Dataset for Automatic Music Generation Project:<\/h3>\n<p>Please download the dataset for the automatic music generation project from the following link <a href=\"https:\/\/www.kaggle.com\/soumikrakshit\/classical-music-midi\">Classical Music MIDI<\/a><\/p>\n<p>This dataset consists of classical piano midi files containing compositions of 19 famous composers scraped from <a href=\"http:\/\/www.piano-midi.de\/\">Official Classical Piano Midi<\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/automatic-music-generation-dataset-file.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-106956\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/automatic-music-generation-dataset-file.webp\" alt=\"automatic music generation dataset file\" width=\"1219\" height=\"361\" \/><\/a><\/p>\n<h3>Download Automatic Music Generation Project Code<\/h3>\n<p>Please download the source code of automatic music generation with tensorflow: <a href=\"https:\/\/drive.google.com\/file\/d\/1uVwWiPhzHx3x554eUzNa8jIYcN1DQ5js\/view?usp=drive_link\"><strong>Automatic Music Generation Project Code<\/strong><\/a><\/p>\n<h3>Automatic Music Generation Project Prerequisites<\/h3>\n<p>Install the following libraries using pip :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">pip install numpy, music21, tensorflow, sklearn<\/pre>\n<p>The versions which are used in automatic music generation project for python and its corresponding modules are as follows:<\/p>\n<ol>\n<li>Python: 3.8.5<\/li>\n<li>TensorFlow: 2.3.1\u00a0 <em>Note: TensorFlow version should be 2.2 or higher in order to use Keras or else install keras directly<\/em><\/li>\n<li>music21: 5.5.0<\/li>\n<li>Numpy: 1.19.5<\/li>\n<li>sklearn: 0.24.2<\/li>\n<\/ol>\n<p>Music21 is a python library that is used to parse and read various musical files. In this project we will be using the Musical Instrument Digital Interface (MIDI) file which is a small file size with ease of modification and manipulation and a wide choice of electronic instruments. It is a universally accepted file format, which means that music produced by one synthesiser in MIDI format can be modified by another synthesiser.<\/p>\n<h3>Project Structure<\/h3>\n<ul>\n<li>All Midi Files\/: This is the dataset folder containing various midi files of different composers.<\/li>\n<li>auto_music_gen.py: In this file, we will build, train and test our model.<\/li>\n<li>s2s\/: This directory contains optimizer, metrics, and weights of our trained model.<\/li>\n<li>pred_music.mid: This is a music file of predicted notes.<\/li>\n<\/ul>\n<h3>Steps for Automatic Music Generation Project:<\/h3>\n<h4>1. Import Libraries<\/h4>\n<p>Firstly we will import all the required libraries which have been shared in the prerequisites section.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#DataFlair Automatic Music Generation Project\r\n#load all the libraries\r\nfrom music21 import *\r\nimport glob\r\nfrom tqdm import tqdm\r\nimport numpy as np\r\nimport random\r\nfrom tensorflow.keras.layers import LSTM,Dense,Input,Dropout\r\nfrom tensorflow.keras.models import Sequential,Model,load_model\r\nfrom sklearn.model_selection import train_test_split<\/pre>\n<h4>2. Reading and Parsing the Midi File<\/h4>\n<p>We will now read the midi file dataset. We will be using \u201cSchubert\u201d composed files. You can use more or less depending on your system.<\/p>\n<p>For this project, we will be only working on files that contain sequential streams of Piano data. We will separate all files by their instruments and use only Piano. Piano stream from the midi file contains many datas like Keys, Time Signature, Chord, Note etc. We don\u2019t require all of this except Notes and Chords to generate music. Lastly, we will return arrays of notes and chords.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def read_files(file):\r\n notes=[]\r\n notes_to_parse=None\r\n #parse the midi file\r\n midi=converter.parse(file)\r\n #seperate all instruments from the file\r\n instrmt=instrument.partitionByInstrument(midi)\r\n\r\nfor part in instrmt.parts:\r\n #fetch data only of Piano instrument\r\n if 'Piano' in str(part):\r\n notes_to_parse=part.recurse()\r\n\r\n #iterate over all the parts of sub stream elements\r\n #check if element's type is Note or chord\r\n #if it is chord split them into notes\r\n for element in notes_to_parse:\r\n if type(element)==note.Note:\r\n  notes.append(str(element.pitch))\r\n elif type(element)==chord.Chord:\r\n  notes.append('.'.join(str(n) for n in element.normalOrder))\r\n\r\n#return the list of notes\r\nreturn notes\r\n\r\n#retrieve paths recursively from inside the directories\/files\r\nfile_path=[\"schubert\"]\r\nall_files=glob.glob('All Midi Files\/'+file_path[0]+'\/*.mid',recursive=True)\r\n\r\n#reading each midi file\r\nnotes_array = np.array([read_files(i) for i in tqdm(all_files,position=0,leave=True)])<\/pre>\n<h4>3. Exploring the dataset<\/h4>\n<p>Let\u2019s check how many unique notes we have and what is the distribution of these notes.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#unique notes\r\nnotess = sum(notes_array,[])\r\nunique_notes = list(set(notess))\r\nprint(\"Unique Notes:\",len(unique_notes))\r\n\r\n#notes with their frequency\r\nfreq=dict(map(lambda x: (x,notess.count(x)),unique_notes))\r\n\r\n#get the threshold frequency\r\nfor i in range(30,100,20):\r\nprint(i,\":\",len(list(filter(lambda x:x[1]&gt;=i,freq.items()))))\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/explore-dataset.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-106957\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/explore-dataset.webp\" alt=\"explore dataset\" width=\"392\" height=\"230\" \/><\/a><\/p>\n<p>As you can see, we have 304 unique notes and most of our notes have frequencies greater than 30 or 50. It will become hard to train our model with all the notes therefore for this project we will be going to use 50 as a threshold frequency. So we will take only those notes which have frequencies more than 50. You can anytime change these parameters.<\/p>\n<p>Also, we will change our \u2018notes_array\u2019 which will contain notes that are greater than threshold frequency.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#filter notes greater than threshold i.e. 50\r\nfreq_notes=dict(filter(lambda x:x[1]&gt;=50,freq.items()))\r\n\r\n#create new notes using the frequent notes\r\nnew_notes=[[i for i in j if i in freq_notes] for j in notes_array]<\/pre>\n<p>We are going to create two dictionaries where one will have notes index as a key and notes as value and other will be the reverse of the first i.e key will be notes and value will be its respective index. We will be going to see its use later.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#dictionary having key as note index and value as note\r\nind2note=dict(enumerate(freq_notes))\r\n\r\n#dictionary having key as note and value as note index\r\nnote2ind=dict(map(reversed,ind2note.items()))<\/pre>\n<h4>4. Input and Output Sequence for model<\/h4>\n<p>Now we will create input and output sequences for our model. We will be using a timestep of 50. So if we traverse 50 notes of our input sequence then the 51th note will be the output for that sequence. Let\u2019s take an example to see how it works.<\/p>\n<p>We will use \u2018DataFlair is best for machine learning projects\u2019 sentence and a timestep of 2. So we will provide 2 words at every input to get the output.<\/p>\n<p>(x)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 (y)<\/p>\n<p>DataFlair is,\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0best<br \/>\nis best,\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0for<br \/>\nfor machine,\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0learning<br \/>\nmachine learning\u00a0 \u00a0 \u00a0 \u00a0 projects<\/p>\n<p>And so on as you can see after feeding input(x) of 2 words(timesteps) our output(y) is the next word. As our model requires numeric data, we will convert all notes to its respective index value using the \u201cnote2ind\u201d (note to index) dictionary which we have created earlier.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#timestep\r\ntimesteps=50\r\n\r\n#store values of input and output\r\nx=[] ; y=[]\r\n\r\nfor i in new_notes:\r\n for j in range(0,len(i)-timesteps):\r\n  #input will be the current index + timestep\r\n  #output will be the next index after timestep\r\n  inp=i[j:j+timesteps] ; out=i[j+timesteps]\r\n\r\n  #append the index value of respective notes\r\n  x.append(list(map(lambda x:note2ind[x],inp)))\r\n  y.append(note2ind[out])\r\n\r\nx_new=np.array(x)\r\ny_new=np.array(y)<\/pre>\n<h4>5. Training and Testing sets<\/h4>\n<p>We will reshape our array for our model and split the data into 80:20 ratio. 80% for the training set and 20% for the testing set.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#reshape input and output for the model\r\nx_new = np.reshape(x_new,(len(x_new),timesteps,1))\r\ny_new = np.reshape(y_new,(-1,1))\r\n\r\n#split the input and value into training and testing sets\r\n#80% for training and 20% for testing sets\r\nx_train,x_test,y_train,y_test = train_test_split(x_new,y_new,test_size=0.2,random_state=42)<\/pre>\n<h4>6. Building the model<\/h4>\n<p>As we have discussed earlier we are going to use LSTM model architecture. We will use 2 stacked LSTM layers with a dropout rate of 0.2. Dropout basically prevents overfitting while training the model, while it does not affect the inference model. Finally we will be using a fully connected Dense layer for output.<\/p>\n<p>Output dimension of the Dense Layer will be equal to the length of our unique notes along with the \u2018softmax\u2019 activation function which is used for multi-class classification problems.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#create the model\r\nmodel = Sequential()\r\n#create two stacked LSTM layer with the latent dimension of 256\r\nmodel.add(LSTM(256,return_sequences=True,input_shape=(x_new.shape[1],x_new.shape[2])))\r\nmodel.add(Dropout(0.2))\r\nmodel.add(LSTM(256))\r\nmodel.add(Dropout(0.2))\r\nmodel.add(Dense(256,activation='relu'))\r\n#fully connected layer for the output with softmax activation\r\nmodel.add(Dense(len(note2ind),activation='softmax'))\r\nmodel.summary()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/model-summary.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-106958\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/model-summary.webp\" alt=\"model summary\" width=\"978\" height=\"436\" \/><\/a><\/p>\n<h4>7) Train the Model<\/h4>\n<p>After building the model, we will now train it on the input and output data. For this will be using \u2018Adam\u2019 optimizer on batch size of 128 and for total 80 epochs.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#compile the model using Adam optimizer\r\nmodel.compile(loss='sparse_categorical_crossentropy', optimizer='adam',metrics=['accuracy'])\r\n\r\n#train the model on training sets and validate on testing sets\r\nmodel.fit(\r\n x_train,y_train,\r\n batch_size=128,epochs=80,\r\n validation_data=(x_test,y_test))<\/pre>\n<p>After we finish training let&#8217;s save the model for prediction.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#save the model for predictions\r\nmodel.save(\"s2s\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/model-s2s.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-106959\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/model-s2s.webp\" alt=\"model s2s\" width=\"732\" height=\"255\" \/><\/a><\/p>\n<h4>8) Inference (sampling) phase.<\/h4>\n<p>In this section we will finally compose our own music. Using the trained model we will predict the notes.<\/p>\n<p>Firstly generate a random integer(index) for our testing input array which will be our testing input pattern. We will reshape our array and predict the output. Using the \u2018np.argmax()\u2019 function, we will get the data of the maximum probability value. Convert this predicted index to notes using \u2018ind2note\u2019(index to note) dictionary. Our next music pattern will be one step ahead of the previous pattern. Repeat this process till we generate 200 notes. Again you can change this parameter as per your requirements.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#load the model\r\nmodel = load_model(\u201cs2s\u201d)\r\n#generate random index\r\nindex = np.random.randint(0,len(x_test)-1)\r\n#get the data of generated index from x_test\r\nmusic_pattern = x_test[index]\r\nout_pred=[] #it will store predicted notes\r\n\r\n#iterate till 200 note is generated\r\nfor i in range(200):\r\n\r\n #reshape the music pattern\r\n music_pattern = music_pattern.reshape(1,len(music_pattern),1)\r\n\r\n #get the maximum probability value from the predicted output\r\n pred_index = np.argmax(model.predict(music_pattern))\r\n #get the note using predicted index and\r\n #append to the output prediction list\r\n out_pred.append(ind2note[pred_index])\r\n music_pattern = np.append(music_pattern,pred_index)\r\n\r\n #update the music pattern with one timestep ahead\r\n music_pattern = music_pattern[1:]<\/pre>\n<h4>9) Saving the file<\/h4>\n<p>Finally, we are ready with the predicted output notes. Now we will save them into a MIDI file.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">output_notes = []\r\nfor offset,pattern in enumerate(out_pred):\r\n#if pattern is a chord instance\r\nif ('.' in pattern) or pattern.isdigit():\r\n #split notes from the chord\r\n notes_in_chord = pattern.split('.')\r\n notes = []\r\n for current_note in notes_in_chord:\r\n  i_curr_note=int(current_note)\r\n  #cast the current note to Note object and\r\n  #append the current note\r\n  new_note = note.Note(i_curr_note)\r\n  new_note.storedInstrument = instrument.Piano()\r\n  notes.append(new_note)\r\n\r\n #cast the current note to Chord object\r\n #offset will be 1 step ahead from the previous note\r\n #as it will prevent notes to stack up\r\n new_chord = chord.Chord(notes)\r\n new_chord.offset = offset\r\n output_notes.append(new_chord)\r\n\r\nelse:\r\n #cast the pattern to Note object apply the offset and\r\n #append the note\r\n new_note = note.Note(pattern)\r\n new_note.offset = offset\r\n new_note.storedInstrument = instrument.Piano()\r\n output_notes.append(new_note)\r\n\r\n#save the midi file\r\nmidi_stream = stream.Stream(output_notes)\r\nmidi_stream.write('midi', fp='pred_music.mid')<\/pre>\n<p><strong>You can listen the predicted music from the Shared Codes of this project.<\/strong><\/p>\n<h3>Summary<\/h3>\n<p>We built a model for Automatic Music Generation. It predicted for every input note at different timesteps. The accuracy of the model is 80%, which is quite good as we have taken a midi file from only one composer. You can try with more composers which will increase the model accuracy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Automatic Music Generation is a process where a system composes short pieces of music using different parameters like pitch interval, notes, chords, tempo, etc. In this Project we are going to use Piano Instrument&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":106960,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22216],"tags":[26449,26458,22272,26450,20623,26457],"class_list":["post-106940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-deep-learning","tag-automatic-music-generation-project","tag-automatic-music-generation-using-deep-learning","tag-deep-learning-project-for-beginners","tag-lstm-automatic-music-generation-project","tag-ml-project","tag-music-generation-lstm"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Automatic Music Generation Project using Deep Learning - DataFlair<\/title>\n<meta name=\"description\" content=\"Develop machine learning project of Automatic Music Generation using LSTM Neural Network. It predicts every input note at different timesteps\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automatic Music Generation Project using Deep Learning - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Develop machine learning project of Automatic Music Generation using LSTM Neural Network. It predicts every input note at different timesteps\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-24T04:57:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-01T09:05:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/automatic-music-generation-deep-learning-lstm.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Automatic Music Generation Project using Deep Learning - DataFlair","description":"Develop machine learning project of Automatic Music Generation using LSTM Neural Network. It predicts every input note at different timesteps","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/","og_locale":"en_US","og_type":"article","og_title":"Automatic Music Generation Project using Deep Learning - DataFlair","og_description":"Develop machine learning project of Automatic Music Generation using LSTM Neural Network. It predicts every input note at different timesteps","og_url":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-12-24T04:57:07+00:00","article_modified_time":"2026-06-01T09:05:35+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/automatic-music-generation-deep-learning-lstm.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Automatic Music Generation Project using Deep Learning","datePublished":"2021-12-24T04:57:07+00:00","dateModified":"2026-06-01T09:05:35+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/"},"wordCount":1374,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/automatic-music-generation-deep-learning-lstm.webp","keywords":["Automatic Music Generation Project","automatic music generation using deep learning","deep learning project for beginners","LSTM Automatic Music Generation Project","ML project","music generation lstm"],"articleSection":["Deep Learning Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/","url":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/","name":"Automatic Music Generation Project using Deep Learning - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/automatic-music-generation-deep-learning-lstm.webp","datePublished":"2021-12-24T04:57:07+00:00","dateModified":"2026-06-01T09:05:35+00:00","description":"Develop machine learning project of Automatic Music Generation using LSTM Neural Network. It predicts every input note at different timesteps","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/automatic-music-generation-deep-learning-lstm.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/automatic-music-generation-deep-learning-lstm.webp","width":1200,"height":628,"caption":"automatic music generation deep learning lstm"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/automatic-music-generation-lstm-deep-learning\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Deep Learning Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/deep-learning\/"},{"@type":"ListItem","position":3,"name":"Automatic Music Generation Project using Deep Learning"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":false,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/106940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=106940"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/106940\/revisions"}],"predecessor-version":[{"id":148730,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/106940\/revisions\/148730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/106960"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=106940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=106940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=106940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}