<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/love-machine-automating-the-romantic-songwriting-process/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.mango-solutions.com/blog/love-machine-automating-the-romantic-songwriting-process"> Mango Solutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <img sizes="100vw" src="https://i2.wp.com/www.mango-solutions.com/blog/images/7/c/f/e/a/7cfea28325be977c285867512b25f591ce0865d8-love-machine-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h5>Owen Jones, Placement Student</h5>
<blockquote>
<p>Songwriting is a very mysterious process. It feels like creating something from nothing. It’s something I don’t feel like I really control.</p>
<p><em>— Tracy Chapman</em></p>
</blockquote>
<p>It is February. The shortest, coldest, wettest, miserablest month of the British year.</p>
<p>Only two things happen in Britain during February. For a single evening, the people refrain from dipping all their food in batter and deep-frying it, and instead save some time by pouring the batter straight into a frying pan and eating it by itself; and for an entire day, the exchange of modest indications of affection between consenting adults is permitted, although the government advises against significant deviation from the actions specified in the state-issued Approved Romantic Gestures Handbook.</p>
<p>In Section 8.4 (Guidelines for Pre-Marital Communication) the following suggestion is made:</p>
<pre><code>&quot;Written expressions of emotion should be avoided where possible. Should it become absolutely necessary to express emotion in a written format, it should be limited to a 'popular' form of romantic lyricism. Examples of such 'popular' forms include 'love poem' and 'love song'.</code></pre>
<p>Thankfully, for those who have not achieved at least a master’s degree in a related field, writing a poem or song is a virtually impossible task. And following the sustained and highly successful effort to persuade the British youth that a career in the arts is a fast-track to unemployment, the number of applications to study non-STEM subjects at British universities has been falling consistently since the turn of the decade. This ensures that only the very best and most talented songwriters, producing the most creatively ingenuous work, are able to achieve widespread recognition, and therefore the British public are only exposed to high-quality creative influences.</p>
<p>But to us scientists, the lack of method is disturbing. This “creativity” must have a rational explanation. There must be some pattern.</p>
<p>This is unquestionably a problem which can be solved by machine learning, so let’s take the most obvious approach we can: we’ll train a recurrent neural network to generate song lyrics character by character.</p>
<blockquote>
<p>You write down a paragraph or two describing several different subjects creating a kind of story ingredients-list, I suppose, and then cut the sentences into four or five-word sections; mix ’em up and reconnect them. You can get some pretty interesting idea combinations like this. You can use them as is or, if you have a craven need to not lose control, bounce off these ideas and write whole new sections.</p>
<p><em>— David Bowie</em></p>
</blockquote>
<p>To build our neural network I’m going to be using the Keras machine learning interface (which we’re very excited about here at Mango right now – keep an eye out for workshops in the near future!). I’ve largely followed the steps in <a rel="nofollow" href="https://keras.rstudio.com/articles/examples/lstm_text_generation.html" target="_blank">this example</a> from the Keras for R website, and I’m going to stick to a high-level description of what’s going on, but if you’re the sort of person who would rather dive head-first into the code, don’t feel like you have to hang around here – go ahead and have a play! And if you want to read more about RNNs, <a rel="nofollow" href="http://karpathy.github.io/2015/05/21/rnn-effectiveness/" target="_blank">this excellent post</a> by Andrej Kaparthy is at least as entertaining and significantly more informative than the one you’re currently reading.</p>
<p>We start by scraping as many love song lyrics as possible from the web – these will form our training material. Here’s the sort of thing we’re talking about:</p>
<p><img alt="" sizes="100vw" src="https://i2.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/155.love-machine-automating-the-romantic-songwriting-process/lyr0.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Well… that’s how they look to us. Actually, after a bit of preprocessing, the computer sees something more like this:</p>
<p><img alt="" sizes="100vw" src="https://i1.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/155.love-machine-automating-the-romantic-songwriting-process/lyr1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>All line breaks are represented by the pair of characters “\n”, and so all the lyrics from all the songs are squashed down into one big long string.</p>
<p>Then we use this string to train the network. We show the network a section of the string, and tell it what comes next.</p>
<p><img alt="" sizes="100vw" src="https://i1.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/155.love-machine-automating-the-romantic-songwriting-process/lyr2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>So the network gradually learns which characters tend to follow a given fixed-length “sentence”. The more of these what-comes-next examples it sees, the better it gets at correctly guessing what should follow any sentence we feed in.</p>
<p>At this point, our network is like a loyal student of a great artist, dutifully copying every brushstroke in minuscule detail and receiving a slap on the wrist and a barked correction every time it slips up. Via this process it appears to have done two things.</p>
<p>Firstly, it seems to have developed an understanding of the “rules” of writing a song. These rules are complex and multi-levelled; the network first had to learn the rules of English spelling and grammar, before it could start to make decisions about when to move to a new line or which rhyming pattern to use.</p>
<p>(Of course, it hasn’t actually “developed an understanding” of these rules. It has no idea what a “word” is, or a “new line”. It just knows that every few characters it should guess <code>&quot; &quot;</code>, and then sometimes it should put in a <code>&quot;\&quot;</code>, and whenever it puts in a <code>&quot;\&quot;</code> then it’s got to follow that up with a <code>&quot;n&quot;</code> and then immediately a capital letter. Easy peasy.)</p>
<p>Secondly, and in exactly the same way, the network will have picked up some of the style of the work it is copying. If we were training it on the songs one specific artist, it would have learned to imitate the style of that particular artist – but we’ve gone one better than that and trained it on all the love songs we could find. So effectively, it’s learned how <em>everyone else</em> writes love songs.</p>
<p>But no-one gets famous by writing songs which have already been written. What we need now is some creativity, some passion, a little bit of <em>je ne sais quoi</em>.</p>
<p>Let’s stop telling our network what comes next. Let’s give it the freedom to write whatever it likes.</p>
<blockquote>
<p>I don’t think you can ever do your best. Doing your best is a process of trying to do your best.</p>
<p><em>— Townes van Zandt</em></p>
</blockquote>
<p>It’s interesting to look at the songwriting attempts of the network in the very early stages of training. At first, it is guessing more or less at random what character should come next, so we end up with semi-structured gobbledegook:</p>
<pre><code>fameliawmalYaws. Boflyi, methabeethirts yt
play3mppioty2=ytrnfuunuiYs blllstis
Byyovcecrowth andtpazo's youltpuduc,s Ijd&quot;a]bemob8b>fiume,;Co
Bliovlkfrenuyione (ju'te,'ve ru t Kis
go arLUUs,k'CaufkfR )s'xCvectdvoldes

4So
Avanrvous Ist'dyMe Dolriri</code></pre>
<p>But notice that even in that example, which was taken from a very early training stage, the network has already nailed the “\n” newline combo and has even started to pick up on other consistent structural patterns like closing a “(” with a “)”. Actually, the jumbled nonsense becomes coherent English (or English-esque) ramblings quite quickly.</p>
<p>There is one interesting parameter to adjust when we ask the model to produce some output: the “diversity” parameter, which determines how adventurous the network should be in its choice of character. The higher we set this parameter, the more the network will favour slightly-less-probable characters over the most obvious choice at each point.</p>
<p>If we set the diversity parameter too low, we often degenerate into uncontrolled bursts of la-ing:</p>
<pre><code>la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la
la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la
la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la
(... lots more &quot;la&quot;s)</code></pre>
<p>But set it too high and the network decides dictionary English is too limiting.</p>
<pre><code>Oh, this younan every, drock on
Scridh's tty'
Is go only ealled
You could have like the one don'm I dope
Love me
And woment while you all that
Was it statiinc. I living you must?
We dirls anythor</code></pre>
<p>It’s difficult to find the right balance between syllabic repetition and progressive vocabulary, and there’s a surprisingly fine line between the two – this will probably prove to be a fruitful area for further academic research.</p>
<p>I think that identifying the optimal diversity parameter is probably the key to good songwriting.</p>
<blockquote>
<p>Songwriting is like editing. You write down all this stuff – all this bad, stupid stuff – and then you have to get rid of everything except the very best.</p>
<p><em>— Juliana Hatfield</em></p>
</blockquote>
<p>So, that’s what I did. </p>
<p>Here are some particularly “beautiful” passages taken from the huge amount of (largely poor) material the model produced. I haven’t done any editing other than to isolate a few consecutive lines at a time and in the last few examples, to start the network off with certain sentences. </p>
<p><strong>Automated love</strong></p>
<p>I know your eyes in the morning sun<br>
I feel the name of love<br>
Love is a picked the sun<br>
All my life I can make me wanna be with you<br>
I just give up in your head<br>
And I can stay that you want a life<br>
I’ve stay the more than I do</p>
<p>How long will I love you<br>
As long as there is that songs<br>
All the things that you want to find you<br>
I could say me true<br>
I want to fall in love with you<br>
I want my life<br>
And you’re so sweet<br>
When I see you wanted to that for you<br>
I can see you and thing, baby<br>
I wanna be alone</p>
<p>Oh yeah I tell you somethin’<br>
I think you’ll understand<br>
When I say that somethin’<br>
I thought the dartion hyand<br>
I want me way to hear<br>
All the things what you do</p>
<p>Wise men say<br>
Only fools rush in<br>
But I can hear your love<br>
And I don’t wanna be alone</p>
<p>If I should stay<br>
I would only be in your head<br>
I wanna know that I hope I see the sun<br>
I want a best there for me too<br>
I just see that I can have beautiful<br>
So hold me to you</p>
<p>Wishing you a Happy Valentine’s Day! (And, I don’t recommend reciting this to your loved one, they might run away.)</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.mango-solutions.com/blog/love-machine-automating-the-romantic-songwriting-process"> Mango Solutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/vJhHH05WpXE" height="1"></a></div>