{"id":5010,"date":"2017-04-14T13:27:45","date_gmt":"2017-04-14T18:27:45","guid":{"rendered":"https:\/\/blog.lib.uiowa.edu\/studio\/?p=5010"},"modified":"2018-10-31T13:50:16","modified_gmt":"2018-10-31T18:50:16","slug":"neural-network-poetry","status":"publish","type":"post","link":"https:\/\/blog.lib.uiowa.edu\/studio\/2017\/04\/14\/neural-network-poetry\/","title":{"rendered":"Neural Network Poetry"},"content":{"rendered":"<figure id=\"attachment_5011\" aria-describedby=\"caption-attachment-5011\" style=\"width: 300px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/blog.lib.uiowa.edu\/studio\/files\/2017\/04\/writing.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-5011\" src=\"https:\/\/blog.lib.uiowa.edu\/studio\/files\/2017\/04\/writing-300x152.png\" alt=\"\" width=\"300\" height=\"152\" srcset=\"https:\/\/blog.lib.uiowa.edu\/studio\/files\/2017\/04\/writing-300x152.png 300w, https:\/\/blog.lib.uiowa.edu\/studio\/files\/2017\/04\/writing-768x389.png 768w, https:\/\/blog.lib.uiowa.edu\/studio\/files\/2017\/04\/writing.png 846w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-5011\" class=\"wp-caption-text\">An Iowa writer hard at work<\/figcaption><\/figure>\n<p>As you may know, April is <a href=\"https:\/\/www.poets.org\/national-poetry-month\/home\">national poetry month<\/a>, an annual series of events by the Academy of American Poets to help support the appreciation of American poetry. If you&#8217;re looking for great book-length collections of poems, you might be interested in the <a href=\"https:\/\/www.uipress.uiowa.edu\/search\/browse-series\/browse-IPP.htm\">Iowa Poetry Prize winners<\/a>. Many of the previous years&#8217; winners are <a href=\"http:\/\/ir.uiowa.edu\/uipress_ipp\/\">made available in PDF form<\/a> at Iowa Research Online. What you may not know is that April is also <a href=\"https:\/\/github.com\/NaPoGenMo\/NaPoGenMo2017\">National Poetry Generation Month<\/a>, an annual tradition where programmers and creative coders spend the month writing code that generates poetry.<\/p>\n<p>In honor of this time of year, I thought I&#8217;d take a look at the Iowa Poetry Prize winners through code.\u00a0There are many methods for analyzing and generating natural language, but one system that has received a lot of attention recently is <a href=\"https:\/\/en.wikipedia.org\/wiki\/Artificial_neural_network\">neural networks<\/a>. A neural network is a large collection of artificial neurons based very loosely on a biological brain. These neurons exist in layers that perform statistical calculations and affect the state of other connected neurons. It differs from other computational models in that there is no knowledge hard coded and controlled by elaborate conditional statements (if <em>this<\/em> then <em>that<\/em>). Rather, neural networks learn to solve tasks by observing data and producing optimal functions that will produce similar outputs given new data it&#8217;s never seen before. The uses for such a system include image and speech recognition, classification problems, and many forms of prediction and decision making. For example, a neural net could be <em>trained<\/em> to detect images of cats\u00a0by observing tens of thousands of labeled images of\u00a0cats. Google has recently <a href=\"https:\/\/www.autodraw.com\/\">launched a new project<\/a> that uses this technique to match your doodles with professional drawings.<\/p>\n<p>What happens when we train an artificial intelligence to write english language having only read Iowa Poetry Prize winners? Let&#8217;s find out!<\/p>\n<p>To start, I downloaded all of the IPP winners from Iowa Research Online, extracted the poems as plain text, and concatenated them all into a single text file named poems.txt. This served as the training set. Next, I set up this <a href=\"http:\/\/torch.ch\/\">Torch<\/a>-based <a href=\"https:\/\/hub.docker.com\/r\/crisbal\/torch-rnn\/\">Docker container<\/a>\u00a0implementation of a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Recurrent_neural_network\">recurrent neural network<\/a> based on work by <a href=\"http:\/\/cs.stanford.edu\/people\/karpathy\/\">Andrej Karpathy<\/a> and <a href=\"http:\/\/cs.stanford.edu\/people\/jcjohns\/\">Justin Johnson<\/a>. It was tempting to spin up the Google cloud\u00a0VM with an attached GPU, since these types of machine learning tasks are sped up greatly running on a graphics processing unit with <a href=\"https:\/\/developer.nvidia.com\/about-cuda\">CUDA<\/a>, but it&#8217;s also quite expensive at 75 cents-per-hour. Once I had it working, I started the preprocessing and training, which took about 16 hours to complete.<\/p>\n<p>After a lot of experimentation to create some useful training models and keep the network from <a href=\"https:\/\/en.wikipedia.org\/wiki\/Overfitting\">overfitting and underfitting<\/a> the data, I had something that was acceptable and so began sampling output. One parameter of sampling that was fun to play with was the &#8220;temperature&#8221; of the sample. A lower temperature produced output that was much more predictable and less error prone while a higher temperature was much more inventive but riddled with mistakes. I decided to split the difference and start at 0.5. Here&#8217;s the first poem.<\/p>\n<p style=\"text-align: center\">Speritas Of The Stars<\/p>\n<p style=\"text-align: center\">Morning comes of the sun<br \/>\nto the thin world is a star of her light.<\/p>\n<p style=\"text-align: center\">The sheet and the body of parts<br \/>\nof the flame is a light, the body<br \/>\nsees of the wars beautiful on the street.<br \/>\nThe sun, the stars of the sound, and desire,<br \/>\nand a man could love the streets.<\/p>\n<p style=\"text-align: center\">The single shiller of light,<br \/>\nand the single stranger falls countal.<br \/>\nFather and she were the sutters of the body<br \/>\ninstraining to the complete<br \/>\nwindow of light, still.<\/p>\n<p style=\"text-align: left\">You&#8217;ll notice a few words in this poem that don&#8217;t actually exist in english. That&#8217;s because this RNN operates at the character level, not the word level. It has to learn, from scratch, how to write english. It starts with random strings of letters and slowly, after many iterations, learns about spaces, proper punctuation, and finally readable words. The higher the sampling temperature, the more invented words. Let&#8217;s look at a &#8220;hot&#8221; poem.<\/p>\n<p style=\"text-align: center\">Pelies, One Yighter<\/p>\n<p style=\"text-align: center\">The shadows just plance croved<br \/>\nI am one<br \/>\nits funlet from the wind<br \/>\nstaskaccus, gring of detches of hearts face eashog<br \/>\nwhat wing to the streed in the resert of change, a glince<br \/>\nthe life.<br \/>\nShe read on his fill bathered, a hand the<br \/>\nmarks<br \/>\nwith beautiful, casty, stery, kooms, in one father<\/p>\n<p style=\"text-align: center\">something the mouth cold leaves.<br \/>\nA night and no one is a woman; you green her<\/p>\n<p style=\"text-align: center\">My spere would must not the look teering mower<br \/>\nI see itselfor.<br \/>\nAt that sign they thought the remelled the mum,<br \/>\nbut like an wait they mite of ammiral<br \/>\nafter things of the body<br \/>\nwhich children would love<br \/>\nnow, not<br \/>\nthe forest flowers and hark a path.<br \/>\nThe shawr rate in a ruched parts in humstily<br \/>\nhis poom her as of the trabs conterlity.<\/p>\n<p style=\"text-align: left\">Much more Jabberwockyesque. If we ease up just a little on this we get<\/p>\n<p style=\"text-align: center\">A Badicar Flower<\/p>\n<p style=\"text-align: center\">The watcher blue says<br \/>\nthey would have shapes,<br \/>\nthe night dreaming,<br \/>\na painted nother<br \/>\ntricks me, the wind,<br \/>\nthe dayed from the boging feeling<br \/>\nof the histance in his everyness.<\/p>\n<p style=\"text-align: left\">What do you think &#8212; poetry prize worthy? While writing poetry is fun, there are, of course, practical applications too. I&#8217;m currently working with faculty member\u00a0<a href=\"https:\/\/clas.uiowa.edu\/history\/people\/mariola-espinosa\">Mariola Espinosa<\/a>\u00a0on a HathiTrust\u00a0project called\u00a0<a href=\"https:\/\/www.hathitrust.org\/hathitrust-research-center-awards-three-acs-projects\">Fighting Fever in the Caribbean: Medicine and Empire, 1650-1902<\/a>. We have 9.3 million pages of medical journals and need to find references to yellow fever in multiple languages. A trained neural network could look through these quickly and find references that a human might miss. I&#8217;m also working on another project with\u00a0<a href=\"https:\/\/english.uiowa.edu\/people\/heidi-renee-aijala\">Heidi Renee Aijala<\/a> looking for references to coal smoke in Victorian literature. Perhaps a neural net could be trained to look for non-keyword references.<\/p>\n<p style=\"text-align: left\">While I&#8217;m probably not going to put a poet out of work any time soon, you can imagine many real-world uses. There is a tremendous potential for neural networks and other types of machine learning to caption images, transcribe handwriting, translate\u00a0documents, understand the spoken word, and <a href=\"https:\/\/www.technologyreview.com\/s\/541276\/deep-learning-machine-teaches-itself-chess-in-72-hours-plays-at-international-master\/\">play chess at the international master level<\/a>. Perhaps someday it might also <a href=\"https:\/\/www.theguardian.com\/technology\/2016\/may\/17\/googles-ai-write-poetry-stark-dramatic-vogons\">write a meaningful poem<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you may know, April is national poetry month, an annual series of events by the Academy of American Poets to help support the appreciation of American poetry. If you&#8217;re looking for great book-length collections of poems, you might be interested in the Iowa Poetry Prize winners. Many of the previous years&#8217; winners are made<a class=\"more-link\" href=\"https:\/\/blog.lib.uiowa.edu\/studio\/2017\/04\/14\/neural-network-poetry\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Neural Network Poetry&#8221;<\/span><\/a><\/p>\n","protected":false},"author":153,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"syndication":[],"_links":{"self":[{"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/posts\/5010"}],"collection":[{"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/users\/153"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/comments?post=5010"}],"version-history":[{"count":17,"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/posts\/5010\/revisions"}],"predecessor-version":[{"id":5032,"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/posts\/5010\/revisions\/5032"}],"wp:attachment":[{"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/media?parent=5010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/categories?post=5010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/tags?post=5010"},{"taxonomy":"syndication","embeddable":true,"href":"https:\/\/blog.lib.uiowa.edu\/studio\/wp-json\/wp\/v2\/syndication?post=5010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}