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 1.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.6.9"
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": "view-in-github"
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%201.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 "import tensorflow as tf\n",
68 "print(tf.__version__)\n",
69 "\n",
70 "# !pip install -q tensorflow-datasets"
71 ],
72 "execution_count": null,
73 "outputs": []
74 },
75 {
76 "cell_type": "code",
77 "metadata": {
78 "id": "_IoM4VFxWpMR"
79 },
80 "source": [
81 "import tensorflow_datasets as tfds\n",
82 "imdb, info = tfds.load(\"imdb_reviews\", with_info=True, as_supervised=True)\n"
83 ],
84 "execution_count": null,
85 "outputs": []
86 },
87 {
88 "cell_type": "code",
89 "metadata": {
90 "id": "wHQ2Ko0zl7M4"
91 },
92 "source": [
93 "import numpy as np\n",
94 "\n",
95 "train_data, test_data = imdb['train'], imdb['test']\n",
96 "\n",
97 "training_sentences = []\n",
98 "training_labels = []\n",
99 "\n",
100 "testing_sentences = []\n",
101 "testing_labels = []\n",
102 "\n",
103 "# str(s.tonumpy()) is needed in Python3 instead of just s.numpy()\n",
104 "for s,l in train_data:\n",
105 " training_sentences.append(s.numpy().decode('utf8'))\n",
106 " training_labels.append(l.numpy())\n",
107 " \n",
108 "for s,l in test_data:\n",
109 " testing_sentences.append(s.numpy().decode('utf8'))\n",
110 " testing_labels.append(l.numpy())\n",
111 " \n",
112 "training_labels_final = np.array(training_labels)\n",
113 "testing_labels_final = np.array(testing_labels)\n"
114 ],
115 "execution_count": null,
116 "outputs": []
117 },
118 {
119 "cell_type": "code",
120 "metadata": {
121 "id": "7n15yyMdmoH1"
122 },
123 "source": [
124 "vocab_size = 10000\n",
125 "embedding_dim = 16\n",
126 "max_length = 120\n",
127 "trunc_type='post'\n",
128 "oov_tok = \"<OOV>\"\n",
129 "\n",
130 "\n",
131 "from tensorflow.keras.preprocessing.text import Tokenizer\n",
132 "from tensorflow.keras.preprocessing.sequence import pad_sequences\n",
133 "\n",
134 "tokenizer = Tokenizer(num_words = vocab_size, oov_token=oov_tok)\n",
135 "tokenizer.fit_on_texts(training_sentences)\n",
136 "word_index = tokenizer.word_index\n",
137 "sequences = tokenizer.texts_to_sequences(training_sentences)\n",
138 "padded = pad_sequences(sequences,maxlen=max_length, truncating=trunc_type)\n",
139 "\n",
140 "testing_sequences = tokenizer.texts_to_sequences(testing_sentences)\n",
141 "testing_padded = pad_sequences(testing_sequences,maxlen=max_length)\n"
142 ],
143 "execution_count": null,
144 "outputs": []
145 },
146 {
147 "cell_type": "code",
148 "metadata": {
149 "id": "9axf0uIXVMhO"
150 },
151 "source": [
152 "reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])\n",
153 "\n",
154 "def decode_review(text):\n",
155 " return ' '.join([reverse_word_index.get(i, '?') for i in text])\n",
156 "\n",
157 "print(decode_review(padded[3]))\n",
158 "print(training_sentences[3])"
159 ],
160 "execution_count": null,
161 "outputs": []
162 },
163 {
164 "cell_type": "code",
165 "metadata": {
166 "id": "5NEpdhb8AxID"
167 },
168 "source": [
169 "model = tf.keras.Sequential([\n",
170 " tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),\n",
171 " tf.keras.layers.Flatten(),\n",
172 " tf.keras.layers.Dense(6, activation='relu'),\n",
173 " tf.keras.layers.Dense(1, activation='sigmoid')\n",
174 "])\n",
175 "model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])\n",
176 "model.summary()\n"
177 ],
178 "execution_count": null,
179 "outputs": []
180 },
181 {
182 "cell_type": "code",
183 "metadata": {
184 "id": "V5LLrXC-uNX6"
185 },
186 "source": [
187 "num_epochs = 10\n",
188 "model.fit(padded, training_labels_final, epochs=num_epochs, validation_data=(testing_padded, testing_labels_final))"
189 ],
190 "execution_count": null,
191 "outputs": []
192 },
193 {
194 "cell_type": "code",
195 "metadata": {
196 "id": "yAmjJqEyCOF_"
197 },
198 "source": [
199 "e = model.layers[0]\n",
200 "weights = e.get_weights()[0]\n",
201 "print(weights.shape) # shape: (vocab_size, embedding_dim)"
202 ],
203 "execution_count": null,
204 "outputs": []
205 },
206 {
207 "cell_type": "code",
208 "metadata": {
209 "id": "jmB0Uxk0ycP6"
210 },
211 "source": [
212 "import io\n",
213 "\n",
214 "out_v = io.open('vecs.tsv', 'w', encoding='utf-8')\n",
215 "out_m = io.open('meta.tsv', 'w', encoding='utf-8')\n",
216 "for word_num in range(1, vocab_size):\n",
217 " word = reverse_word_index[word_num]\n",
218 " embeddings = weights[word_num]\n",
219 " out_m.write(word + \"\\n\")\n",
220 " out_v.write('\\t'.join([str(x) for x in embeddings]) + \"\\n\")\n",
221 "out_v.close()\n",
222 "out_m.close()"
223 ],
224 "execution_count": null,
225 "outputs": []
226 },
227 {
228 "cell_type": "code",
229 "metadata": {
230 "id": "VDeqpOCVydtq"
231 },
232 "source": [
233 "try:\n",
234 " from google.colab import files\n",
235 "except ImportError:\n",
236 " pass\n",
237 "else:\n",
238 " files.download('vecs.tsv')\n",
239 " files.download('meta.tsv')"
240 ],
241 "execution_count": null,
242 "outputs": []
243 },
244 {
245 "cell_type": "code",
246 "metadata": {
247 "id": "YRxoxc2apscY"
248 },
249 "source": [
250 "sentence = \"I really think this is amazing. honest.\"\n",
251 "sequence = tokenizer.texts_to_sequences([sentence])\n",
252 "print(sequence)"
253 ],
254 "execution_count": null,
255 "outputs": []
256 }
257 ]
258 }