-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path05_cta_notebook2.html
647 lines (638 loc) · 64.9 KB
/
05_cta_notebook2.html
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
289
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
454
455
456
457
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
497
498
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
556
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-0.9.165">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Word embedding</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="05_cta_notebook2_files/libs/clipboard/clipboard.min.js"></script>
<script src="05_cta_notebook2_files/libs/quarto-html/quarto.js"></script>
<script src="05_cta_notebook2_files/libs/quarto-html/popper.min.js"></script>
<script src="05_cta_notebook2_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="05_cta_notebook2_files/libs/quarto-html/anchor.min.js"></script>
<link href="05_cta_notebook2_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link id="quarto-text-highlighting-styles" href="05_cta_notebook2_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet">
<script src="05_cta_notebook2_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="05_cta_notebook2_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="05_cta_notebook2_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title"><h1 class="title display-7">Word embedding</h1><p class="subtitle lead">SICSS, 2022</p></div></header>
<section id="word-embedding-notebook" class="level1">
<h1>Word embedding notebook</h1>
<p>This hands-on exercise focuses on word embedding and provides an overview of the data structures, and functions relevant for, estimating word vectors for word-embedding analyses.</p>
<p>In this tutorial, you will learn how to:</p>
<ul>
<li>Generate word vectors (embeddings) via SVD</li>
<li>Train a local word embedding model in GloVe</li>
<li>Visualize and inspect results</li>
<li>Load and examine pre-trained embeddings</li>
</ul>
<p>Note: Adapts from tutorials by Chris Bail <a href="https://cbail.github.io/textasdata/word2vec/rmarkdown/word2vec.html">here</a> and Julia Silge <a href="https://juliasilge.com/blog/tidy-word-vectors/">here</a> and Emil Hvitfeldt and Julia Silge <a href="https://smltar.com/">here</a>.</p>
<section id="setup" class="level2">
<h2 class="anchored" data-anchor-id="setup">Setup</h2>
<div class="cell">
<div class="sourceCode" id="cb1"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse) <span class="co"># loads dplyr, ggplot2, and others</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(stringr) <span class="co"># to handle text elements</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidytext) <span class="co"># includes set of functions useful for manipulating text</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggthemes) <span class="co"># to make your plots look nice</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(text2vec) <span class="co"># for word embedding implementation</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(widyr) <span class="co"># for reshaping the text data</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(irlba) <span class="co"># for svd</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(umap) <span class="co"># for dimensionality reduction</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We begin by reading in the data. These data come from a sample of 1m tweets by elected UK MPs over the period 2017-2019. The data contain just the name of the MP-user, the text of the tweet, and the MP’s party. We then just add an ID variable called “postID.”</p>
<div class="cell">
<div class="sourceCode" id="cb2"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>twts_sample <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="st">"data/twts_corpus_sample.rds"</span>)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co">#create tweet id</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>twts_sample<span class="sc">$</span>postID <span class="ot"><-</span> <span class="fu">row.names</span>(twts_sample)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>If you’re working on this document from your own computer (“locally”) you can download the tweets sample data in the following way:</p>
<div class="cell">
<div class="sourceCode" id="cb3"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>twts_sample <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="fu">gzcon</span>(<span class="fu">url</span>(<span class="st">"https://github.com/cjbarrie/CTA-ED/blob/main/data/wordembed/twts_corpus_sample.rds?raw=true"</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="word-vectors-via-svd" class="level2">
<h2 class="anchored" data-anchor-id="word-vectors-via-svd">Word vectors via SVD</h2>
<p>We’re going to set about generating a set of word vectors with from our text data. Note that many word embedding applications will use pre-trained embeddings from a much larger corpus, or will generate local embeddings using neural net-based approaches.</p>
<p>Here, we’re instead going to generate a set of embeddings or word vectors by making a series of calculations based on the frequencies with which words appear in different contexts. We will then use a technique called the “Singular Value Decomposition” (SVD). This is a dimensionality reduction technique where the first axis of the resulting composition is designed to capture the most variance, the second the second-most etc…</p>
<p>How do we achieve this?</p>
</section>
<section id="implementation" class="level2">
<h2 class="anchored" data-anchor-id="implementation">Implementation</h2>
<p>The first thing we need to do is to get our data in the right format to calculate so-called “skip-gram probabilties.” If you go through the code line by the line in the below you will begin to understand what these are.</p>
<p>What’s going on?</p>
<p>Well, we’re first unnesting our tweet data as in previous exercises. But importantly, here, we’re not unnesting to individual tokens but to ngrams of length 6 or, in other words, for postID n with words k indexed by i, we take words i<sub>1</sub> …i<sub>6</sub>, then we take words i<sub>2</sub> …i<sub>7</sub>. Try just running the first two lines of the code below to see what this means in practice.</p>
<p>After this, we make a unique ID for the particular ngram we create for each postID, and then we make a unique skipgramID for each postID and ngram. And then we unnest the words of each ngram associated with each skipgramID.</p>
<p>You can see the resulting output below.</p>
<div class="cell">
<div class="sourceCode" id="cb4"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co">#create context window with length 6</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>tidy_skipgrams <span class="ot"><-</span> twts_sample <span class="sc">%>%</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">unnest_tokens</span>(ngram, tweet, <span class="at">token =</span> <span class="st">"ngrams"</span>, <span class="at">n =</span> <span class="dv">6</span>) <span class="sc">%>%</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">ngramID =</span> <span class="fu">row_number</span>()) <span class="sc">%>%</span> </span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> tidyr<span class="sc">::</span><span class="fu">unite</span>(skipgramID, postID, ngramID) <span class="sc">%>%</span></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">unnest_tokens</span>(word, ngram)</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(tidy_skipgrams, <span class="at">n=</span><span class="dv">20</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="cell-output-stdout">
<pre><code># A tibble: 20 × 4
username party_value skipgramID word
<chr> <chr> <chr> <chr>
1 kirstysnp Scottish National Party 1_1 in
2 kirstysnp Scottish National Party 1_1 amongst
3 kirstysnp Scottish National Party 1_1 all
4 kirstysnp Scottish National Party 1_1 the
5 kirstysnp Scottish National Party 1_1 horror
6 kirstysnp Scottish National Party 1_1 at
7 kirstysnp Scottish National Party 1_2 amongst
8 kirstysnp Scottish National Party 1_2 all
9 kirstysnp Scottish National Party 1_2 the
10 kirstysnp Scottish National Party 1_2 horror
11 kirstysnp Scottish National Party 1_2 at
12 kirstysnp Scottish National Party 1_2 the
13 kirstysnp Scottish National Party 1_3 all
14 kirstysnp Scottish National Party 1_3 the
15 kirstysnp Scottish National Party 1_3 horror
16 kirstysnp Scottish National Party 1_3 at
17 kirstysnp Scottish National Party 1_3 the
18 kirstysnp Scottish National Party 1_3 notion
19 kirstysnp Scottish National Party 1_4 the
20 kirstysnp Scottish National Party 1_4 horror </code></pre>
</div>
</div>
<p>What next?</p>
<p>Well we can now calculate a set of probabilities from our skipgrams. We do so with the <code>pairwise_count()</code> function from the <tt>widyr</tt> package. Essentially, this function is saying: for each skipgramID count the number of times a word appears with another word for that feature (where the feature is the skipgramID). We set <code>diag</code> to <code>TRUE</code> when we also want to count the number of times a word appears near itself.</p>
<p>The probability we are then calculating is the number of times a word appears with another word denominated by the total number of word pairings across the whole corpus.</p>
<div class="cell">
<div class="sourceCode" id="cb6"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co">#calculate probabilities</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>skipgram_probs <span class="ot"><-</span> tidy_skipgrams <span class="sc">%>%</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">pairwise_count</span>(word, skipgramID, <span class="at">diag =</span> <span class="cn">TRUE</span>, <span class="at">sort =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span> <span class="co"># diag = T means that we also count when the word appears twice within the window</span></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">p =</span> n <span class="sc">/</span> <span class="fu">sum</span>(n))</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(skipgram_probs[<span class="dv">1000</span><span class="sc">:</span><span class="dv">1020</span>,], <span class="at">n=</span><span class="dv">20</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="cell-output-stderr">
<pre><code>Warning: `distinct_()` was deprecated in dplyr 0.7.0.
Please use `distinct()` instead.
See vignette('programming') for more help
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.</code></pre>
</div>
<div class="cell-output-stdout">
<pre><code># A tibble: 20 × 4
item1 item2 n p
<chr> <chr> <dbl> <dbl>
1 no to 4100 0.0000531
2 vote for 4099 0.0000531
3 for vote 4099 0.0000531
4 see the 4078 0.0000528
5 the see 4078 0.0000528
6 having having 4076 0.0000528
7 by of 4065 0.0000527
8 of by 4065 0.0000527
9 this with 4051 0.0000525
10 with this 4051 0.0000525
11 set set 4050 0.0000525
12 right the 4045 0.0000524
13 the right 4045 0.0000524
14 what the 4044 0.0000524
15 going to 4044 0.0000524
16 the what 4044 0.0000524
17 to going 4044 0.0000524
18 evening evening 4035 0.0000523
19 get the 4032 0.0000522
20 the get 4032 0.0000522</code></pre>
</div>
</div>
<p>So we see, for example, the words vote and for appear 4099 times together. Denominating that by the total n of word pairings (or <code>sum(skipgram_probs$n)</code>), gives us our probability p. </p>
<p>Okay, now we have our skipgram probabilities we need to get our “unigram probabilities” in order to normalize the skipgram probabilities before applying the singular value decomposition.</p>
<p>What is a “unigram probability”? Well, this is just a technical way of saying: count up all the appearances of a given word in our corpus then divide that by the total number of words in our corpus. And we can do this as such:</p>
<div class="cell">
<div class="sourceCode" id="cb9"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co">#calculate unigram probabilities (used to normalize skipgram probabilities later)</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>unigram_probs <span class="ot"><-</span> twts_sample <span class="sc">%>%</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">unnest_tokens</span>(word, tweet) <span class="sc">%>%</span></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(word, <span class="at">sort =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span></span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">p =</span> n <span class="sc">/</span> <span class="fu">sum</span>(n))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Finally, it’s time to normalize our skipgram probabilities.</p>
<p>We take our skipgram probabilities, we filter out word pairings that appear twenty times or less. We rename our words “item1” and “item2,” we merge in the unigram probabilities for both words.</p>
<p>And then we calculate the joint probability as the skipgram probability divided by the unigram probability for the first word in the pairing divided by the unigram probability for the second word in the pairing. This is equivalent to: P(x,y)/P(x)P(y).</p>
<p>In essence, the interpretation of this value is: <em>“do events (words) x and y occur together more often than we would expect if they were independent”</em>?</p>
<p>Once we’ve recovered these normalized probabilities, we can have a look at the joint probabilities for a given item, i.e., word. Here, we look at the word “brexit” and look at those words with the highest value for “p_together.”</p>
<p>Higher values greater than 1 indicate that the words are more likely to appear close to each other; low values less than 1 indicate that they are unlikely to appear close to each other. This, in other words, gives an indication of the association of two words.</p>
<div class="cell">
<div class="sourceCode" id="cb10"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co">#normalize skipgram probabilities</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>normalized_prob <span class="ot"><-</span> skipgram_probs <span class="sc">%>%</span></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(n <span class="sc">></span> <span class="dv">20</span>) <span class="sc">%>%</span> <span class="co">#filter out skipgrams with n <=20</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">rename</span>(<span class="at">word1 =</span> item1, <span class="at">word2 =</span> item2) <span class="sc">%>%</span></span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">left_join</span>(unigram_probs <span class="sc">%>%</span></span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="at">word1 =</span> word, <span class="at">p1 =</span> p),</span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a> <span class="at">by =</span> <span class="st">"word1"</span>) <span class="sc">%>%</span></span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">left_join</span>(unigram_probs <span class="sc">%>%</span></span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="at">word2 =</span> word, <span class="at">p2 =</span> p),</span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a> <span class="at">by =</span> <span class="st">"word2"</span>) <span class="sc">%>%</span></span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">p_together =</span> p <span class="sc">/</span> p1 <span class="sc">/</span> p2)</span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true" tabindex="-1"></a>normalized_prob <span class="sc">%>%</span> </span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(word1 <span class="sc">==</span> <span class="st">"brexit"</span>) <span class="sc">%>%</span></span>
<span id="cb10-15"><a href="#cb10-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="sc">-</span>p_together)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-stdout">
<pre><code># A tibble: 1,016 × 7
word1 word2 n p p1 p2 p_together
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 brexit brexit 38517 0.000499 0.00279 0.00279 64.0
2 brexit softer 50 0.000000648 0.00279 0.00000484 48.0
3 brexit dividend 176 0.00000228 0.00279 0.0000201 40.7
4 brexit scotlandsplaceineurope 37 0.000000479 0.00279 0.00000446 38.5
5 brexit botched 129 0.00000167 0.00279 0.0000208 28.7
6 brexit gridlock 30 0.000000389 0.00279 0.00000521 26.7
7 brexit deadlock 120 0.00000155 0.00279 0.0000242 23.0
8 brexit preparedness 22 0.000000285 0.00279 0.00000446 22.9
9 brexit soft 89 0.00000115 0.00279 0.0000190 21.8
10 brexit weaken 24 0.000000311 0.00279 0.00000521 21.4
# … with 1,006 more rows</code></pre>
</div>
</div>
<p>Using this normalized probabilities, we then calculate the PMI or “Pointwise Mutual Information” value, which is simply the log of the joint probability we calculated above.</p>
<p><strong>Definition time</strong>: “PMI is logarithm of the probability of finding two words together, normalized for the probability of finding each of the words alone.”</p>
<p>We then cast our word pairs into a sparse matrix where values correspond to the PMI between two corresponding words.</p>
<div class="cell">
<div class="sourceCode" id="cb12"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>pmi_matrix <span class="ot"><-</span> normalized_prob <span class="sc">%>%</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">pmi =</span> <span class="fu">log10</span>(p_together)) <span class="sc">%>%</span></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">cast_sparse</span>(word1, word2, pmi)</span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a><span class="co">#remove missing data</span></span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a>pmi_matrix<span class="sc">@</span>x[<span class="fu">is.na</span>(pmi_matrix<span class="sc">@</span>x)] <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a><span class="co">#run SVD</span></span>
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a>pmi_svd <span class="ot"><-</span> <span class="fu">irlba</span>(pmi_matrix, <span class="dv">256</span>, <span class="at">maxit =</span> <span class="dv">500</span>)</span>
<span id="cb12-9"><a href="#cb12-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-10"><a href="#cb12-10" aria-hidden="true" tabindex="-1"></a><span class="fu">glimpse</span>(pmi_matrix)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="cell-output-stdout">
<pre><code>Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..@ i : int [1:350700] 0 1 2 3 4 5 6 7 8 9 ...
..@ p : int [1:21173] 0 7819 14360 20175 25467 29910 34368 39207 43376 46401 ...
..@ Dim : int [1:2] 21172 21172
..@ Dimnames:List of 2
.. ..$ : chr [1:21172] "the" "to" "and" "of" ...
.. ..$ : chr [1:21172] "the" "to" "and" "of" ...
..@ x : num [1:350700] 0.65326 -0.01948 -0.00645 0.27136 -0.52462 ...
..@ factors : list()</code></pre>
</div>
</div>
<p>Notice here that we are setting the vector size to equal 256. This just means that we have a vector length of 256 for any given word.</p>
<p>That is, the set of numbers used to represent a word has length limited to 256. This is arbitrary and can be changed. Typically, a size in the low hundreds is chosen when representing a word as a vector.</p>
<p>The word vectors are then taken as the “u” column, or the left-singular vectors, of the SVD.</p>
<div class="cell">
<div class="sourceCode" id="cb14"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="co">#next we output the word vectors:</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>word_vectors <span class="ot"><-</span> pmi_svd<span class="sc">$</span>u</span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="fu">rownames</span>(word_vectors) <span class="ot"><-</span> <span class="fu">rownames</span>(pmi_matrix)</span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a><span class="fu">dim</span>(word_vectors)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-stdout">
<pre><code>[1] 21172 256</code></pre>
</div>
</div>
</section>
<section id="exploration" class="level2">
<h2 class="anchored" data-anchor-id="exploration">Exploration</h2>
<p>We can define a simple function below to then take our word vector, and find the most similar words, or nearest neighbours, for a given word:</p>
<div class="cell">
<div class="sourceCode" id="cb16"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>nearest_words <span class="ot"><-</span> <span class="cf">function</span>(word_vectors, word){</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a> selected_vector <span class="ot">=</span> word_vectors[word,]</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> mult <span class="ot">=</span> <span class="fu">as.data.frame</span>(word_vectors <span class="sc">%*%</span> selected_vector) <span class="co">#dot product of selected word vector and all word vectors</span></span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a> mult <span class="sc">%>%</span></span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">rownames_to_column</span>() <span class="sc">%>%</span></span>
<span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">rename</span>(<span class="at">word =</span> rowname,</span>
<span id="cb16-8"><a href="#cb16-8" aria-hidden="true" tabindex="-1"></a> <span class="at">similarity =</span> V1) <span class="sc">%>%</span></span>
<span id="cb16-9"><a href="#cb16-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">anti_join</span>(<span class="fu">get_stopwords</span>(<span class="at">language =</span> <span class="st">"en"</span>)) <span class="sc">%>%</span></span>
<span id="cb16-10"><a href="#cb16-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="sc">-</span>similarity)</span>
<span id="cb16-11"><a href="#cb16-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-12"><a href="#cb16-12" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb16-13"><a href="#cb16-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-14"><a href="#cb16-14" aria-hidden="true" tabindex="-1"></a>boris_synonyms <span class="ot"><-</span> <span class="fu">nearest_words</span>(word_vectors, <span class="st">"boris"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-stderr">
<pre><code>Joining, by = "word"</code></pre>
</div>
<div class="sourceCode" id="cb18"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>brexit_synonyms <span class="ot"><-</span> <span class="fu">nearest_words</span>(word_vectors, <span class="st">"brexit"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-stderr">
<pre><code>Joining, by = "word"</code></pre>
</div>
<div class="sourceCode" id="cb20"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(boris_synonyms, <span class="at">n=</span><span class="dv">10</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-stdout">
<pre><code> word similarity
1 johnson 0.10309556
2 boris 0.09940448
3 jeremy 0.04823204
4 trust 0.04800155
5 corbyn 0.04102031
6 farage 0.03973588
7 trump 0.03938184
8 can.t 0.03533624
9 says 0.03324624
10 word 0.03267437</code></pre>
</div>
<div class="sourceCode" id="cb22"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(brexit_synonyms, <span class="at">n=</span><span class="dv">10</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-stdout">
<pre><code> word similarity
1 brexit 0.38737979
2 deal 0.15083433
3 botched 0.05003683
4 tory 0.04377030
5 unleash 0.04233445
6 impact 0.04139872
7 theresa 0.04017608
8 approach 0.03970233
9 handling 0.03901461
10 orderly 0.03897535</code></pre>
</div>
<div class="sourceCode" id="cb24"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="co">#then we can visualize</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>brexit_synonyms <span class="sc">%>%</span></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">selected =</span> <span class="st">"brexit"</span>) <span class="sc">%>%</span></span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">bind_rows</span>(boris_synonyms <span class="sc">%>%</span></span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">selected =</span> <span class="st">"boris"</span>)) <span class="sc">%>%</span></span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(selected) <span class="sc">%>%</span></span>
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">top_n</span>(<span class="dv">15</span>, similarity) <span class="sc">%>%</span></span>
<span id="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">token =</span> <span class="fu">reorder</span>(word, similarity)) <span class="sc">%>%</span></span>
<span id="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(token<span class="sc">!=</span>selected) <span class="sc">%>%</span></span>
<span id="cb24-10"><a href="#cb24-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(token, similarity, <span class="at">fill =</span> selected)) <span class="sc">+</span></span>
<span id="cb24-11"><a href="#cb24-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">show.legend =</span> <span class="cn">FALSE</span>) <span class="sc">+</span></span>
<span id="cb24-12"><a href="#cb24-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>selected, <span class="at">scales =</span> <span class="st">"free"</span>) <span class="sc">+</span></span>
<span id="cb24-13"><a href="#cb24-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#336B87"</span>, <span class="st">"#2A3132"</span>)) <span class="sc">+</span></span>
<span id="cb24-14"><a href="#cb24-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb24-15"><a href="#cb24-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_tufte</span>(<span class="at">base_family =</span> <span class="st">"Helvetica"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="05_cta_notebook2_files/figure-html/unnamed-chunk-26-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="glove-embeddings" class="level2">
<h2 class="anchored" data-anchor-id="glove-embeddings">GloVe Embeddings</h2>
<p>This section adapts from tutorials by Pedro Rodriguez <a href="https://github.com/prodriguezsosa/conText/blob/master/vignettes/quickstart_local_transform.md">here</a> and Dmitriy Selivanov <a href="http://text2vec.org/glove.html">here</a> and Wouter van Gils <a href="https://medium.com/broadhorizon-cmotions/nlp-with-r-part-2-training-word-embedding-models-and-visualize-results-ae444043e234">here</a>.</p>
</section>
<section id="glove-algorithm" class="level2">
<h2 class="anchored" data-anchor-id="glove-algorithm">GloVe algorithm</h2>
<p>This section is taken from <tt>text2vec</tt> package page <a href="http://text2vec.org/glove.html">here</a>.</p>
<p>The GloVe algorithm by pennington_glove_2014 consists of the following steps:</p>
<ol type="1">
<li><p>Collect word co-occurence statistics in a form of word co-ocurrence matrix <span class="math inline">\(X\)</span>. Each element <span class="math inline">\(X_{ij}\)</span> of such matrix represents how often word <em>i</em> appears in context of word <em>j</em>. Usually we scan our corpus in the following manner: for each term we look for context terms within some area defined by a <em>window_size</em> before the term and a <em>window_size</em> after the term. Also we give less weight for more distant words, usually using this formula: <span class="math display">\[decay = 1/offset\]</span></p></li>
<li><p>Define soft constraints for each word pair: <span class="math display">\[w_i^Tw_j + b_i + b_j = log(X_{ij})\]</span> Here <span class="math inline">\(w_i\)</span> - vector for the main word, <span class="math inline">\(w_j\)</span> - vector for the context word, <span class="math inline">\(b_i\)</span>, <span class="math inline">\(b_j\)</span> are scalar biases for the main and context words.</p></li>
<li><p>Define a cost function <span class="math display">\[J = \sum_{i=1}^V \sum_{j=1}^V \; f(X_{ij}) ( w_i^T w_j + b_i + b_j - \log X_{ij})^2\]</span> Here <span class="math inline">\(f\)</span> is a weighting function which help us to prevent learning only from extremely common word pairs. The GloVe authors choose the following function:</p></li>
</ol>
<p><span class="math display">\[
f(X_{ij}) =
\begin{cases}
(\frac{X_{ij}}{x_{max}})^\alpha & \text{if } X_{ij} < XMAX \\
1 & \text{otherwise}
\end{cases}
\]</span></p>
<p>How do we go about implementing this algorithm in R?</p>
<p>Let’s first make sure we have loaded the packages we need:</p>
<div class="cell">
<div class="sourceCode" id="cb25"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(text2vec) <span class="co"># for implementation of GloVe algorithm</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(stringr) <span class="co"># to handle text strings</span></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(umap) <span class="co"># for dimensionality reduction later on</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="implementation-1" class="level2">
<h2 class="anchored" data-anchor-id="implementation-1">Implementation</h2>
<p>We then need to set some of the choice parameters of the GloVe model. The first is the window size <code>WINDOW_SIZE</code>, which, as above, is arbitrary but normally set around 6-8. This means we are looking for word context of words up to 6 words around the target word. The image below illustrates this choice parameter for the word “cat” in a given sentence, with increase context window size:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="data/window.png" class="img-fluid figure-img" style="width:50.0%"></p>
<p></p><figcaption aria-hidden="true" class="figure-caption">Context window</figcaption><p></p>
</figure>
</div>
<p>And this will ultimately be understood in matrix format as:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="data/matrix_context.png" class="img-fluid figure-img" style="width:50.0%"></p>
<p></p><figcaption aria-hidden="true" class="figure-caption">Context window</figcaption><p></p>
</figure>
</div>
<p>The iterations parameter <code>ITERS</code> simply sets the maximum number of iterations to allow for model convergence. This number of iterations is relatively high and the model will likely converge before 100 iterations.</p>
<p>The <code>DIM</code> parameter specifies the length of the word vector we want to result (i.e., just as we set a limit of 256 for the SVD approach above). Finally, <code>COUNT_MIN</code> is specifying the minimum count of words that we want to keep. In other words, if a word appears fewer than ten times, it is discarded. Again, this is the same as above where we discarded word pairings that appeared fewer than twenty times.</p>
<div class="cell">
<div class="sourceCode" id="cb26"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ================================ choice parameters</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="co"># ================================</span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a>WINDOW_SIZE <span class="ot"><-</span> <span class="dv">6</span></span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a>DIM <span class="ot"><-</span> <span class="dv">300</span></span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a>ITERS <span class="ot"><-</span> <span class="dv">100</span></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a>COUNT_MIN <span class="ot"><-</span> <span class="dv">10</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We next “shuffle” our text. This just means we are randomly reordering the character vector of tweets.</p>
<div class="cell">
<div class="sourceCode" id="cb27"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="co"># shuffle text</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(42L)</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a>text <span class="ot"><-</span> <span class="fu">sample</span>(twts_sample<span class="sc">$</span>tweet)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We then create a list object, tokenizing the text of each tweet within each item of the list. After this, we create the vocabulary object needed to implement the GloVe algorithm. We do this by creating an “itoken” object with <code>itoken()</code> and then creating a vocabulary with <code>create_vocabulary</code>. We then remove words that do not exceed our specified threshold above with <code>prune_vocabulary()</code>.</p>
<div class="cell">
<div class="sourceCode" id="cb28"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ================================ create vocab ================================</span></span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a>tokens <span class="ot"><-</span> <span class="fu">space_tokenizer</span>(text)</span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a>it <span class="ot"><-</span> <span class="fu">itoken</span>(tokens, <span class="at">progressbar =</span> <span class="cn">FALSE</span>)</span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a>vocab <span class="ot"><-</span> <span class="fu">create_vocabulary</span>(it)</span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a>vocab_pruned <span class="ot"><-</span> <span class="fu">prune_vocabulary</span>(vocab, <span class="at">term_count_min =</span> COUNT_MIN) <span class="co"># keep only words that meet count threshold</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Next up we vectorize our vocabulary and create our term co-occurrence matrix. Again, this is similar to the above where we created a matrix of PMIs for each of the word pairings in our corpus.</p>
<div class="cell">
<div class="sourceCode" id="cb29"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ================================ create term co-occurrence matrix</span></span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a><span class="co"># ================================</span></span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a>vectorizer <span class="ot"><-</span> <span class="fu">vocab_vectorizer</span>(vocab_pruned)</span>
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a>tcm <span class="ot"><-</span> <span class="fu">create_tcm</span>(it, vectorizer, <span class="at">skip_grams_window =</span> WINDOW_SIZE, <span class="at">skip_grams_window_context =</span> <span class="st">"symmetric"</span>, </span>
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a> <span class="at">weights =</span> <span class="fu">rep</span>(<span class="dv">1</span>, WINDOW_SIZE))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Then we set our final model parameters, learning rate and fit the model. This whole process will take some time. To save time when working through this tutorial, you may also download the resulting embedding from the Github repo linked a little further below.</p>
<div class="cell">
<div class="sourceCode" id="cb30"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ================================ set model parameters</span></span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="co"># ================================</span></span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a>glove <span class="ot"><-</span> GlobalVectors<span class="sc">$</span><span class="fu">new</span>(<span class="at">rank =</span> DIM, <span class="at">x_max =</span> <span class="dv">100</span>, <span class="at">learning_rate =</span> <span class="fl">0.05</span>)</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a><span class="co"># ================================ fit model ================================</span></span>
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a>word_vectors_main <span class="ot"><-</span> glove<span class="sc">$</span><span class="fu">fit_transform</span>(tcm, <span class="at">n_iter =</span> ITERS, <span class="at">convergence_tol =</span> <span class="fl">0.001</span>, </span>
<span id="cb30-7"><a href="#cb30-7" aria-hidden="true" tabindex="-1"></a> <span class="at">n_threads =</span> RcppParallel<span class="sc">::</span><span class="fu">defaultNumThreads</span>())</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Finally, we get the resulting word embedding and save it as a .rds file.</p>
<div class="cell">
<div class="sourceCode" id="cb31"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ================================ get output ================================</span></span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a>word_vectors_context <span class="ot"><-</span> glove<span class="sc">$</span>components</span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a>glove_embedding <span class="ot"><-</span> word_vectors_main <span class="sc">+</span> <span class="fu">t</span>(word_vectors_context) <span class="co"># word vectors</span></span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a><span class="co"># ================================ save ================================</span></span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a><span class="fu">saveRDS</span>(glove_embedding, <span class="at">file =</span> <span class="st">"local_glove.rds"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p><strong>To save time when working through this tutorial, you may also download the resulting embedding from the Github repo with</strong>:</p>
<div class="cell">
<div class="sourceCode" id="cb32"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a>url <span class="ot"><-</span> <span class="st">"https://github.com/cjbarrie/CTA-ED/blob/main/data/wordembed/local_glove.rds?raw=true"</span></span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a>glove_embedding <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="fu">url</span>(url, <span class="at">method=</span><span class="st">"libcurl"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="visualization" class="level2">
<h2 class="anchored" data-anchor-id="visualization">Visualization</h2>
<div class="cell">
</div>
<p>How do we explore these embeddings? Well, imagine that our embeddings will look something not dissimilar to this visualization of another embedding <a href="https://anvaka.github.io/pm/#/galaxy/word2vec-wiki?cx=-17&cy=-237&cz=-613&lx=-0.0575&ly=-0.9661&lz=-0.2401&lw=-0.0756&ml=300&s=1.75&l=1&v=d50_clean">here</a>. In other words, we are talking about something that doesn’t lend itself to projection in 2D space!</p>
<p>But…</p>
<p>All hope is not lost, space travellers. A smart technique by <span class="citation" data-cites="mcinnes_umap_2020">@mcinnes_umap_2020</span> linked <a href="https://arxiv.org/abs/1802.03426">here</a> describes a way to reduce the dimensionality of such embedding layers using what is called “Uniform Manifold Approximation and Projection.” How do we do this? Well, happily, with the <tt>umap</tt> package it is pretty straightforward!</p>
<div class="cell">
<div class="sourceCode" id="cb33"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="co"># GloVe dimension reduction</span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a>glove_umap <span class="ot"><-</span> <span class="fu">umap</span>(glove_embedding, <span class="at">n_components =</span> <span class="dv">2</span>, <span class="at">metric =</span> <span class="st">"cosine"</span>, <span class="at">n_neighbors =</span> <span class="dv">25</span>, <span class="at">min_dist =</span> <span class="fl">0.1</span>, <span class="at">spread=</span><span class="dv">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
</div>
<p>Why is this helpful? Well, for a number of reasons, but it is particularly helpful here for visualizing our embeddings in two-dimensional space.</p>
<div class="cell">
<div class="sourceCode" id="cb34"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Put results in a dataframe for ggplot</span></span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a>df_glove_umap <span class="ot"><-</span> <span class="fu">as.data.frame</span>(glove_umap[[<span class="st">"layout"</span>]])</span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Add the labels of the words to the dataframe</span></span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a>df_glove_umap<span class="sc">$</span>word <span class="ot"><-</span> <span class="fu">rownames</span>(df_glove_umap)</span>
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(df_glove_umap) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"UMAP1"</span>, <span class="st">"UMAP2"</span>, <span class="st">"word"</span>)</span>
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-8"><a href="#cb34-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot the UMAP dimensions</span></span>
<span id="cb34-9"><a href="#cb34-9" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(df_glove_umap) <span class="sc">+</span></span>
<span id="cb34-10"><a href="#cb34-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">x =</span> UMAP1, <span class="at">y =</span> UMAP2), <span class="at">colour =</span> <span class="st">'blue'</span>, <span class="at">size =</span> <span class="fl">0.05</span>) <span class="sc">+</span></span>
<span id="cb34-11"><a href="#cb34-11" aria-hidden="true" tabindex="-1"></a> ggplot2<span class="sc">::</span><span class="fu">annotate</span>(<span class="st">"rect"</span>, <span class="at">xmin =</span> <span class="sc">-</span><span class="dv">3</span>, <span class="at">xmax =</span> <span class="sc">-</span><span class="dv">2</span>, <span class="at">ymin =</span> <span class="dv">5</span>, <span class="at">ymax =</span> <span class="dv">7</span>,<span class="at">alpha =</span> .<span class="dv">2</span>) <span class="sc">+</span></span>
<span id="cb34-12"><a href="#cb34-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"GloVe word embedding in 2D using UMAP"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="05_cta_notebook2_files/figure-html/unnamed-chunk-50-1.png" class="img-fluid" width="672"></p>
</div>
<div class="sourceCode" id="cb35"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot the shaded part of the GloVe word embedding with labels</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(df_glove_umap[df_glove_umap<span class="sc">$</span>UMAP1 <span class="sc"><</span> <span class="sc">-</span><span class="fl">2.5</span> <span class="sc">&</span> df_glove_umap<span class="sc">$</span>UMAP1 <span class="sc">></span> <span class="sc">-</span><span class="dv">3</span> <span class="sc">&</span> df_glove_umap<span class="sc">$</span>UMAP2 <span class="sc">></span> <span class="dv">5</span> <span class="sc">&</span> df_glove_umap<span class="sc">$</span>UMAP2 <span class="sc"><</span> <span class="fl">6.5</span>,]) <span class="sc">+</span></span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">x =</span> UMAP1, <span class="at">y =</span> UMAP2), <span class="at">colour =</span> <span class="st">'blue'</span>, <span class="at">size =</span> <span class="dv">2</span>) <span class="sc">+</span></span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_text</span>(<span class="fu">aes</span>(UMAP1, UMAP2, <span class="at">label =</span> word), <span class="at">size =</span> <span class="fl">2.5</span>, <span class="at">vjust=</span><span class="sc">-</span><span class="dv">1</span>, <span class="at">hjust=</span><span class="dv">0</span>) <span class="sc">+</span></span>
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"GloVe word embedding in 2D using UMAP - partial view"</span>) <span class="sc">+</span></span>
<span id="cb35-6"><a href="#cb35-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">plot.title =</span> <span class="fu">element_text</span>(<span class="at">hjust =</span> .<span class="dv">5</span>, <span class="at">size =</span> <span class="dv">14</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="05_cta_notebook2_files/figure-html/unnamed-chunk-50-2.png" class="img-fluid" width="672"></p>
</div>
<div class="sourceCode" id="cb36"><pre class="sourceCode r cell-code code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot the word embedding of words that are related for the GloVe model</span></span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a>word <span class="ot"><-</span> glove_embedding[<span class="st">"economy"</span>,, drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a>cos_sim <span class="ot">=</span> <span class="fu">sim2</span>(<span class="at">x =</span> glove_embedding, <span class="at">y =</span> word, <span class="at">method =</span> <span class="st">"cosine"</span>, <span class="at">norm =</span> <span class="st">"l2"</span>)</span>
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a>select <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="fu">rownames</span>(<span class="fu">as.data.frame</span>(<span class="fu">head</span>(<span class="fu">sort</span>(cos_sim[,<span class="dv">1</span>], <span class="at">decreasing =</span> <span class="cn">TRUE</span>), <span class="dv">25</span>))))</span>
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(select) <span class="ot"><-</span> <span class="st">"word"</span></span>
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a>selected_words <span class="ot"><-</span> df_glove_umap <span class="sc">%>%</span> <span class="fu">inner_join</span>(<span class="at">y=</span>select, <span class="at">by=</span> <span class="st">"word"</span>, <span class="at">match =</span> <span class="st">"all"</span>) </span>
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-8"><a href="#cb36-8" aria-hidden="true" tabindex="-1"></a><span class="co">#The ggplot visual for GloVe</span></span>
<span id="cb36-9"><a href="#cb36-9" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(selected_words, <span class="fu">aes</span>(<span class="at">x =</span> UMAP1, <span class="at">y =</span> UMAP2)) <span class="sc">+</span> </span>
<span id="cb36-10"><a href="#cb36-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">show.legend =</span> <span class="cn">FALSE</span>) <span class="sc">+</span> </span>
<span id="cb36-11"><a href="#cb36-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_text</span>(<span class="fu">aes</span>(UMAP1, UMAP2, <span class="at">label =</span> word), <span class="at">show.legend =</span> <span class="cn">FALSE</span>, <span class="at">size =</span> <span class="fl">2.5</span>, <span class="at">vjust=</span><span class="sc">-</span><span class="fl">1.5</span>, <span class="at">hjust=</span><span class="dv">0</span>) <span class="sc">+</span></span>
<span id="cb36-12"><a href="#cb36-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"GloVe word embedding of words related to 'economy'"</span>) <span class="sc">+</span></span>
<span id="cb36-13"><a href="#cb36-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">plot.title =</span> <span class="fu">element_text</span>(<span class="at">hjust =</span> .<span class="dv">5</span>, <span class="at">size =</span> <span class="dv">14</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="05_cta_notebook2_files/figure-html/unnamed-chunk-50-3.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>We can see, here, then that our embeddings seem to make sense. We zoomed in first on that little outgrowth of our 2D mapping, which seemed to correspond to numbers and number words. Then we looked at words around “economy” and we see other related terms like “growth” and “jobs.”</p>
</section>
<section id="exercises" class="level2">
<h2 class="anchored" data-anchor-id="exercises">Exercises</h2>
<ol type="1">
<li>Inspect and visualize the nearest neighbour synonyms of other relevant words in the tweets corpus</li>
<li>Identify another region of interest in the GloVe-trained model and visualize</li>
</ol>
</section>
</section>
</main>
<!-- /main column -->
<script type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
setTimeout(function() {
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
let href = ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const cites = ref.parentNode.getAttribute('data-cites').split(' ');
tippyHover(ref, function() {
var popup = window.document.createElement('div');
cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
});
</script>
</div> <!-- /content -->
</body></html>