Format: application/octet-stream
⬇ Download File (9.5 KB)
   1  {
   2    "nbformat": 4,
   3    "nbformat_minor": 0,
   4    "metadata": {
   5      "accelerator": "GPU",
   6      "colab": {
   7        "name": "Course 3 - Week 2 - Lesson 2.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": "view-in-github"
  43        },
  44        "source": [
  45          "<a href=\"https://colab.research.google.com/github/lmoroney/dlaicourse/blob/master/TensorFlow%20In%20Practice/Course%203%20-%20NLP/Course%203%20-%20Week%202%20-%20Lesson%202.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
  46        ]
  47      },
  48      {
  49        "cell_type": "code",
  50        "metadata": {
  51          "id": "4gs9htvM7n_x"
  52        },
  53        "source": [
  54          "# Run this to ensure TensorFlow 2.x is used\n",
  55          "try:\n",
  56          "  # %tensorflow_version only exists in Colab.\n",
  57          "  %tensorflow_version 2.x\n",
  58          "except Exception:\n",
  59          "  pass"
  60        ],
  61        "execution_count": null,
  62        "outputs": []
  63      },
  64      {
  65        "cell_type": "code",
  66        "metadata": {
  67          "id": "XYYDvoskkE61"
  68        },
  69        "source": [
  70          "import json\n",
  71          "import tensorflow as tf\n",
  72          "\n",
  73          "from tensorflow.keras.preprocessing.text import Tokenizer\n",
  74          "from tensorflow.keras.preprocessing.sequence import pad_sequences"
  75        ],
  76        "execution_count": null,
  77        "outputs": []
  78      },
  79      {
  80        "cell_type": "code",
  81        "metadata": {
  82          "id": "0eJSTTYnkJQd"
  83        },
  84        "source": [
  85          "vocab_size = 10000\n",
  86          "embedding_dim = 16\n",
  87          "max_length = 100\n",
  88          "trunc_type='post'\n",
  89          "padding_type='post'\n",
  90          "oov_tok = \"<OOV>\"\n",
  91          "training_size = 20000\n"
  92        ],
  93        "execution_count": null,
  94        "outputs": []
  95      },
  96      {
  97        "cell_type": "code",
  98        "metadata": {
  99          "id": "BQVuQrZNkPn9"
 100        },
 101        "source": [
 102          "!wget --no-check-certificate \\\n",
 103          "    https://storage.googleapis.com/laurencemoroney-blog.appspot.com/sarcasm.json \\\n",
 104          "    -O /tmp/sarcasm.json\n"
 105        ],
 106        "execution_count": null,
 107        "outputs": []
 108      },
 109      {
 110        "cell_type": "code",
 111        "metadata": {
 112          "id": "oaLaaqhNkUPd"
 113        },
 114        "source": [
 115          "with open(\"/tmp/sarcasm.json\", 'r') as f:\n",
 116          "    datastore = json.load(f)\n",
 117          "\n",
 118          "sentences = []\n",
 119          "labels = []\n",
 120          "\n",
 121          "for item in datastore:\n",
 122          "    sentences.append(item['headline'])\n",
 123          "    labels.append(item['is_sarcastic'])"
 124        ],
 125        "execution_count": null,
 126        "outputs": []
 127      },
 128      {
 129        "cell_type": "code",
 130        "metadata": {
 131          "id": "S1sD-7v0kYWk"
 132        },
 133        "source": [
 134          "training_sentences = sentences[0:training_size]\n",
 135          "testing_sentences = sentences[training_size:]\n",
 136          "training_labels = labels[0:training_size]\n",
 137          "testing_labels = labels[training_size:]"
 138        ],
 139        "execution_count": null,
 140        "outputs": []
 141      },
 142      {
 143        "cell_type": "code",
 144        "metadata": {
 145          "id": "3u8UB0MCkZ5N"
 146        },
 147        "source": [
 148          "tokenizer = Tokenizer(num_words=vocab_size, oov_token=oov_tok)\n",
 149          "tokenizer.fit_on_texts(training_sentences)\n",
 150          "\n",
 151          "word_index = tokenizer.word_index\n",
 152          "\n",
 153          "training_sequences = tokenizer.texts_to_sequences(training_sentences)\n",
 154          "training_padded = pad_sequences(training_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)\n",
 155          "\n",
 156          "testing_sequences = tokenizer.texts_to_sequences(testing_sentences)\n",
 157          "testing_padded = pad_sequences(testing_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)"
 158        ],
 159        "execution_count": null,
 160        "outputs": []
 161      },
 162      {
 163        "cell_type": "code",
 164        "metadata": {
 165          "id": "GrAlWBKf99Ya"
 166        },
 167        "source": [
 168          "# Need this block to get it to work with TensorFlow 2.x\n",
 169          "import numpy as np\n",
 170          "training_padded = np.array(training_padded)\n",
 171          "training_labels = np.array(training_labels)\n",
 172          "testing_padded = np.array(testing_padded)\n",
 173          "testing_labels = np.array(testing_labels)"
 174        ],
 175        "execution_count": null,
 176        "outputs": []
 177      },
 178      {
 179        "cell_type": "code",
 180        "metadata": {
 181          "id": "FufaT4vlkiDE"
 182        },
 183        "source": [
 184          "model = tf.keras.Sequential([\n",
 185          "    tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),\n",
 186          "    tf.keras.layers.GlobalAveragePooling1D(),\n",
 187          "    tf.keras.layers.Dense(24, activation='relu'),\n",
 188          "    tf.keras.layers.Dense(1, activation='sigmoid')\n",
 189          "])\n",
 190          "model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])"
 191        ],
 192        "execution_count": null,
 193        "outputs": []
 194      },
 195      {
 196        "cell_type": "code",
 197        "metadata": {
 198          "id": "XfDt1hmYkiys"
 199        },
 200        "source": [
 201          "model.summary()\n"
 202        ],
 203        "execution_count": null,
 204        "outputs": []
 205      },
 206      {
 207        "cell_type": "code",
 208        "metadata": {
 209          "id": "2DTKQFf1kkyc"
 210        },
 211        "source": [
 212          "num_epochs = 30\n",
 213          "history = model.fit(training_padded, training_labels, epochs=num_epochs, validation_data=(testing_padded, testing_labels), verbose=2)"
 214        ],
 215        "execution_count": null,
 216        "outputs": []
 217      },
 218      {
 219        "cell_type": "code",
 220        "metadata": {
 221          "id": "2HYfBKXjkmU8"
 222        },
 223        "source": [
 224          "import matplotlib.pyplot as plt\n",
 225          "\n",
 226          "\n",
 227          "def plot_graphs(history, string):\n",
 228          "  plt.plot(history.history[string])\n",
 229          "  plt.plot(history.history['val_'+string])\n",
 230          "  plt.xlabel(\"Epochs\")\n",
 231          "  plt.ylabel(string)\n",
 232          "  plt.legend([string, 'val_'+string])\n",
 233          "  plt.show()\n",
 234          "  \n",
 235          "plot_graphs(history, \"accuracy\")\n",
 236          "plot_graphs(history, \"loss\")"
 237        ],
 238        "execution_count": null,
 239        "outputs": []
 240      },
 241      {
 242        "cell_type": "code",
 243        "metadata": {
 244          "id": "7SBdAZAenvzL"
 245        },
 246        "source": [
 247          "reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])\n",
 248          "\n",
 249          "def decode_sentence(text):\n",
 250          "    return ' '.join([reverse_word_index.get(i, '?') for i in text])\n",
 251          "\n",
 252          "print(decode_sentence(training_padded[0]))\n",
 253          "print(training_sentences[2])\n",
 254          "print(labels[2])"
 255        ],
 256        "execution_count": null,
 257        "outputs": []
 258      },
 259      {
 260        "cell_type": "code",
 261        "metadata": {
 262          "id": "c9MqihtEkzQ9"
 263        },
 264        "source": [
 265          "e = model.layers[0]\n",
 266          "weights = e.get_weights()[0]\n",
 267          "print(weights.shape) # shape: (vocab_size, embedding_dim)\n"
 268        ],
 269        "execution_count": null,
 270        "outputs": []
 271      },
 272      {
 273        "cell_type": "code",
 274        "metadata": {
 275          "id": "LoBXVffknldU"
 276        },
 277        "source": [
 278          "import io\n",
 279          "\n",
 280          "out_v = io.open('vecs.tsv', 'w', encoding='utf-8')\n",
 281          "out_m = io.open('meta.tsv', 'w', encoding='utf-8')\n",
 282          "for word_num in range(1, vocab_size):\n",
 283          "  word = reverse_word_index[word_num]\n",
 284          "  embeddings = weights[word_num]\n",
 285          "  out_m.write(word + \"\\n\")\n",
 286          "  out_v.write('\\t'.join([str(x) for x in embeddings]) + \"\\n\")\n",
 287          "out_v.close()\n",
 288          "out_m.close()"
 289        ],
 290        "execution_count": null,
 291        "outputs": []
 292      },
 293      {
 294        "cell_type": "code",
 295        "metadata": {
 296          "id": "U4eZ5HtVnnEE"
 297        },
 298        "source": [
 299          "try:\n",
 300          "  from google.colab import files\n",
 301          "except ImportError:\n",
 302          "  pass\n",
 303          "else:\n",
 304          "  files.download('vecs.tsv')\n",
 305          "  files.download('meta.tsv')"
 306        ],
 307        "execution_count": null,
 308        "outputs": []
 309      },
 310      {
 311        "cell_type": "code",
 312        "metadata": {
 313          "id": "cG8-ArY-qDcz"
 314        },
 315        "source": [
 316          "sentence = [\"granny starting to fear spiders in the garden might be real\", \"game of thrones season finale showing this sunday night\"]\n",
 317          "sequences = tokenizer.texts_to_sequences(sentence)\n",
 318          "padded = pad_sequences(sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)\n",
 319          "print(model.predict(padded))"
 320        ],
 321        "execution_count": null,
 322        "outputs": []
 323      }
 324    ]
 325  }