Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img width=\"800px\" src=\"../fidle/img/00-Fidle-header-01.svg\"></img>\n",
"\n",
"# <!-- TITLE --> [IMDB1] - Sentiment analysis with hot-one encoding\n",
"<!-- DESC --> A basic example of sentiment analysis with sparse encoding, using a dataset from Internet Movie Database (IMDB)\n",
"<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->\n",
"\n",
"## Objectives :\n",
" - The objective is to guess whether film reviews are **positive or negative** based on the analysis of the text. \n",
" - Understand the management of **textual data** and **sentiment analysis**\n",
"\n",
"Original dataset can be find **[there](http://ai.stanford.edu/~amaas/data/sentiment/)** \n",
"Note that [IMDb.com](https://imdb.com) offers several easy-to-use [datasets](https://www.imdb.com/interfaces/) \n",
"For simplicity's sake, we'll use the dataset directly [embedded in Keras](https://www.tensorflow.org/api_docs/python/tf/keras/datasets)\n",
"\n",
"## What we're going to do :\n",
"\n",
" - Retrieve data\n",
" - Preparing the data\n",
" - Build a model\n",
" - Train the model\n",
" - Evaluate the result\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1 - Import and init\n",
"### 1.1 - Python stuff"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import tensorflow as tf\n",
"import tensorflow.keras as keras\n",
"import tensorflow.keras.datasets.imdb as imdb\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib\n",
"\n",
"import pandas as pd\n",
"\n",
"import os,sys,h5py,json\n",
"from importlib import reload\n",
"\n",
"sys.path.append('..')\n",
"import fidle.pwk as pwk\n",
"\n",
"run_dir = './run/IMDB1'\n",
"datasets_dir = pwk.init('IMDB1', run_dir)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.2 - Parameters\n",
"The words in the vocabulary are classified from the most frequent to the rarest. \n",
"`vocab_size` is the number of words we will remember in our vocabulary (the other words will be considered as unknown). \n",
"`hide_most_frequently` is the number of ignored words, among the most common ones "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vocab_size = 10000\n",
"hide_most_frequently = 0\n",
"\n",
"epochs = 10\n",
"batch_size = 512"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Override parameters (batch mode) - Just forget this cell"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pwk.override('vocab_size', 'hide_most_frequently', 'batch_size', 'epochs')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2 - Understanding hot-one encoding\n",
"#### We have a **sentence** and a **dictionary** :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sentence = \"I've never seen a movie like this before\"\n",
"\n",
"dictionary = {\"a\":0, \"before\":1, \"fantastic\":2, \"i've\":3, \"is\":4, \"like\":5, \"movie\":6, \"never\":7, \"seen\":8, \"this\":9}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### We encode our sentence as a **numerical vector** :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sentence_words = sentence.lower().split()\n",
"\n",
"sentence_vect = [ dictionary[w] for w in sentence_words ]\n",
"\n",
"print('Words sentence are : ', sentence_words)\n",
"print('Our vectorized sentence is : ', sentence_vect)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Next, we **one-hot** encode our vectorized sentence as a tensor :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ---- We get a (sentence length x vector size) matrix of zeros\n",
"#\n",
"onehot = np.zeros( (10,8) )\n",
"\n",
"# ---- We set some 1 for each word\n",
"#\n",
"for i,w in enumerate(sentence_vect):\n",
" onehot[w,i]=1\n",
"\n",
"# --- Show it\n",
"#\n",
"print('In a basic way :\\n\\n', onehot, '\\n\\nWith a pandas wiew :\\n')\n",
"data={ f'{sentence_words[i]:.^10}':onehot[:,i] for i,w in enumerate(sentence_vect) }\n",
"df=pd.DataFrame(data)\n",
"df.index=dictionary.keys()\n",
"df.style.set_precision(0).highlight_max(axis=0).set_properties(**{'text-align': 'center'})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2 - Retrieve data\n",
"\n",
"IMDb dataset can bet get directly from Keras - see [documentation](https://www.tensorflow.org/api_docs/python/tf/keras/datasets) \n",
"Note : Due to their nature, textual data can be somewhat complex.\n",
"\n",
"### 2.1 - Data structure : \n",
"The dataset is composed of 2 parts: \n",
"\n",
" - **reviews**, this will be our **x**\n",
" - **opinions** (positive/negative), this will be our **y**\n",
"\n",
"There are also a **dictionary**, because words are indexed in reviews\n",
"\n",
"```\n",
"<dataset> = (<reviews>, <opinions>)\n",
"\n",
"with : <reviews> = [ <review1>, <review2>, ... ]\n",
" <opinions> = [ <rate1>, <rate2>, ... ] where <ratei> = integer\n",
"\n",
"where : <reviewi> = [ <w1>, <w2>, ...] <wi> are the index (int) of the word in the dictionary\n",
" <ratei> = int 0 for negative opinion, 1 for positive\n",
"\n",
"\n",
"<dictionary> = [ <word1>:<w1>, <word2>:<w2>, ... ]\n",
"\n",
"with : <wordi> = word\n",
" <wi> = int\n",
"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.2 - Load dataset\n",
"For simplicity, we will use a pre-formatted dataset - See [documentation](https://www.tensorflow.org/api_docs/python/tf/keras/datasets/imdb/load_data) \n",
"However, Keras offers some usefull tools for formatting textual data - See [documentation](https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/text) \n",
"\n",
"By default : \n",
" - Start of a sequence will be marked with : 1\n",
" - Out of vocabulary word will be : 2\n",
" - First index will be : 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ----- Retrieve x,y\n",
"#\n",
"(x_train, y_train), (x_test, y_test) = imdb.load_data( num_words=vocab_size, skip_top=hide_most_frequently)\n",
"\n",
"y_train = np.asarray(y_train).astype('float32')\n",
"y_test = np.asarray(y_test ).astype('float32')\n",
"\n",
"# ---- About\n",
"#\n",
"print(\"Max(x_train,x_test) : \", pwk.rmax([x_train,x_test]) )\n",
"print(\"Min(x_train,x_test) : \", pwk.rmin([x_train,x_test]) )\n",
"print(\"x_train : {} y_train : {}\".format(x_train.shape, y_train.shape))\n",
"print(\"x_test : {} y_test : {}\".format(x_test.shape, y_test.shape))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3 - About our dataset\n",
"When we loaded the dataset, we asked for using \\<start\\> as 1, \\<unknown word\\> as 2 \n",
"So, we shifted the dataset by 3 with the parameter index_from=3\n",
"\n",
"### 3.1 - Sentences encoding"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('\\nReview example (x_train[12]) :\\n\\n',x_train[12])\n",
"print('\\nOpinions (y_train) :\\n\\n',y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.2 - Load dictionary"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ---- Retrieve dictionary {word:index}, and encode it in ascii\n",
"#\n",
"word_index = imdb.get_word_index()\n",
"\n",
"# ---- Shift the dictionary from +3\n",
"#\n",
"word_index = {w:(i+3) for w,i in word_index.items()}\n",
"\n",
"# ---- Add <pad>, <start> and <unknown> tags\n",
"#\n",
"word_index.update( {'<pad>':0, '<start>':1, '<unknown>':2, '<undef>':3,} )\n",
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
"\n",
"# ---- Create a reverse dictionary : {index:word}\n",
"#\n",
"index_word = {index:word for word,index in word_index.items()} \n",
"\n",
"# ---- About dictionary\n",
"#\n",
"print('\\nDictionary size : ', len(word_index))\n",
"print('\\nSmall extract :\\n')\n",
"for k in range(440,455):print(f' {k:2d} : {index_word[k]}' )\n",
"\n",
"# ---- Add a nice function to transpose :\n",
"#\n",
"def dataset2text(review):\n",
" return ' '.join([index_word.get(i, '?') for i in review])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.3 - Have a look, for human"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pwk.subtitle('Review example :')\n",
"print(x_train[12])\n",
"pwk.subtitle('After translation :')\n",
"print(dataset2text(x_train[12]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.4 - Few statistics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sizes=[len(i) for i in x_train]\n",
"plt.figure(figsize=(16,6))\n",
"plt.hist(sizes, bins=400)\n",
"plt.gca().set(title='Distribution of reviews by size - [{:5.2f}, {:5.2f}]'.format(min(sizes),max(sizes)), \n",
" xlabel='Size', ylabel='Density', xlim=[0,1500])\n",
"pwk.save_fig('01-stats-sizes')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"unk=[ 100*(s.count(2)/len(s)) for s in x_train]\n",
"plt.figure(figsize=(16,6))\n",
"plt.hist(unk, bins=100)\n",
"plt.gca().set(title='Percent of unknown words - [{:5.2f}, {:5.2f}]'.format(min(unk),max(unk)), \n",
" xlabel='# unknown', ylabel='Density', xlim=[0,30])\n",
"pwk.save_fig('02-stats-unknown')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3 - Basic approach with \"one-hot\" vector encoding\n",
"Basic approach. \n",
"\n",
"Each sentence is encoded with a **vector** of length equal to the **size of the dictionary**. \n",
"\n",
"Each sentence will therefore be encoded with a simple vector. \n",
"The value of each component is 0 if the word is not present in the sentence or 1 if the word is present.\n",
"\n",
"For a sentence s=[3,4,7] and a dictionary of 10 words... \n",
"We wil have a vector v=[0,0,0,1,1,0,0,1,0,0,0]\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.1 - Our one-hot encoder"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def one_hot_encoder(x, vector_size=10000):\n",
" \n",
" # ---- Set all to 0\n",
" #\n",
" x_encoded = np.zeros((len(x), vector_size))\n",
" \n",
" # ---- For each sentence\n",
" #\n",
" for i,sentence in enumerate(x):\n",
" for word in sentence:\n",
" x_encoded[i, word] = 1.\n",
"\n",
" return x_encoded"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.2 - Encoding.."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x_train = one_hot_encoder(x_train)\n",
"x_test = one_hot_encoder(x_test)\n",
"\n",
"print(\"To have a look, x_train[12] became :\", x_train[12] )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4 - Build the model\n",
"Few remarks :\n",
" - We'll choose a dense vector size for the embedding output with **dense_vector_size**\n",
" - **GlobalAveragePooling1D** do a pooling on the last dimension : (None, lx, ly) -> (None, ly) \n",
" In other words: we average the set of vectors/words of a sentence\n",
" - L'embedding de Keras fonctionne de manière supervisée. Il s'agit d'une couche de *vocab_size* neurones vers *n_neurons* permettant de maintenir une table de vecteurs (les poids constituent les vecteurs). Cette couche ne calcule pas de sortie a la façon des couches normales, mais renvois la valeur des vecteurs. n mots => n vecteurs (ensuite empilés par le pooling) \n",
"Voir : [Explication plus détaillée (en)](https://stats.stackexchange.com/questions/324992/how-the-embedding-layer-is-trained-in-keras-embedding-layer) \n",
"ainsi que : [Sentiment detection with Keras](https://www.liip.ch/en/blog/sentiment-detection-with-keras-word-embeddings-and-lstm-deep-learning-networks) \n",
"\n",
"More documentation about this model functions :\n",
" - [Embedding](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Embedding)\n",
" - [GlobalAveragePooling1D](https://www.tensorflow.org/api_docs/python/tf/keras/layers/GlobalAveragePooling1D)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_model(vector_size=10000):\n",
" \n",
" model = keras.Sequential()\n",
" model.add(keras.layers.Input( shape=(vector_size,) ))\n",
" model.add(keras.layers.Dense( 32, activation='relu'))\n",
" model.add(keras.layers.Dense( 32, activation='relu'))\n",
" model.add(keras.layers.Dense( 1, activation='sigmoid'))\n",
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
" \n",
" model.compile(optimizer = 'rmsprop',\n",
" loss = 'binary_crossentropy',\n",
" metrics = ['accuracy'])\n",
" return model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5 - Train the model\n",
"### 5.1 - Get it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = get_model(vector_size=vocab_size)\n",
"\n",
"model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2 - Add callback"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.makedirs(f'{run_dir}/models', mode=0o750, exist_ok=True)\n",
"save_dir = f'{run_dir}/models/best_model.h5'\n",
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
"savemodel_callback = tf.keras.callbacks.ModelCheckpoint(filepath=save_dir, verbose=0, save_best_only=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.1 - Train it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"\n",
"history = model.fit(x_train,\n",
" y_train,\n",
" epochs = epochs,\n",
" batch_size = batch_size,\n",
" validation_data = (x_test, y_test),\n",
" verbose = 1,\n",
" callbacks = [savemodel_callback])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6 - Evaluate\n",
"### 6.1 - Training history"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pwk.plot_history(history, save_as='02-history')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 6.2 - Reload and evaluate best model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = keras.models.load_model(f'{run_dir}/models/best_model.h5')\n",
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
"\n",
"# ---- Evaluate\n",
"score = model.evaluate(x_test, y_test, verbose=0)\n",
"\n",
"print('x_test / loss : {:5.4f}'.format(score[0]))\n",
"print('x_test / accuracy : {:5.4f}'.format(score[1]))\n",
"\n",
"values=[score[1], 1-score[1]]\n",
"pwk.plot_donut(values,[\"Accuracy\",\"Errors\"], title=\"#### Accuracy donut is :\", save_as='03-donut')\n",
"\n",
"# ---- Confusion matrix\n",
"\n",
"y_sigmoid = model.predict(x_test)\n",
"\n",
"y_pred = y_sigmoid.copy()\n",
"y_pred[ y_sigmoid< 0.5 ] = 0\n",
"y_pred[ y_sigmoid>=0.5 ] = 1 \n",
"\n",
"pwk.display_confusion_matrix(y_test,y_pred,labels=range(2))\n",
"pwk.plot_confusion_matrix(y_test,y_pred,range(2), figsize=(8, 8),normalize=False, save_as='04-confusion-matrix')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pwk.end()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"<img width=\"80px\" src=\"../fidle/img/00-Fidle-logo-01.svg\"></img>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}