Format: application/octet-stream
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 2,
6 "id": "3f0e4dc5",
7 "metadata": {},
8 "outputs": [],
9 "source": [
10 "import tensorflow_datasets as tfds"
11 ]
12 },
13 {
14 "cell_type": "code",
15 "execution_count": 3,
16 "id": "cf67fff4",
17 "metadata": {},
18 "outputs": [
19 {
20 "name": "stdout",
21 "output_type": "stream",
22 "text": [
23 "\u001b[1mDownloading and preparing dataset Unknown size (download: Unknown size, generated: Unknown size, total: Unknown size) to C:\\Users\\PC\\tensorflow_datasets\\imdb_reviews\\plain_text\\1.0.0...\u001b[0m\n"
24 ]
25 },
26 {
27 "data": {
28 "application/vnd.jupyter.widget-view+json": {
29 "model_id": "a7b64b1fc98b43e5b89b9e528ea07b25",
30 "version_major": 2,
31 "version_minor": 0
32 },
33 "text/plain": [
34 "Dl Completed...: 0 url [00:00, ? url/s]"
35 ]
36 },
37 "metadata": {},
38 "output_type": "display_data"
39 },
40 {
41 "data": {
42 "application/vnd.jupyter.widget-view+json": {
43 "model_id": "21c118e765e44287a6d29c1d9921d7a3",
44 "version_major": 2,
45 "version_minor": 0
46 },
47 "text/plain": [
48 "Dl Size...: 0 MiB [00:00, ? MiB/s]"
49 ]
50 },
51 "metadata": {},
52 "output_type": "display_data"
53 },
54 {
55 "data": {
56 "application/vnd.jupyter.widget-view+json": {
57 "model_id": "",
58 "version_major": 2,
59 "version_minor": 0
60 },
61 "text/plain": [
62 "Generating splits...: 0%| | 0/3 [00:00<?, ? splits/s]"
63 ]
64 },
65 "metadata": {},
66 "output_type": "display_data"
67 },
68 {
69 "data": {
70 "application/vnd.jupyter.widget-view+json": {
71 "model_id": "",
72 "version_major": 2,
73 "version_minor": 0
74 },
75 "text/plain": [
76 "Generating train examples...: 0 examples [00:00, ? examples/s]"
77 ]
78 },
79 "metadata": {},
80 "output_type": "display_data"
81 },
82 {
83 "data": {
84 "application/vnd.jupyter.widget-view+json": {
85 "model_id": "",
86 "version_major": 2,
87 "version_minor": 0
88 },
89 "text/plain": [
90 "Shuffling imdb_reviews-train.tfrecord...: 0%| | 0/25000 [00:00<?, ? examples/s]"
91 ]
92 },
93 "metadata": {},
94 "output_type": "display_data"
95 },
96 {
97 "data": {
98 "application/vnd.jupyter.widget-view+json": {
99 "model_id": "",
100 "version_major": 2,
101 "version_minor": 0
102 },
103 "text/plain": [
104 "Generating test examples...: 0 examples [00:00, ? examples/s]"
105 ]
106 },
107 "metadata": {},
108 "output_type": "display_data"
109 },
110 {
111 "data": {
112 "application/vnd.jupyter.widget-view+json": {
113 "model_id": "",
114 "version_major": 2,
115 "version_minor": 0
116 },
117 "text/plain": [
118 "Shuffling imdb_reviews-test.tfrecord...: 0%| | 0/25000 [00:00<?, ? examples/s]"
119 ]
120 },
121 "metadata": {},
122 "output_type": "display_data"
123 },
124 {
125 "data": {
126 "application/vnd.jupyter.widget-view+json": {
127 "model_id": "",
128 "version_major": 2,
129 "version_minor": 0
130 },
131 "text/plain": [
132 "Generating unsupervised examples...: 0 examples [00:00, ? examples/s]"
133 ]
134 },
135 "metadata": {},
136 "output_type": "display_data"
137 },
138 {
139 "data": {
140 "application/vnd.jupyter.widget-view+json": {
141 "model_id": "",
142 "version_major": 2,
143 "version_minor": 0
144 },
145 "text/plain": [
146 "Shuffling imdb_reviews-unsupervised.tfrecord...: 0%| | 0/50000 [00:00<?, ? examples/s]"
147 ]
148 },
149 "metadata": {},
150 "output_type": "display_data"
151 },
152 {
153 "name": "stdout",
154 "output_type": "stream",
155 "text": [
156 "\u001b[1mDataset imdb_reviews downloaded and prepared to C:\\Users\\PC\\tensorflow_datasets\\imdb_reviews\\plain_text\\1.0.0. Subsequent calls will reuse this data.\u001b[0m\n"
157 ]
158 }
159 ],
160 "source": [
161 "#imdb, info= tfds.load('imdb_reviews', with_info=True, as_supervised=True)"
162 ]
163 },
164 {
165 "cell_type": "code",
166 "execution_count": 4,
167 "id": "16309217",
168 "metadata": {},
169 "outputs": [],
170 "source": [
171 "train_data, test_data= imdb['train'], imdb['test']"
172 ]
173 },
174 {
175 "cell_type": "code",
176 "execution_count": 5,
177 "id": "acf53763",
178 "metadata": {},
179 "outputs": [],
180 "source": [
181 "import numpy as np"
182 ]
183 },
184 {
185 "cell_type": "code",
186 "execution_count": null,
187 "id": "e9169c88",
188 "metadata": {},
189 "outputs": [],
190 "source": []
191 },
192 {
193 "cell_type": "code",
194 "execution_count": 8,
195 "id": "e0af2f79",
196 "metadata": {},
197 "outputs": [],
198 "source": [
199 "\n",
200 "training_sentences = []\n",
201 "training_labels = []\n",
202 "\n",
203 "testing_sentences = []\n",
204 "testing_labels = []\n",
205 "\n",
206 "# str(s.tonumpy()) is needed in Python3 instead of just s.numpy()\n",
207 "for s,l in train_data:\n",
208 " training_sentences.append(s.numpy().decode('utf8'))\n",
209 " training_labels.append(l.numpy())\n",
210 " \n",
211 "for s,l in test_data:\n",
212 " testing_sentences.append(s.numpy().decode('utf8'))\n",
213 " testing_labels.append(l.numpy())\n",
214 " \n",
215 "training_labels_final = np.array(training_labels)\n",
216 "testing_labels_final = np.array(testing_labels)\n"
217 ]
218 },
219 {
220 "cell_type": "code",
221 "execution_count": null,
222 "id": "d6a9c40c",
223 "metadata": {},
224 "outputs": [],
225 "source": []
226 },
227 {
228 "cell_type": "code",
229 "execution_count": 11,
230 "id": "72cec8f1",
231 "metadata": {},
232 "outputs": [],
233 "source": [
234 "vocab_size = 10000\n",
235 "embedding_dim = 16\n",
236 "max_length = 120\n",
237 "trunc_type='post'\n",
238 "oov_tok = \"<OOV>\"\n",
239 "\n",
240 "\n",
241 "from tensorflow.keras.preprocessing.text import Tokenizer\n",
242 "from tensorflow.keras.preprocessing.sequence import pad_sequences\n",
243 "\n",
244 "tokenizer = Tokenizer(num_words = vocab_size, oov_token=oov_tok)\n",
245 "tokenizer.fit_on_texts(training_sentences)\n",
246 "word_index = tokenizer.word_index\n",
247 "sequences = tokenizer.texts_to_sequences(training_sentences)\n",
248 "padded = pad_sequences(sequences,maxlen=max_length, truncating=trunc_type)\n",
249 "\n",
250 "testing_sequences = tokenizer.texts_to_sequences(testing_sentences)\n",
251 "testing_padded = pad_sequences(testing_sequences,maxlen=max_length)\n"
252 ]
253 },
254 {
255 "cell_type": "code",
256 "execution_count": 12,
257 "id": "c0dcf0fb",
258 "metadata": {},
259 "outputs": [
260 {
261 "name": "stdout",
262 "output_type": "stream",
263 "text": [
264 "? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? this is the kind of film for a snowy sunday afternoon when the rest of the world can go ahead with its own business as you <OOV> into a big arm chair and <OOV> for a couple of hours wonderful performances from cher and nicolas cage as always gently row the plot along there are no <OOV> to cross no dangerous waters just a warm and witty <OOV> through new york life at its best a family film in every sense and one that deserves the praise it received\n",
265 "This is the kind of film for a snowy Sunday afternoon when the rest of the world can go ahead with its own business as you descend into a big arm-chair and mellow for a couple of hours. Wonderful performances from Cher and Nicolas Cage (as always) gently row the plot along. There are no rapids to cross, no dangerous waters, just a warm and witty paddle through New York life at its best. A family film in every sense and one that deserves the praise it received.\n"
266 ]
267 }
268 ],
269 "source": [
270 "reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])\n",
271 "\n",
272 "def decode_review(text):\n",
273 " return ' '.join([reverse_word_index.get(i, '?') for i in text])\n",
274 "\n",
275 "print(decode_review(padded[3]))\n",
276 "print(training_sentences[3])"
277 ]
278 },
279 {
280 "cell_type": "code",
281 "execution_count": null,
282 "id": "e686566e",
283 "metadata": {},
284 "outputs": [],
285 "source": []
286 },
287 {
288 "cell_type": "code",
289 "execution_count": 14,
290 "id": "d1eb1544",
291 "metadata": {},
292 "outputs": [],
293 "source": [
294 "import tensorflow as tf"
295 ]
296 },
297 {
298 "cell_type": "code",
299 "execution_count": 15,
300 "id": "37439b38",
301 "metadata": {},
302 "outputs": [
303 {
304 "name": "stdout",
305 "output_type": "stream",
306 "text": [
307 "Model: \"sequential\"\n",
308 "_________________________________________________________________\n",
309 "Layer (type) Output Shape Param # \n",
310 "=================================================================\n",
311 "embedding (Embedding) (None, 120, 16) 160000 \n",
312 "_________________________________________________________________\n",
313 "flatten (Flatten) (None, 1920) 0 \n",
314 "_________________________________________________________________\n",
315 "dense (Dense) (None, 6) 11526 \n",
316 "_________________________________________________________________\n",
317 "dense_1 (Dense) (None, 1) 7 \n",
318 "=================================================================\n",
319 "Total params: 171,533\n",
320 "Trainable params: 171,533\n",
321 "Non-trainable params: 0\n",
322 "_________________________________________________________________\n"
323 ]
324 }
325 ],
326 "source": [
327 "model = tf.keras.Sequential([\n",
328 " tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),\n",
329 " tf.keras.layers.Flatten(),\n",
330 " tf.keras.layers.Dense(6, activation='relu'),\n",
331 " tf.keras.layers.Dense(1, activation='sigmoid')\n",
332 "])\n",
333 "model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])\n",
334 "model.summary()\n"
335 ]
336 },
337 {
338 "cell_type": "code",
339 "execution_count": null,
340 "id": "ec82f16a",
341 "metadata": {},
342 "outputs": [],
343 "source": []
344 },
345 {
346 "cell_type": "code",
347 "execution_count": 16,
348 "id": "8f99ce40",
349 "metadata": {},
350 "outputs": [
351 {
352 "name": "stdout",
353 "output_type": "stream",
354 "text": [
355 "Epoch 1/10\n",
356 "782/782 [==============================] - 3s 4ms/step - loss: 0.4897 - accuracy: 0.7482 - val_loss: 0.3403 - val_accuracy: 0.8504\n",
357 "Epoch 2/10\n",
358 "782/782 [==============================] - 4s 5ms/step - loss: 0.2309 - accuracy: 0.9116 - val_loss: 0.3690 - val_accuracy: 0.8396\n",
359 "Epoch 3/10\n",
360 "782/782 [==============================] - 4s 5ms/step - loss: 0.0766 - accuracy: 0.9824 - val_loss: 0.4656 - val_accuracy: 0.8250\n",
361 "Epoch 4/10\n",
362 "782/782 [==============================] - 4s 4ms/step - loss: 0.0169 - accuracy: 0.9982 - val_loss: 0.5381 - val_accuracy: 0.8250\n",
363 "Epoch 5/10\n",
364 "782/782 [==============================] - 3s 4ms/step - loss: 0.0044 - accuracy: 0.9997 - val_loss: 0.5966 - val_accuracy: 0.8249\n",
365 "Epoch 6/10\n",
366 "782/782 [==============================] - 3s 4ms/step - loss: 0.0015 - accuracy: 1.0000 - val_loss: 0.6405 - val_accuracy: 0.8264\n",
367 "Epoch 7/10\n",
368 "782/782 [==============================] - 3s 4ms/step - loss: 7.7152e-04 - accuracy: 1.0000 - val_loss: 0.6814 - val_accuracy: 0.8265\n",
369 "Epoch 8/10\n",
370 "782/782 [==============================] - 4s 5ms/step - loss: 5.2565e-04 - accuracy: 1.0000 - val_loss: 0.7277 - val_accuracy: 0.8255\n",
371 "Epoch 9/10\n",
372 "782/782 [==============================] - 4s 5ms/step - loss: 2.6606e-04 - accuracy: 1.0000 - val_loss: 0.7623 - val_accuracy: 0.8263\n",
373 "Epoch 10/10\n",
374 "782/782 [==============================] - 3s 4ms/step - loss: 1.4898e-04 - accuracy: 1.0000 - val_loss: 0.7965 - val_accuracy: 0.8258\n"
375 ]
376 },
377 {
378 "data": {
379 "text/plain": [
380 "<tensorflow.python.keras.callbacks.History at 0x202067b78e0>"
381 ]
382 },
383 "execution_count": 16,
384 "metadata": {},
385 "output_type": "execute_result"
386 }
387 ],
388 "source": [
389 "num_epochs = 10\n",
390 "model.fit(padded, training_labels_final, epochs=num_epochs, validation_data=(testing_padded, testing_labels_final))"
391 ]
392 },
393 {
394 "cell_type": "code",
395 "execution_count": null,
396 "id": "bfbc9d49",
397 "metadata": {},
398 "outputs": [],
399 "source": []
400 },
401 {
402 "cell_type": "code",
403 "execution_count": null,
404 "id": "82624b40",
405 "metadata": {},
406 "outputs": [],
407 "source": []
408 },
409 {
410 "cell_type": "code",
411 "execution_count": 17,
412 "id": "5f51dcbc",
413 "metadata": {},
414 "outputs": [
415 {
416 "name": "stdout",
417 "output_type": "stream",
418 "text": [
419 "(10000, 16)\n"
420 ]
421 }
422 ],
423 "source": [
424 "e = model.layers[0]\n",
425 "weights = e.get_weights()[0]\n",
426 "print(weights.shape) # shape: (vocab_size, embedding_dim)"
427 ]
428 },
429 {
430 "cell_type": "code",
431 "execution_count": null,
432 "id": "d80bb061",
433 "metadata": {},
434 "outputs": [],
435 "source": []
436 },
437 {
438 "cell_type": "code",
439 "execution_count": 18,
440 "id": "3819a423",
441 "metadata": {},
442 "outputs": [
443 {
444 "name": "stdout",
445 "output_type": "stream",
446 "text": [
447 "[[11, 64, 102, 12, 7, 478, 1200]]\n"
448 ]
449 }
450 ],
451 "source": [
452 "sentence = \"I really think this is amazing. honest.\"\n",
453 "sequence = tokenizer.texts_to_sequences([sentence])\n",
454 "print(sequence)"
455 ]
456 },
457 {
458 "cell_type": "code",
459 "execution_count": null,
460 "id": "27fe916d",
461 "metadata": {},
462 "outputs": [],
463 "source": []
464 }
465 ],
466 "metadata": {
467 "kernelspec": {
468 "display_name": "Python 3",
469 "language": "python",
470 "name": "python3"
471 },
472 "language_info": {
473 "codemirror_mode": {
474 "name": "ipython",
475 "version": 3
476 },
477 "file_extension": ".py",
478 "mimetype": "text/x-python",
479 "name": "python",
480 "nbconvert_exporter": "python",
481 "pygments_lexer": "ipython3",
482 "version": "3.8.8"
483 }
484 },
485 "nbformat": 4,
486 "nbformat_minor": 5
487 }
488