Created
September 17, 2017 10:59
-
-
Save lirnli/c3efe5104d9fc0832c9208f947d18862 to your computer and use it in GitHub Desktop.
tiny Shakespeare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Download and encode txt data\n", | |
"One way is to use ASCII table to encode characters. But a more general approach is to build a custom symbol table to map characters to integers. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"n_input = 100\n", | |
"corpus sample: First Citizen:\n", | |
"Before we proceed any further, hear me speak.\n", | |
"\n", | |
"All:\n", | |
"Speak, speak.\n", | |
"\n", | |
"First Citizen:\n", | |
"You\n", | |
"corpus_tab sample: [41, 18, 27, 28, 29, 94, 38, 18, 29, 18, 35, 14, 23, 77, 96, 37, 14, 15, 24, 27, 14, 94, 32, 14, 94, 25, 27, 24, 12, 14, 14, 13, 94, 10, 23, 34, 94, 15, 30, 27, 29, 17, 14, 27, 73, 94, 17, 14, 10, 27, 94, 22, 14, 94, 28, 25, 14, 10, 20, 75, 96, 96, 36, 21, 21, 77, 96, 54, 25, 14, 10, 20, 73, 94, 28, 25, 14, 10, 20, 75, 96, 96, 41, 18, 27, 28, 29, 94, 38, 18, 29, 18, 35, 14, 23, 77, 96, 60, 24, 30]\n" | |
] | |
} | |
], | |
"source": [ | |
"import unicodedata\n", | |
"import string\n", | |
"from six.moves import urllib\n", | |
"\n", | |
"all_characters = string.printable\n", | |
"n_input = len(all_characters)\n", | |
"char2tab = {char:tab for tab, char in enumerate(all_characters)}\n", | |
"tab2char = {tab:char for tab, char in enumerate(all_characters)}\n", | |
"\n", | |
"url ='https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt'\n", | |
"http_response = urllib.request.urlopen(url).read()\n", | |
"corpus = http_response.decode()\n", | |
"\n", | |
"corpus = unicodedata.normalize(\"NFD\", corpus) \n", | |
"corpus_tab = [char2tab.get(c) for c in corpus]\n", | |
"\n", | |
"print('n_input = ', n_input)\n", | |
"print('corpus sample: ', corpus[:100])\n", | |
"print('corpus_tab sample:', corpus_tab[:100])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Neural Network Model\n", | |
"\n", | |
"The *evaluate( )* function is used to generate new random texts. When generating new texts, a new character is randomly picked with respect to softmax probabilities. The *temperature* parameter is introduced to adjust the probabilities. \n", | |
"- (Deterministic) When *temperature* is close to zero, it is equivalent to pick strictly the maximum possibility.\n", | |
"- (Stochastic) When *temperature* is much larger than one, it is equivalent to pick completely randomly.\n", | |
"\n", | |
"I find that when a small temperature is used, the net tends to generate repeating short phrases." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import torch\n", | |
"from torch import nn, optim\n", | |
"from torch.autograd import Variable" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"RNN (\n", | |
" (embedding): Embedding(100, 100)\n", | |
" (rnn): GRU(100, 128, num_layers=2)\n", | |
" (logits): Linear (128 -> 100)\n", | |
")\n" | |
] | |
} | |
], | |
"source": [ | |
"class RNN(nn.Module):\n", | |
" def __init__(self):\n", | |
" super(RNN, self).__init__()\n", | |
" self.embedding = nn.Embedding(n_input,100)\n", | |
" self.rnn = nn.GRU(100,128,2)\n", | |
" self.logits = nn.Linear(128,n_input)\n", | |
" def forward(self, input, hidden=None):\n", | |
" output = input.view(-1)\n", | |
" output = self.embedding(output).unsqueeze(1)\n", | |
" output, hidden = self.rnn(output,hidden)\n", | |
" output = output.squeeze(1)\n", | |
" output = self.logits(output)\n", | |
" return output, hidden\n", | |
" def evaluate(self, warmup, hidden=None, n=100, temperature = 0.8):\n", | |
" output, hidden = self(warmup, hidden)\n", | |
" x = output[-1]\n", | |
" res = []\n", | |
" for i in range(n):\n", | |
" x_max = x.view(-1).div(temperature).exp().multinomial(1)\n", | |
" res.append(x_max.data[0])\n", | |
" x, hidden = self.forward(x_max, hidden)\n", | |
" return res\n", | |
"\n", | |
"print(RNN())" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*decode( )* is a convinient function to convert symbol table back to english text." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def decode(seq):\n", | |
" res = [tab2char[x] for x in seq]\n", | |
" return \"\".join(res)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Model training" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import random\n", | |
"net = RNN()\n", | |
"criterion = nn.CrossEntropyLoss()\n", | |
"optimizer = optim.Adam(net.parameters(), lr = 0.001)\n", | |
"batch_size = 128\n", | |
"\n", | |
"batches = torch.LongTensor(corpus_tab).split(batch_size)\n", | |
"batches = batches[:-1] # remove the last batch, which might be too short." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
">> iteration 0, loss 4.599607944488525\n", | |
"\n", | |
"j4szO<Sg\f", | |
"`0T1B$c4dWPIf,?#b'hsH:*_mnh5?P:&Yx)}+7`|VB6B^%;0TihoH**`C$ e8!u8594ZmJ'|o84)EAK)>N>/A$7o}+\n", | |
"\n", | |
">> iteration 100, loss 2.6262011528015137\n", | |
".\n", | |
"%Re ors sots mCe oe poe \n", | |
"osthd fhan moe, fou tare widnd tgoared hcmanc foy to.\n", | |
"o' lhe haln.\n", | |
"k acf \n", | |
"\n", | |
">> iteration 200, loss 2.513347864151001\n", | |
"reey bee motow the, I osor buteam peagh wite luras theat what ind is oly Yrouu sy wor ous\n", | |
"'malk way \n", | |
"\n", | |
">> iteration 300, loss 2.3067784309387207\n", | |
"en thes thont this frecd bothes cutir, oh mear the my a heal\n", | |
"\n", | |
"'YETTI:\n", | |
"Wo you in gortatree,\n", | |
"And an st\n", | |
"\n", | |
">> iteration 400, loss 2.0262978076934814\n", | |
"t mise arlought saidelk\n", | |
"Toul,\n", | |
"Kame ning thariens kang 's in forte wilt wis she ach.\n", | |
"\n", | |
"UNONO IIO:\n", | |
"No o\n", | |
"\n", | |
">> iteration 500, loss 1.8533573150634766\n", | |
"re, havest is my thee will my hall wered, my to you that shed of and of in a and wo thot it and the \n", | |
"\n", | |
">> iteration 600, loss 2.1954147815704346\n", | |
" end of to by and to not be dost Tupct you hought and bipe'd for the the rasturtult a ary ad qath\n", | |
"An\n", | |
"\n", | |
">> iteration 700, loss 1.8252092599868774\n", | |
"rais the deester:\n", | |
"Shath he was of miver\n", | |
"Thoue of wing whild I dima? moother sood my lidiaver not ent\n", | |
"\n", | |
">> iteration 800, loss 2.3801426887512207\n", | |
"s?\n", | |
"\n", | |
"IO ING IINUS:\n", | |
"Gold, send that the ard.\n", | |
"\n", | |
"HROTIO:\n", | |
"He, is sharn the the by hearnt ont in mut,\n", | |
"On an\n", | |
"\n", | |
">> iteration 900, loss 1.868074893951416\n", | |
"nigrapoe\n", | |
"The mears, lord be coddere haw mirsor.\n", | |
"\n", | |
"INIG:\n", | |
"Is my with the I sange,\n", | |
"What lith fing thou s\n", | |
"\n", | |
">> iteration 1000, loss 1.775469183921814\n", | |
" Whorsly beetings laked you be the hald, in her of his will hom what in have of thy be shall me from\n", | |
"\n", | |
">> iteration 1100, loss 1.8824411630630493\n", | |
"ay not ose, gont's and thy doth come dedorling listome untuldor this but more, you both aed betory t\n", | |
"\n", | |
">> iteration 1200, loss 1.810351848602295\n", | |
"ear then, that a do\n", | |
"Whis of with me crack's stone in to sole beatile tonfing will bothe's the sless \n", | |
"\n", | |
">> iteration 1300, loss 2.1884472370147705\n", | |
"ar,\n", | |
"So crecony, warrly bide to shish our frow makound and aplarnt mastlated with wills to do rest so\n", | |
"\n", | |
">> iteration 1400, loss 1.7758312225341797\n", | |
"r to a must\n", | |
"The livesent in his nown, and be timings baster?\n", | |
"\n", | |
"CORICOMING IO: O my your the a good c\n", | |
"\n", | |
">> iteration 1500, loss 1.670606017112732\n", | |
"ad you world highs and me, good hall the well with say soglery shand of may you, thom's somes worch \n", | |
"\n", | |
">> iteration 1600, loss 1.8913791179656982\n", | |
", then serveor prash in and the forder,\n", | |
"Shad, the epon that shall a pure it it\n", | |
"Tis would dyen stried\n", | |
"\n", | |
">> iteration 1700, loss 1.8667945861816406\n", | |
"nds.\n", | |
"\n", | |
"ORBOFAAS:\n", | |
"Here and me time is my louak ithers heavented when selving to make affece God avent.\n", | |
"\n", | |
">> iteration 1800, loss 1.817230463027954\n", | |
"GREOSTORA:\n", | |
"Leff the the stist:\n", | |
"If hove kise.\n", | |
"\n", | |
"GLOUCESTES:\n", | |
"O, you!\n", | |
"Go death thave the dig\n", | |
"And flacce\n", | |
"\n", | |
"\n", | |
">> iteration 1900, loss 1.8060367107391357\n", | |
"e with is eneld the preate, Coth the wine tent's hath would my preveral? for bilk,\n", | |
"To come, no sare,\n", | |
"\n", | |
">> iteration 2000, loss 1.9592806100845337\n", | |
" canding us of the deniece, thou shast but for and doith your to shure her your condend of; there yo\n", | |
"\n", | |
">> iteration 2100, loss 1.978979468345642\n", | |
"ou must a spentle time, let then eyn'd state I have, and that I am rearemy do be I hear, his lime I \n", | |
"\n", | |
">> iteration 2200, loss 1.6275146007537842\n", | |
"or shiem too see the sare of her out pring one fulse have suptly pray that stenswick.\n", | |
"\n", | |
"LULIO:\n", | |
"The pa\n", | |
"\n", | |
">> iteration 2300, loss 2.097935199737549\n", | |
" the may, let I meine keet everew death larder then agrace\n", | |
"I parset, and were then mildon.\n", | |
"And lord.\n", | |
"\n", | |
">> iteration 2400, loss 2.199026584625244\n", | |
",\n", | |
"Why chrath streak well rethers heart\n", | |
"Till dest on some men the great, Some dost to whose the peach\n", | |
"\n", | |
">> iteration 2500, loss 1.448434591293335\n", | |
"re to my lord, amaning. O, should his compare,\n", | |
"Onhe is so that but so brother shall,\n", | |
"If the pardant \n", | |
"\n", | |
">> iteration 2600, loss 1.7607046365737915\n", | |
"rself, our I our fous by besears of him? I have the come:\n", | |
"I down, not atting, the fathing the our pr\n", | |
"\n", | |
">> iteration 2700, loss 2.129148006439209\n", | |
"er come wlas that so, brochter.\n", | |
"\n", | |
"ROMERESTER:\n", | |
"H God were gone of Peries the come's gentlean's grace w\n", | |
"\n", | |
">> iteration 2800, loss 1.6758149862289429\n", | |
" my Lord we all come his seast your sould breatle hink of you the hear him\n", | |
"Thout amess hall\n", | |
"The have\n", | |
"\n", | |
">> iteration 2900, loss 1.4098724126815796\n", | |
"hou ip and friend\n", | |
"The is maltery's him thou dauge of auger thy hearth,\n", | |
"And what sidery, Of Lucil cap\n", | |
"\n", | |
">> iteration 3000, loss 1.6746625900268555\n", | |
" shall see offer there's but the margence they to,\n", | |
"Lide sair, my my king to tell a triegd of kings, \n", | |
"\n", | |
">> iteration 3100, loss 1.0673905611038208\n", | |
"ou titing suckers father.\n", | |
"\n", | |
"PETRUCHI:\n", | |
"What her know and benonos.\n", | |
"\n", | |
"ANDIO:\n", | |
"What will do'that the prest \n", | |
"\n", | |
">> iteration 3200, loss 1.8535510301589966\n", | |
":\n", | |
"Not be good a heard, my pation.\n", | |
"\n", | |
"SICSINIUS:\n", | |
"A fir all be so prospatned our do\n", | |
"Not how way the know\n", | |
"\n", | |
">> iteration 3300, loss 1.655726432800293\n", | |
"ays.\n", | |
"\n", | |
"KING EDWARD IO:\n", | |
"I'll good So from that fits fomedemir!\n", | |
"\n", | |
"VARIO:\n", | |
"In heart\n", | |
"Where dear the ends of\n", | |
"\n", | |
">> iteration 3400, loss 1.6821529865264893\n", | |
"t what the prayer.\n", | |
"\n", | |
"CEMLINES:\n", | |
"He bring hate the\n", | |
"I have pryst at them;\n", | |
"Are I trought or my scarmente \n", | |
"\n", | |
">> iteration 3500, loss 1.4316707849502563\n", | |
"s in colloud!\n", | |
"And is to dist-Mied and deever dient here,\n", | |
"Preceot to you of there's live an is did wi\n", | |
"\n", | |
">> iteration 3600, loss 1.7352709770202637\n", | |
"an now; whar tell made thy lord,\n", | |
"I band wellst a must and love ceeter'd myself\n", | |
"His resties thands al\n", | |
"\n", | |
">> iteration 3700, loss 2.042280912399292\n", | |
"rs:\n", | |
"This of and had old I wind riness you, surfesself.\n", | |
"Let I war. Which thine, good far of you serve\n", | |
"\n", | |
">> iteration 3800, loss 1.4871227741241455\n", | |
"aven clease, thou sloudsh abvany is here in beat and no ming thee be I wears tome the ore the streak\n", | |
"\n", | |
">> iteration 3900, loss 1.6458359956741333\n", | |
"esirs of God, go here, prituse for untend of\n", | |
"For his nearth thee I mastering and his are wous sister\n", | |
"\n", | |
">> iteration 4000, loss 1.6687238216400146\n", | |
" they sir,\n", | |
"I'll reviled.\n", | |
"\n", | |
"MENENIUS:\n", | |
"We shall being yet did natue courn one is presenty and so well.\n", | |
"\n", | |
"\n", | |
">> iteration 4100, loss 1.4892507791519165\n", | |
", thus she drow me good unstion.\n", | |
"\n", | |
"LAUTH:\n", | |
"I must unto her me, sir, council my can my struck of Green.\n", | |
"\n", | |
">> iteration 4200, loss 1.8225152492523193\n", | |
"ood ney liff thinks be our repect the is\n", | |
"If a bear the lordsher:\n", | |
"Where his words, away to me desires\n", | |
"\n", | |
">> iteration 4300, loss 1.6448373794555664\n", | |
"s by know to done may he? What was have may with we comford,\n", | |
"And sun my sorred to see so man, as so;\n", | |
"\n", | |
">> iteration 4400, loss 1.6661694049835205\n", | |
"d if haw has pleasing you.\n", | |
"\n", | |
"Secioply some to my grevent off. This crows our lie.\n", | |
"\n", | |
"GRUCIS:\n", | |
"My buch no\n", | |
"\n", | |
">> iteration 4500, loss 2.0030219554901123\n", | |
"goosteret of the gone,\n", | |
"My own this cause.\n", | |
"\n", | |
"PAULINIUS:\n", | |
"I so stronghers:\n", | |
"What thou have me in the stru\n", | |
"\n", | |
">> iteration 4600, loss 1.3772730827331543\n", | |
"ht there you fell that?\n", | |
"\n", | |
"DUKE OF AUTER:\n", | |
"God my least of the lawns\n", | |
"I cousin, and place the death, wil\n", | |
"\n", | |
">> iteration 4700, loss 1.6133774518966675\n", | |
"nd for loves, suble, now fair begars gave he will she parns my rawant: then end, I\n", | |
"And shen his unou\n", | |
"\n", | |
">> iteration 4800, loss 1.6192853450775146\n", | |
"ur capure\n", | |
"That have than I talk. To believe upon them that to the back be,\n", | |
"For here have in bite thi\n", | |
"\n", | |
">> iteration 4900, loss 1.8546178340911865\n", | |
" the\n", | |
"pray ome.\n", | |
"\n", | |
"AUSIOLANUS:\n", | |
"No be should which a like to hour fair forth a jergue ware to earther,\n", | |
"D\n", | |
"\n", | |
">> iteration 5000, loss 1.5237149000167847\n", | |
"and sun\n", | |
"And the cold'd surfess to confore to-dais\n", | |
"Treajed speak.\n", | |
"\n", | |
"GLOUCESTER:\n", | |
"Good from Edward, what\n", | |
"\n", | |
">> iteration 5100, loss 1.8401648998260498\n", | |
"istre trusters chappet,\n", | |
"And my spice the reneings of harw'st thus rouse:\n", | |
"Weyou thou couse\n", | |
"That that \n", | |
"\n" | |
] | |
}, | |
{ | |
"ename": "KeyboardInterrupt", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-6-887db6f72aef>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mlogits\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhidden\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnet\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbatch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mloss\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcriterion\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlogits\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mbatch\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mloss\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0moptimizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0miteration\u001b[0m\u001b[0;34m%\u001b[0m\u001b[0;36m100\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/torch/autograd/variable.py\u001b[0m in \u001b[0;36mbackward\u001b[0;34m(self, gradient, retain_graph, create_graph, retain_variables)\u001b[0m\n\u001b[1;32m 154\u001b[0m \u001b[0mVariable\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 155\u001b[0m \"\"\"\n\u001b[0;32m--> 156\u001b[0;31m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mautograd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgradient\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mretain_graph\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mretain_variables\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 157\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 158\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mregister_hook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhook\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/torch/autograd/__init__.py\u001b[0m in \u001b[0;36mbackward\u001b[0;34m(variables, grad_variables, retain_graph, create_graph, retain_variables)\u001b[0m\n\u001b[1;32m 96\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 97\u001b[0m Variable._execution_engine.run_backward(\n\u001b[0;32m---> 98\u001b[0;31m variables, grad_variables, retain_graph)\n\u001b[0m\u001b[1;32m 99\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 100\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mKeyboardInterrupt\u001b[0m: " | |
] | |
} | |
], | |
"source": [ | |
"max_iteration = 10000\n", | |
"for iteration in range(max_iteration):\n", | |
" batch = Variable(random.choice(batches))\n", | |
" optimizer.zero_grad()\n", | |
" logits, hidden = net(batch)\n", | |
" loss = criterion(logits[:-1],batch[1:])\n", | |
" loss.backward()\n", | |
" optimizer.step()\n", | |
" if iteration%100 == 0:\n", | |
" print('>> iteration {}, loss {}'.format(iteration, loss.data[0]))\n", | |
" tmp = net.evaluate(batch)\n", | |
" print(decode(tmp))\n", | |
" print()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Evaluation/ Text Generation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"KING EDWARD:\n", | |
"No must kings not barged as a worson of go, and seemh, where no beggard to the way,\n", | |
"And trived, as thy first the pack'd in thy self, but see it; but look,\n", | |
"Or heer me gave mern a miside his heart of the tale\n", | |
"Thee no juck. With should prausunied defent now.\n", | |
"\n", | |
"BUCKINGHARD III:\n", | |
"No then, the as woith thee in honourmbed to lived my sake shones,' with,\n", | |
"To blessenge the father first that son of down done, vice's met,\n", | |
"Usent nor be thing no prostince\n", | |
"Coneman end in his then no more,\n", | |
"Which kind, may been one a procome, my besingle one arm.\n", | |
"\n", | |
"HASTINGS:\n", | |
"And should this death of shalt of Warwick,\n", | |
"Which: rime him a till did, with the lated in the viatent I more a flower with and wear and firdatch,\n", | |
"For Fore bance in the sheel my sun be exaulter God.\n", | |
"\n", | |
"Prom the father for be presing, the vicking oner,\n", | |
"First Murderer of wall the stay! I would driver:\n", | |
"And maning of murderion his ward not I may neit's and diserford\n", | |
"To be wide though him a presented begnard: with 't faince, a good our look the v\n" | |
] | |
} | |
], | |
"source": [ | |
"warmup = \"Siri, who is Shakespeare?\\n\"\n", | |
"warmup = Variable(torch.LongTensor([char2tab[c] for c in warmup]))\n", | |
"res = net.evaluate(warmup, n=1000)\n", | |
"res = decode(res)\n", | |
"print(res)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Some thoughts\n", | |
"- RNN is like a N-gram model but on the charater-level, where N is not explcitly determined. If this net does not use a recurrent layer, it works like a uni-gram language model.\n", | |
"- RNN also resembles reinforcement learning. The input and hidden state together represent a Markov state. The softmax logits can be taken as some *advantage function*." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [conda env:tensorflow]", | |
"language": "python", | |
"name": "conda-env-tensorflow-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment