{"id":23115,"date":"2021-11-18T17:36:18","date_gmt":"2021-11-18T17:36:18","guid":{"rendered":"https:\/\/www.askpython.com\/?p=23115"},"modified":"2023-02-16T19:56:47","modified_gmt":"2023-02-16T19:56:47","slug":"predict-shakespearean-text","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/predict-shakespearean-text","title":{"rendered":"Predict Shakespearean Text Using Keras TensorFlow"},"content":{"rendered":"\n<p>Hey folks! In this tutorial, we will look at how to use the Keras TensorFlow API in Python to create a Recurrent Neural Network model to predict Shakespearean text.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python\/examples\/stock-price-prediction-python\" data-type=\"post\" data-id=\"18944\">Stock Price Prediction using Python<\/a><\/em><\/strong><\/p>\n\n\n\n<p>To produce fresh text, we will train the <strong><a href=\"https:\/\/raw.githubusercontent.com\/karpathy\/char-rnn\/master\/data\/tinyshakespeare\/input.txt\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub Shakespearean text dataset<\/a><\/strong> using a custom-built RNN model.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 1: Importing Libraries<\/strong><\/h2>\n\n\n\n<p>We utilized some of the most popular deep learning libraries. Sweetviz is a new package that automates exploratory data analysis and is particularly beneficial in analyzing our training dataset.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\npip install sweetviz\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nimport sweetviz as sw\nimport seaborn as sns\nsns.set()\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 2: Loading the Dataset<\/strong><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nshakespeare_url=&#039;https:\/\/raw.githubusercontent.com\/karpathy\/char-rnn\/master\/data\/tinyshakespeare\/input.txt&#039;\nfilepath=keras.utils.get_file(&#039;shakespeare.txt&#039;,shakespeare_url)\nwith open(filepath) as f:\n    shakespeare_text=f.read()\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nDownloading data from https:\/\/raw.githubusercontent.com\/karpathy\/char-rnn\/master\/data\/tinyshakespeare\/input.txt\n1122304\/1115394 &#x5B;==============================] - 0s 0us\/step\n1130496\/1115394 &#x5B;==============================] - 0s 0us\/step\n<\/pre><\/div>\n\n\n<p>Now that we&#8217;ve downloaded the dataset into our Python notebook, we need to preprocess it before we can utilize it for training.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 3: Pre-processing the Dataset<\/strong><\/h2>\n\n\n\n<p>Tokenisation is the process of dividing lengthy text strings into smaller portions or tokens. Larger chunks of text can be tokenized into sentences, and then into words.<\/p>\n\n\n\n<p>Pre-processing will also involve removing punctuations from the tokens generates as well.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ntokenizer=keras.preprocessing.text.Tokenizer(char_level=True)\ntokenizer.fit_on_texts(shakespeare_text)\n\nmax_id=len(tokenizer.word_index)\ndataset_size=tokenizer.document_count\n&#x5B;encoded]=np.array(tokenizer.texts_to_sequences(&#x5B;shakespeare_text]))-1\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 4: Preparing the Dataset<\/strong><\/h2>\n\n\n\n<p>We will be using&nbsp;<code>tf.data.Dataset<\/code>&nbsp;which is generally useful for a large set of elements like huge chunks of textual data.<\/p>\n\n\n\n<p><code>Dataset.repeat()<\/code>&nbsp;goes over the dataset and repeats the dataset a specified number of times.&nbsp;<code>window()<\/code>&nbsp;is like a sliding window that slides the window by a specified number each time for repeated iteration.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ntrain_size=dataset_size*90\/\/100\ndataset=tf.data.Dataset.from_tensor_slices(encoded&#x5B;:train_size])\n\nn_steps=100\nwindow_length=n_steps+1\ndataset=dataset.repeat().window(window_length,shift=1,drop_remainder=True)\n\ndataset=dataset.flat_map(lambda window: window.batch(window_length))\n\nbatch_size=32\ndataset=dataset.shuffle(10000).batch(batch_size)\ndataset=dataset.map(lambda windows: (windows&#x5B;:,:-1],windows&#x5B;:,1:]))\ndataset=dataset.map(lambda X_batch,Y_batch: (tf.one_hot(X_batch,depth=max_id),Y_batch))\ndataset=dataset.prefetch(1)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 5: Building the Model<\/strong><\/h2>\n\n\n\n<p>The model building is pretty simple. We will be creating a sequential model and adding layers to the model with certain characteristics.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nmodel=keras.models.Sequential()\nmodel.add(keras.layers.GRU(128,return_sequences=True,input_shape=&#x5B;None,max_id]))\nmodel.add(keras.layers.GRU(128,return_sequences=True))\nmodel.add(keras.layers.TimeDistributed(keras.layers.Dense(max_id,activation=&#039;softmax&#039;)))\n<\/pre><\/div>\n\n\n<p>Next, we will be compiling the model and fitting the model on the dataset. We will be using&nbsp;<code>Adam<\/code>&nbsp;optimizer but you can also use other available optimizers according to your preferences.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nmodel.compile(loss=&#039;sparse_categorical_crossentropy&#039;,optimizer=&#039;adam&#039;)\nhistory=model.fit(dataset,steps_per_epoch=train_size \/\/ batch_size,epochs=1)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n31370\/31370 &#x5B;==============================] - 1598s 51ms\/step - loss: 0.9528\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 6: Testing the Model<\/strong><\/h2>\n\n\n\n<p>We have defined some functions in the code snippet mentioned below. The functions will preprocess and prepare the input data according to our defined model and predict the next characters up to the specified number of characters.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ndef preprocess(texts):\n    X=np.array(tokenizer.texts_to_sequences(texts))-1\n    return tf.one_hot(X,max_id)\n\ndef next_char(text,temperature=1):\n    X_new=preprocess(&#x5B;text])\n    y_proba=model.predict(X_new)&#x5B;0,-1:,:]\n    rescaled_logits=tf.math.log(y_proba)\/temperature\n    char_id=tf.random.categorical(rescaled_logits,num_samples=1)+1\n    return tokenizer.sequences_to_texts(char_id.numpy())&#x5B;0]\n\ndef complete_text(text,n_chars=50,temperature=1):\n    for _ in range(n_chars):\n        text+=next_char(text,temperature)\n    return text\n<\/pre><\/div>\n\n\n<p>Let&#8217;s predict the text for a certain letter or a word using the code mentioned below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nprint(&quot;Some predicted texts for letter &#039;D&#039; are as follows:\\n &quot;)\nfor i in range(3):\n  print(complete_text(&#039;d&#039;))\n  print()\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSome predicted texts for letter &#039;D&#039; are as follows:\n \nd, swalld tell you in mine,\nthe remeiviss if i shou\n\ndima&#039;s for me, sir, to comes what this roguty.\n\ndening to girl, ne&#039;er i was deckong?\nwhich never be\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nprint(&quot;Some predicted texts for word &#039;SHINE&#039; are as follows:\\n &quot;)\nfor i in range(3):\n  print(complete_text(&#039;shine&#039;))\n  print()\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSome predicted texts for word &#039;SHINE&#039; are as follows:\n \nshine on here is your viririno penaite the cursue,\ni&#039;ll\n\nshine yet it the become done to-k\nmake you his ocrowing\n\nshine dises&#039;-leck a word or my head\nnot oning,\nso long \n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p id=\"block-d144faf1-1abb-488c-a4bd-8a80520fbf7b\">Congratulations! You just learned how to build a Shakespearean text predictor using RNN. Hope you enjoyed it! &#x1f607;<\/p>\n\n\n\n<p id=\"block-4e73ecff-f7fa-46a5-be6e-35bbb0867ba8\">Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:<\/p>\n\n\n\n<ol class=\"wp-block-list\" id=\"block-39ec4c2c-ab54-45a6-804e-58eb24573b45\"><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/classifying-clothing-images\"><\/a><a href=\"https:\/\/www.askpython.com\/python\/examples\/stock-price-prediction-python\">Stock Price Prediction using Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/crypto-price-prediction\">Crypto Price Prediction with Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/stock-price-prediction-python\">Stock Price Prediction using Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/box-office-revenue-prediction\">Box Office Revenue Prediction in Python \u2013 An Easy Implementation<\/a><\/li><\/ol>\n\n\n\n<p id=\"block-177e4b87-db13-427b-90fa-d70aac750e1d\">Thank you for taking your time out! Hope you learned something new!! &#x1f604;<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Hey folks! In this tutorial, we will look at how to use the Keras TensorFlow API in Python to create a Recurrent Neural Network model to predict Shakespearean text. Also read: Stock Price Prediction using Python To produce fresh text, we will train the GitHub Shakespearean text dataset using a custom-built RNN model. Step 1: [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":23712,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-23115","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/23115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=23115"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/23115\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/23712"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=23115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=23115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=23115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}