Format: application/octet-stream
⬇ Download File (5.4 KB)
   1  {
   2    "nbformat": 4,
   3    "nbformat_minor": 0,
   4    "metadata": {
   5      "accelerator": "GPU",
   6      "colab": {
   7        "name": "Course 3 - Week 3 - Lesson 1c.ipynb",
   8        "provenance": [],
   9        "collapsed_sections": [],
  10        "toc_visible": true
  11      },
  12      "kernelspec": {
  13        "display_name": "Python 3",
  14        "name": "python3"
  15      }
  16    },
  17    "cells": [
  18      {
  19        "cell_type": "code",
  20        "metadata": {
  21          "id": "zX4Kg8DUTKWO"
  22        },
  23        "source": [
  24          "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
  25          "# you may not use this file except in compliance with the License.\n",
  26          "# You may obtain a copy of the License at\n",
  27          "#\n",
  28          "# https://www.apache.org/licenses/LICENSE-2.0\n",
  29          "#\n",
  30          "# Unless required by applicable law or agreed to in writing, software\n",
  31          "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
  32          "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
  33          "# See the License for the specific language governing permissions and\n",
  34          "# limitations under the License."
  35        ],
  36        "execution_count": null,
  37        "outputs": []
  38      },
  39      {
  40        "cell_type": "markdown",
  41        "metadata": {
  42          "id": "rFiCyWQ-NC5D"
  43        },
  44        "source": [
  45          "# Multiple Layer GRU"
  46        ]
  47      },
  48      {
  49        "cell_type": "code",
  50        "metadata": {
  51          "id": "Y20Lud2ZMBhW"
  52        },
  53        "source": [
  54          "from __future__ import absolute_import, division, print_function, unicode_literals\n",
  55          "\n",
  56          "\n",
  57          "import tensorflow_datasets as tfds\n",
  58          "import tensorflow as tf\n",
  59          "print(tf.__version__)"
  60        ],
  61        "execution_count": null,
  62        "outputs": []
  63      },
  64      {
  65        "cell_type": "code",
  66        "metadata": {
  67          "id": "uAU8g7C0MPZE"
  68        },
  69        "source": [
  70          "import tensorflow_datasets as tfds\n",
  71          "import tensorflow as tf\n",
  72          "print(tf.__version__)"
  73        ],
  74        "execution_count": null,
  75        "outputs": []
  76      },
  77      {
  78        "cell_type": "code",
  79        "metadata": {
  80          "id": "AW-4Vo4TMUHb"
  81        },
  82        "source": [
  83          "# Get the data\n",
  84          "dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True, as_supervised=True)\n",
  85          "train_dataset, test_dataset = dataset['train'], dataset['test']\n"
  86        ],
  87        "execution_count": null,
  88        "outputs": []
  89      },
  90      {
  91        "cell_type": "code",
  92        "metadata": {
  93          "id": "L11bIR6-PKvs"
  94        },
  95        "source": [
  96          "tokenizer = info.features['text'].encoder"
  97        ],
  98        "execution_count": null,
  99        "outputs": []
 100      },
 101      {
 102        "cell_type": "code",
 103        "metadata": {
 104          "id": "ffvRUI0_McDS"
 105        },
 106        "source": [
 107          "BUFFER_SIZE = 10000\n",
 108          "BATCH_SIZE = 64\n",
 109          "\n",
 110          "train_dataset = train_dataset.shuffle(BUFFER_SIZE)\n",
 111          "train_dataset = train_dataset.padded_batch(BATCH_SIZE, train_dataset.output_shapes)\n",
 112          "test_dataset = test_dataset.padded_batch(BATCH_SIZE, test_dataset.output_shapes)"
 113        ],
 114        "execution_count": null,
 115        "outputs": []
 116      },
 117      {
 118        "cell_type": "code",
 119        "metadata": {
 120          "id": "jo1jjO3vn0jo"
 121        },
 122        "source": [
 123          "model = tf.keras.Sequential([\n",
 124          "    tf.keras.layers.Embedding(tokenizer.vocab_size, 64),\n",
 125          "    tf.keras.layers.Conv1D(128, 5, activation='relu'),\n",
 126          "    tf.keras.layers.GlobalAveragePooling1D(),\n",
 127          "    tf.keras.layers.Dense(64, activation='relu'),\n",
 128          "    tf.keras.layers.Dense(1, activation='sigmoid')\n",
 129          "])"
 130        ],
 131        "execution_count": null,
 132        "outputs": []
 133      },
 134      {
 135        "cell_type": "code",
 136        "metadata": {
 137          "id": "QKI5dfPgMioL"
 138        },
 139        "source": [
 140          "model.summary()"
 141        ],
 142        "execution_count": null,
 143        "outputs": []
 144      },
 145      {
 146        "cell_type": "code",
 147        "metadata": {
 148          "id": "Uip7QOVzMoMq"
 149        },
 150        "source": [
 151          "model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])"
 152        ],
 153        "execution_count": null,
 154        "outputs": []
 155      },
 156      {
 157        "cell_type": "code",
 158        "metadata": {
 159          "id": "7mlgzaRDMtF6"
 160        },
 161        "source": [
 162          "NUM_EPOCHS = 10\n",
 163          "history = model.fit(train_dataset, epochs=NUM_EPOCHS, validation_data=test_dataset)"
 164        ],
 165        "execution_count": null,
 166        "outputs": []
 167      },
 168      {
 169        "cell_type": "code",
 170        "metadata": {
 171          "id": "Mp1Z7P9pYRSK"
 172        },
 173        "source": [
 174          "import matplotlib.pyplot as plt\n",
 175          "\n",
 176          "\n",
 177          "def plot_graphs(history, string):\n",
 178          "  plt.plot(history.history[string])\n",
 179          "  plt.plot(history.history['val_'+string])\n",
 180          "  plt.xlabel(\"Epochs\")\n",
 181          "  plt.ylabel(string)\n",
 182          "  plt.legend([string, 'val_'+string])\n",
 183          "  plt.show()"
 184        ],
 185        "execution_count": null,
 186        "outputs": []
 187      },
 188      {
 189        "cell_type": "code",
 190        "metadata": {
 191          "id": "R_sX6ilIM515"
 192        },
 193        "source": [
 194          "plot_graphs(history, 'accuracy')"
 195        ],
 196        "execution_count": null,
 197        "outputs": []
 198      },
 199      {
 200        "cell_type": "code",
 201        "metadata": {
 202          "id": "RFEXtKtqNARB"
 203        },
 204        "source": [
 205          "plot_graphs(history, 'loss')"
 206        ],
 207        "execution_count": null,
 208        "outputs": []
 209      }
 210    ]
 211  }