Format: application/octet-stream
1 {
2 "nbformat": 4,
3 "nbformat_minor": 0,
4 "metadata": {
5 "accelerator": "GPU",
6 "colab": {
7 "name": "Course 3 - Week 3 - Lesson 2c.ipynb",
8 "provenance": [],
9 "collapsed_sections": [],
10 "toc_visible": true
11 },
12 "kernelspec": {
13 "display_name": "Python 3",
14 "language": "python",
15 "name": "python3"
16 },
17 "language_info": {
18 "codemirror_mode": {
19 "name": "ipython",
20 "version": 3
21 },
22 "file_extension": ".py",
23 "mimetype": "text/x-python",
24 "name": "python",
25 "nbconvert_exporter": "python",
26 "pygments_lexer": "ipython3",
27 "version": "3.7.6"
28 }
29 },
30 "cells": [
31 {
32 "cell_type": "code",
33 "metadata": {
34 "id": "zX4Kg8DUTKWO"
35 },
36 "source": [
37 "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
38 "# you may not use this file except in compliance with the License.\n",
39 "# You may obtain a copy of the License at\n",
40 "#\n",
41 "# https://www.apache.org/licenses/LICENSE-2.0\n",
42 "#\n",
43 "# Unless required by applicable law or agreed to in writing, software\n",
44 "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
45 "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
46 "# See the License for the specific language governing permissions and\n",
47 "# limitations under the License."
48 ],
49 "execution_count": null,
50 "outputs": []
51 },
52 {
53 "cell_type": "markdown",
54 "metadata": {
55 "id": "tr1BWgmIVnU-"
56 },
57 "source": [
58 "<a href=\"https://colab.research.google.com/github/lmoroney/dlaicourse/blob/master/TensorFlow%20In%20Practice/Course%203%20-%20NLP/Course%203%20-%20Week%203%20-%20Lesson%202c.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
59 ]
60 },
61 {
62 "cell_type": "code",
63 "metadata": {
64 "id": "jGwXGIXvFhXW"
65 },
66 "source": [
67 "import numpy as np\n",
68 "\n",
69 "import json\n",
70 "import tensorflow as tf\n",
71 "\n",
72 "from tensorflow.keras.preprocessing.text import Tokenizer\n",
73 "from tensorflow.keras.preprocessing.sequence import pad_sequences\n",
74 "\n",
75 "!wget --no-check-certificate \\\n",
76 " https://storage.googleapis.com/laurencemoroney-blog.appspot.com/sarcasm.json \\\n",
77 " -O /tmp/sarcasm.json\n",
78 "\n",
79 "vocab_size = 1000\n",
80 "embedding_dim = 16\n",
81 "max_length = 120\n",
82 "trunc_type='post'\n",
83 "padding_type='post'\n",
84 "oov_tok = \"<OOV>\"\n",
85 "training_size = 20000\n",
86 "\n",
87 "\n",
88 "with open(\"/tmp/sarcasm.json\", 'r') as f:\n",
89 " datastore = json.load(f)\n",
90 "\n",
91 "\n",
92 "sentences = []\n",
93 "labels = []\n",
94 "urls = []\n",
95 "for item in datastore:\n",
96 " sentences.append(item['headline'])\n",
97 " labels.append(item['is_sarcastic'])\n",
98 "\n",
99 "training_sentences = sentences[0:training_size]\n",
100 "testing_sentences = sentences[training_size:]\n",
101 "training_labels = labels[0:training_size]\n",
102 "testing_labels = labels[training_size:]\n",
103 "\n",
104 "tokenizer = Tokenizer(num_words=vocab_size, oov_token=oov_tok)\n",
105 "tokenizer.fit_on_texts(training_sentences)\n",
106 "\n",
107 "word_index = tokenizer.word_index\n",
108 "\n",
109 "training_sequences = tokenizer.texts_to_sequences(training_sentences)\n",
110 "training_padded = pad_sequences(training_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)\n",
111 "\n",
112 "testing_sequences = tokenizer.texts_to_sequences(testing_sentences)\n",
113 "testing_padded = pad_sequences(testing_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)\n",
114 "\n",
115 "model = tf.keras.Sequential([\n",
116 " tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),\n",
117 " tf.keras.layers.Conv1D(128, 5, activation='relu'),\n",
118 " tf.keras.layers.GlobalMaxPooling1D(),\n",
119 " tf.keras.layers.Dense(24, activation='relu'),\n",
120 " tf.keras.layers.Dense(1, activation='sigmoid')\n",
121 "])\n",
122 "model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])\n",
123 "model.summary()\n",
124 "\n",
125 "num_epochs = 50\n",
126 "\n",
127 "training_padded = np.array(training_padded)\n",
128 "training_labels = np.array(training_labels)\n",
129 "testing_padded = np.array(testing_padded)\n",
130 "testing_labels = np.array(testing_labels)\n",
131 "\n",
132 "history = model.fit(training_padded, training_labels, epochs=num_epochs, validation_data=(testing_padded, testing_labels), verbose=1)\n"
133 ],
134 "execution_count": null,
135 "outputs": []
136 },
137 {
138 "cell_type": "code",
139 "metadata": {
140 "id": "g9DC6dmLF8DC"
141 },
142 "source": [
143 "import matplotlib.pyplot as plt\n",
144 "\n",
145 "\n",
146 "def plot_graphs(history, string):\n",
147 " plt.plot(history.history[string])\n",
148 " plt.plot(history.history['val_'+string])\n",
149 " plt.xlabel(\"Epochs\")\n",
150 " plt.ylabel(string)\n",
151 " plt.legend([string, 'val_'+string])\n",
152 " plt.show()\n",
153 "\n",
154 "plot_graphs(history, 'accuracy')\n",
155 "plot_graphs(history, 'loss')"
156 ],
157 "execution_count": null,
158 "outputs": []
159 },
160 {
161 "cell_type": "code",
162 "metadata": {
163 "id": "7ZEZIUppGhdi"
164 },
165 "source": [
166 "model.save(\"test.h5\")"
167 ],
168 "execution_count": null,
169 "outputs": []
170 }
171 ]
172 }