Format: application/octet-stream
1 {
2 "nbformat": 4,
3 "nbformat_minor": 0,
4 "metadata": {
5 "accelerator": "GPU",
6 "colab": {
7 "name": "Course 3 - Week 2 - Lesson 3.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": "VWI-u75cOh0S"
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%202%20-%20Lesson%203.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": "P-AhVYeBWgQ3"
65 },
66 "source": [
67 "# NOTE: PLEASE MAKE SURE YOU ARE RUNNING THIS IN A PYTHON3 ENVIRONMENT\n",
68 "\n",
69 "import tensorflow as tf\n",
70 "print(tf.__version__)\n"
71 ],
72 "execution_count": null,
73 "outputs": []
74 },
75 {
76 "cell_type": "code",
77 "metadata": {
78 "id": "wm9S3T8-9H4q"
79 },
80 "source": [
81 "# Double check TF 2.0x is installed. If you ran the above block, there was a \n",
82 "# 'reset all runtimes' button at the bottom that you needed to press\n",
83 "import tensorflow as tf\n",
84 "print(tf.__version__)"
85 ],
86 "execution_count": null,
87 "outputs": []
88 },
89 {
90 "cell_type": "code",
91 "metadata": {
92 "id": "_IoM4VFxWpMR"
93 },
94 "source": [
95 "# If the import fails, run this\n",
96 "# !pip install -q tensorflow-datasets\n",
97 "\n",
98 "import tensorflow_datasets as tfds\n",
99 "imdb, info = tfds.load(\"imdb_reviews/subwords8k\", with_info=True, as_supervised=True)\n"
100 ],
101 "execution_count": null,
102 "outputs": []
103 },
104 {
105 "cell_type": "code",
106 "metadata": {
107 "id": "wHQ2Ko0zl7M4"
108 },
109 "source": [
110 "train_data, test_data = imdb['train'], imdb['test']"
111 ],
112 "execution_count": null,
113 "outputs": []
114 },
115 {
116 "cell_type": "code",
117 "metadata": {
118 "id": "fqGRSe_eCdOz"
119 },
120 "source": [
121 "tokenizer = info.features['text'].encoder"
122 ],
123 "execution_count": null,
124 "outputs": []
125 },
126 {
127 "cell_type": "code",
128 "metadata": {
129 "id": "F6b_EraCDLOh"
130 },
131 "source": [
132 "print(tokenizer.subwords)"
133 ],
134 "execution_count": null,
135 "outputs": []
136 },
137 {
138 "cell_type": "code",
139 "metadata": {
140 "id": "fPl2BXhYEHRP"
141 },
142 "source": [
143 "sample_string = 'TensorFlow, from basics to mastery'\n",
144 "\n",
145 "tokenized_string = tokenizer.encode(sample_string)\n",
146 "print ('Tokenized string is {}'.format(tokenized_string))\n",
147 "\n",
148 "original_string = tokenizer.decode(tokenized_string)\n",
149 "print ('The original string: {}'.format(original_string))\n"
150 ],
151 "execution_count": null,
152 "outputs": []
153 },
154 {
155 "cell_type": "code",
156 "metadata": {
157 "id": "_3t7vvNLEZml"
158 },
159 "source": [
160 "for ts in tokenized_string:\n",
161 " print ('{} ----> {}'.format(ts, tokenizer.decode([ts])))"
162 ],
163 "execution_count": null,
164 "outputs": []
165 },
166 {
167 "cell_type": "code",
168 "metadata": {
169 "id": "j2jS31uHOh0Y"
170 },
171 "source": [
172 "BUFFER_SIZE = 10000\n",
173 "BATCH_SIZE = 64\n",
174 "\n",
175 "train_dataset = train_data.shuffle(BUFFER_SIZE)\n",
176 "train_dataset = train_dataset.padded_batch(BATCH_SIZE, tf.compat.v1.data.get_output_shapes(train_dataset))\n",
177 "test_dataset = test_data.padded_batch(BATCH_SIZE, tf.compat.v1.data.get_output_shapes(test_data))"
178 ],
179 "execution_count": null,
180 "outputs": []
181 },
182 {
183 "cell_type": "code",
184 "metadata": {
185 "id": "5NEpdhb8AxID"
186 },
187 "source": [
188 "embedding_dim = 64\n",
189 "model = tf.keras.Sequential([\n",
190 " tf.keras.layers.Embedding(tokenizer.vocab_size, embedding_dim),\n",
191 " tf.keras.layers.GlobalAveragePooling1D(),\n",
192 " tf.keras.layers.Dense(6, activation='relu'),\n",
193 " tf.keras.layers.Dense(1, activation='sigmoid')\n",
194 "])\n",
195 "\n",
196 "model.summary()"
197 ],
198 "execution_count": null,
199 "outputs": []
200 },
201 {
202 "cell_type": "code",
203 "metadata": {
204 "id": "fkt8c5dNuUlT"
205 },
206 "source": [
207 "num_epochs = 10\n",
208 "\n",
209 "model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])\n",
210 "\n",
211 "history = model.fit(train_dataset, epochs=num_epochs, validation_data=test_dataset)"
212 ],
213 "execution_count": null,
214 "outputs": []
215 },
216 {
217 "cell_type": "code",
218 "metadata": {
219 "id": "-_rMnm7WxQGT"
220 },
221 "source": [
222 "import matplotlib.pyplot as plt\n",
223 "\n",
224 "\n",
225 "def plot_graphs(history, string):\n",
226 " plt.plot(history.history[string])\n",
227 " plt.plot(history.history['val_'+string])\n",
228 " plt.xlabel(\"Epochs\")\n",
229 " plt.ylabel(string)\n",
230 " plt.legend([string, 'val_'+string])\n",
231 " plt.show()\n",
232 " \n",
233 "plot_graphs(history, \"accuracy\")\n",
234 "plot_graphs(history, \"loss\")"
235 ],
236 "execution_count": null,
237 "outputs": []
238 },
239 {
240 "cell_type": "code",
241 "metadata": {
242 "id": "qACq5FLzTW4A"
243 },
244 "source": [
245 "e = model.layers[0]\n",
246 "weights = e.get_weights()[0]\n",
247 "print(weights.shape) # shape: (vocab_size, embedding_dim)\n",
248 "\n",
249 "import io\n",
250 "\n",
251 "out_v = io.open('vecs.tsv', 'w', encoding='utf-8')\n",
252 "out_m = io.open('meta.tsv', 'w', encoding='utf-8')\n",
253 "for word_num in range(1, tokenizer.vocab_size):\n",
254 " word = tokenizer.decode([word_num])\n",
255 " embeddings = weights[word_num]\n",
256 " out_m.write(word + \"\\n\")\n",
257 " out_v.write('\\t'.join([str(x) for x in embeddings]) + \"\\n\")\n",
258 "out_v.close()\n",
259 "out_m.close()\n",
260 "\n",
261 "\n",
262 "try:\n",
263 " from google.colab import files\n",
264 "except ImportError:\n",
265 " pass\n",
266 "else:\n",
267 " files.download('vecs.tsv')\n",
268 " files.download('meta.tsv')"
269 ],
270 "execution_count": null,
271 "outputs": []
272 }
273 ]
274 }