From 3b4798e08aa565ea8a9e6c493fd996d45ebf0082 Mon Sep 17 00:00:00 2001 From: vmirly Date: Sun, 3 Nov 2019 23:16:43 -0500 Subject: [PATCH] Run all cells (part2) --- ch16/ch16_part2.ipynb | 168 ++++++++++++++++++++++++------------------ ch16/ch16_part2.py | 33 +++++++++ 2 files changed, 128 insertions(+), 73 deletions(-) diff --git a/ch16/ch16_part2.ipynb b/ch16/ch16_part2.ipynb index 5e470b22..354d5a83 100644 --- a/ch16/ch16_part2.ipynb +++ b/ch16/ch16_part2.ipynb @@ -39,11 +39,11 @@ "Sebastian Raschka & Vahid Mirjalili \n", "last updated: 2019-11-03 \n", "\n", - "numpy 1.17.2\n", - "scipy 1.2.1\n", - "matplotlib 3.1.0\n", + "numpy 1.17.3\n", + "scipy 1.3.1\n", + "matplotlib 3.1.1\n", "tensorflow 2.0.0\n", - "tensorflow_datasets 1.3.0\n" + "tensorflow_datasets 1.2.0\n" ] } ], @@ -103,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -112,7 +112,7 @@ "text": [ " % Total % Received % Xferd Average Speed Time Time Time Current\n", " Dload Upload Total Spent Left Speed\n", - "100 1144k 100 1144k 0 0 3034k 0 --:--:-- --:--:-- --:--:-- 3034k\n" + "100 1144k 100 1144k 0 0 1807k 0 --:--:-- --:--:-- --:--:-- 1807k\n" ] } ], @@ -122,7 +122,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -155,7 +155,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -165,7 +165,7 @@ "" ] }, - "execution_count": 4, + "execution_count": 6, "metadata": { "image/png": { "width": 700 @@ -180,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -210,7 +210,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -220,7 +220,7 @@ "" ] }, - "execution_count": 5, + "execution_count": 8, "metadata": { "image/png": { "width": 700 @@ -235,7 +235,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -262,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -292,7 +292,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -302,7 +302,7 @@ "" ] }, - "execution_count": 6, + "execution_count": 11, "metadata": { "image/png": { "width": 700 @@ -317,7 +317,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -351,7 +351,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -360,7 +360,7 @@ "" ] }, - "execution_count": 8, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -385,7 +385,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -436,7 +436,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -444,54 +444,54 @@ "output_type": "stream", "text": [ "Epoch 1/20\n", - "424/424 [==============================] - 69s 162ms/step - loss: 2.3011\n", + "424/424 [==============================] - 94s 221ms/step - loss: 2.3011\n", "Epoch 2/20\n", - "424/424 [==============================] - 66s 157ms/step - loss: 1.7332\n", + "424/424 [==============================] - 93s 219ms/step - loss: 1.7332\n", "Epoch 3/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.5343\n", + "424/424 [==============================] - 93s 218ms/step - loss: 1.5343\n", "Epoch 4/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.4204\n", + "424/424 [==============================] - 93s 219ms/step - loss: 1.4204\n", "Epoch 5/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.3477\n", + "424/424 [==============================] - 93s 220ms/step - loss: 1.3477\n", "Epoch 6/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.2976\n", + "424/424 [==============================] - 93s 221ms/step - loss: 1.2976\n", "Epoch 7/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.2597\n", + "424/424 [==============================] - 93s 220ms/step - loss: 1.2597\n", "Epoch 8/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.2286\n", + "424/424 [==============================] - 93s 219ms/step - loss: 1.2286\n", "Epoch 9/20\n", - "424/424 [==============================] - 66s 157ms/step - loss: 1.2030\n", + "424/424 [==============================] - 92s 218ms/step - loss: 1.2030\n", "Epoch 10/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.1817\n", + "424/424 [==============================] - 93s 220ms/step - loss: 1.1817\n", "Epoch 11/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.1618\n", + "424/424 [==============================] - 94s 221ms/step - loss: 1.1618\n", "Epoch 12/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.1446\n", + "424/424 [==============================] - 94s 221ms/step - loss: 1.1446\n", "Epoch 13/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.1282\n", + "424/424 [==============================] - 94s 221ms/step - loss: 1.1283\n", "Epoch 14/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.1125\n", + "424/424 [==============================] - 93s 218ms/step - loss: 1.1126\n", "Epoch 15/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.0984\n", + "424/424 [==============================] - 93s 220ms/step - loss: 1.0985\n", "Epoch 16/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.0844\n", + "424/424 [==============================] - 92s 218ms/step - loss: 1.0845\n", "Epoch 17/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.0716\n", + "424/424 [==============================] - 93s 220ms/step - loss: 1.0718\n", "Epoch 18/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.0588\n", + "424/424 [==============================] - 94s 222ms/step - loss: 1.0589\n", "Epoch 19/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.0458\n", + "424/424 [==============================] - 93s 219ms/step - loss: 1.0458\n", "Epoch 20/20\n", - "424/424 [==============================] - 67s 157ms/step - loss: 1.0335\n" + "424/424 [==============================] - 94s 221ms/step - loss: 1.0337\n" ] }, { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 10, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -515,7 +515,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -540,7 +540,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -565,15 +565,29 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The island may have been holled up the material recase from his mind now, they had no contact the firearms aroused them, but the first of the sun had a large must sean in a few minutes great best ton. Neb len through the edder the unfortunate, they had remained at the corral, the pointes of fall into the high genior narrow easied at their able themsevere\n", - "wished to protect the ordrain of the roup blad. On the land of their still more mounted by inflammunity cliff ordered the missol. There was on the bows, \n" + "The island is open he heard the victory of the\n", + "Mercy, and brought it into them, and they no longer continue, some on the little man of the felting circle of slopes.\n", + "\n", + "The engineer troused, he could not find our companions.\n", + "\n", + "\n", + "\n", + "Chapter 11\n", + "\n", + "At this position, he might just as if his first true to be finished, and he\n", + "though not more I can this teles.”\n", + "\n", + "“Why shall fear line,” answered the reporter, “what a disposal silence was advanced with them, and in masterspon.\n", + "\n", + "Before three heights of the\n", + "Frenchant Heights \n" ] } ], @@ -622,7 +636,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -647,14 +661,17 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The island was so as to contracted the plateau of Prospect Heights was to be understood. The settlers were brought to the southern coast of the island and stood out of the colonists, who was also met with the palisade. The boat was not a sort of his mise, and they were in the midst of the point of the sea, and he should have been able to save him. “With a ready to the settlers were obliged to that he was the last side of the sea, the strength of the drawn and fell on the shore. The trees were fired at the\n" + "The island was so as to discover the position of the darkness there.\n", + "\n", + "The ground was about to death the man had been so struck the colonists have been able to speak a little by returning to the corral. The next day the summer heard might be supposed that the first time on the shore, and the captain proved the cart of the colonists the stranger was towards the\n", + "colonists, who were very simple to salm a little beach of the corral, the settlers had already proceeded towards the rocks, and they had already sti\n" ] } ], @@ -666,31 +683,36 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The island harned all\n", - "execumed, whether existed\n", - "in Freh oad, or\n", - "notp? atlisheve,\n", - "know Ivan arrih!. Dudgemed it; belongly, still likelowed on ewt.\n", + "The island\n", + "happilid a drems parts,\n", + "withmests? Barmected safed, Lindoency-islew.\n", + "\n", + "Top,\n", + "HiqualshE As culting among Promotion.”\n", + "Criflies verdood. ven,\n", + "had\n", + "lastly vivit!\n", + "Monsh!\n", "\n", - "An Aconnoun unvow Clave,\n", - "Ogen of.” criefly Harding,” observed\n", - "eating from Ta cimitlat. I.\n", + "Coar swimmemsly above Ony indident\n", + "qumbtelfisking\n", + "forty-8ther.\n", "\n", - "These urislarsnifigent gaveAned it, there could ruffil!\n", + "These trenimostly penered; theanesemonerg.\n", + "-gamisw, open in his sudden valarmania\n", + "him leare\n", + "LebThoastor LarbodDeri’s raying\n", + "anchors, unvorunormes an hour, butlo Life polpue escaped by twelve wedcousment.”\n", "\n", - "Memisproy?--its east, ma, in\n", - "hearia! Austhours Oclas!” to” re!” ald he smumps without it did not plenwe Prescries? Certainly this\n", - "horns having putting\n", - "him meat.”\n", - "By damb Capteinggoine his plants-abavanoo;\n", - "near this \n" + "Can ear1-tive,” ed Pencroft, “who cinnot into nacuounls of\n", + "severe \n" ] } ], @@ -714,7 +736,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 22, "metadata": {}, "outputs": [ { @@ -724,7 +746,7 @@ "" ] }, - "execution_count": 8, + "execution_count": 22, "metadata": { "image/png": { "width": 700 @@ -754,7 +776,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -764,7 +786,7 @@ "" ] }, - "execution_count": 9, + "execution_count": 23, "metadata": { "image/png": { "width": 700 @@ -801,7 +823,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -809,7 +831,7 @@ "output_type": "stream", "text": [ "[NbConvertApp] Converting notebook ch16_part2.ipynb to script\n", - "[NbConvertApp] Writing 6472 bytes to ch16_part2.py\n" + "[NbConvertApp] Writing 6935 bytes to ch16_part2.py\n" ] } ], diff --git a/ch16/ch16_part2.py b/ch16/ch16_part2.py index 59bdf0f6..533b6dbe 100644 --- a/ch16/ch16_part2.py +++ b/ch16/ch16_part2.py @@ -21,8 +21,17 @@ + + + + # ## Project two: character-level language modeling in TensorFlow # + + + + + # ### Preprocessing the dataset @@ -49,6 +58,10 @@ + + + + chars_sorted = sorted(char_set) char2int = {ch:i for i,ch in enumerate(chars_sorted)} char_array = np.array(chars_sorted) @@ -67,6 +80,10 @@ + + + + ds_text_encoded = tf.data.Dataset.from_tensor_slices(text_encoded) for ex in ds_text_encoded.take(5): @@ -91,6 +108,10 @@ + + + + ## define the function for splitting x & y def split_input_target(chunk): input_seq = chunk[:-1] @@ -253,10 +274,22 @@ def sample(model, starting_str, # # ## A basic version of self-attention # +# + + + + + # ### Parameterizing the self-attention mechanism with query, key, and value weights +# + # # ## Multi-head attention and the Transformer block + + + + # # ... #