Skip to content

Commit d115e4d

Browse files
authored
Merge pull request #38 from koheiw/dev-doc2vec
Update README
2 parents 12c626f + 3e636e4 commit d115e4d

4 files changed

Lines changed: 98 additions & 56 deletions

File tree

README.Rmd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ knitr::opts_chunk$set(
1414

1515
# Wordvector: word and document vector models
1616

17-
The **wordvector** package is developed to create word and document vectors using **quanteda**. This package currently supports word2vec ([Mikolov et al., 2013](http://arxiv.org/abs/1310.4546)) and latent semantic analysis ([Deerwester et al., 1990](https://doi.org/10.1002/(SICI)1097-4571(199009)41:6<391::AID-ASI1>3.0.CO;2-9)).
17+
The **wordvector** package is developed to create word and document vectors using **quanteda**. This package currently supports word2vec ([Mikolov et al., 2013](http://arxiv.org/abs/1310.4546)), doc2vec ([Le, Q. V., & Mikolov, T., 2014](https://doi.org/10.48550/arXiv.1405.4053)) and latent semantic analysis ([Deerwester et al., 1990](https://doi.org/10.1002/(SICI)1097-4571(199009)41:6<391::AID-ASI1>3.0.CO;2-9)).
1818

1919
## How to install
2020

@@ -69,7 +69,8 @@ wdv <- textmodel_word2vec(toks, dim = 50, type = "cbow", min_count = 5, verbose
6969
`similarity()` computes cosine similarity between word vectors.
7070

7171
```{r}
72-
head(similarity(wdv, c("amazon", "forests", "obama", "america", "afghanistan"), mode = "word"))
72+
head(similarity(wdv, c("amazon", "forests", "obama", "america", "afghanistan"),
73+
mode = "character"))
7374
```
7475

7576
### Arithmetic operations of word vectors

README.md

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
The **wordvector** package is developed to create word and document
55
vectors using **quanteda**. This package currently supports word2vec
6-
([Mikolov et al., 2013](http://arxiv.org/abs/1310.4546)) and latent
7-
semantic analysis ([Deerwester et al.,
6+
([Mikolov et al., 2013](http://arxiv.org/abs/1310.4546)), doc2vec ([Le,
7+
Q. V., & Mikolov, T., 2014](https://doi.org/10.48550/arXiv.1405.4053))
8+
and latent semantic analysis ([Deerwester et al.,
89
1990](https://doi.org/10.1002/(SICI)1097-4571(199009)41:6%3C391::AID-ASI1%3E3.0.CO;2-9)).
910

1011
## How to install
@@ -41,7 +42,7 @@ download.file('https://www.dropbox.com/s/e19kslwhuu9yc2z/yahoo-news.RDS?dl=1',
4142
``` r
4243
library(wordvector)
4344
library(quanteda)
44-
## Package version: 4.2.1
45+
## Package version: 4.3.1
4546
## Unicode version: 15.1
4647
## ICU version: 74.1
4748
## Parallel computing: 16 of 16 threads used.
@@ -64,16 +65,16 @@ wdv <- textmodel_word2vec(toks, dim = 50, type = "cbow", min_count = 5, verbose
6465
## ...using 16 threads for distributed computing
6566
## ...initializing
6667
## ...negative sampling in 10 iterations
67-
## ......iteration 1 elapsed time: 4.99 seconds (alpha: 0.0465)
68-
## ......iteration 2 elapsed time: 10.00 seconds (alpha: 0.0431)
69-
## ......iteration 3 elapsed time: 14.97 seconds (alpha: 0.0396)
70-
## ......iteration 4 elapsed time: 19.94 seconds (alpha: 0.0362)
71-
## ......iteration 5 elapsed time: 25.11 seconds (alpha: 0.0326)
72-
## ......iteration 6 elapsed time: 30.14 seconds (alpha: 0.0291)
73-
## ......iteration 7 elapsed time: 35.12 seconds (alpha: 0.0257)
74-
## ......iteration 8 elapsed time: 40.22 seconds (alpha: 0.0222)
75-
## ......iteration 9 elapsed time: 45.15 seconds (alpha: 0.0188)
76-
## ......iteration 10 elapsed time: 50.32 seconds (alpha: 0.0152)
68+
## ......iteration 1 elapsed time: 6.44 seconds (alpha: 0.0455)
69+
## ......iteration 2 elapsed time: 13.22 seconds (alpha: 0.0408)
70+
## ......iteration 3 elapsed time: 19.80 seconds (alpha: 0.0363)
71+
## ......iteration 4 elapsed time: 26.97 seconds (alpha: 0.0317)
72+
## ......iteration 5 elapsed time: 34.22 seconds (alpha: 0.0270)
73+
## ......iteration 6 elapsed time: 41.09 seconds (alpha: 0.0224)
74+
## ......iteration 7 elapsed time: 47.71 seconds (alpha: 0.0178)
75+
## ......iteration 8 elapsed time: 54.47 seconds (alpha: 0.0131)
76+
## ......iteration 9 elapsed time: 61.07 seconds (alpha: 0.0085)
77+
## ......iteration 10 elapsed time: 67.54 seconds (alpha: 0.0041)
7778
## ...complete
7879
```
7980

@@ -82,21 +83,22 @@ wdv <- textmodel_word2vec(toks, dim = 50, type = "cbow", min_count = 5, verbose
8283
`similarity()` computes cosine similarity between word vectors.
8384

8485
``` r
85-
head(similarity(wdv, c("amazon", "forests", "obama", "america", "afghanistan"), mode = "word"))
86-
## amazon forests obama america
87-
## [1,] "amazon" "forests" "obama" "america"
88-
## [2,] "rainforest" "herds" "barack" "africa"
89-
## [3,] "plantations" "rainforests" "biden" "dakota"
90-
## [4,] "farms" "rainforest" "kerry" "american"
91-
## [5,] "patagonia" "plantations" "administration" "carolina"
92-
## [6,] "warm-water" "farmland" "hagel" "america-focused"
86+
head(similarity(wdv, c("amazon", "forests", "obama", "america", "afghanistan"),
87+
mode = "character"))
88+
## amazon forests obama america
89+
## [1,] "amazon" "forests" "obama" "america"
90+
## [2,] "rainforest" "herds" "biden" "america-focused"
91+
## [3,] "peat" "rainforests" "relationship-building" "carolina"
92+
## [4,] "re-grown" "farmland" "kerry" "american"
93+
## [5,] "peatlands" "rainforest" "hagel" "dakota"
94+
## [6,] "sunflower" "forest" "clinton" "africa"
9395
## afghanistan
9496
## [1,] "afghanistan"
9597
## [2,] "afghan"
96-
## [3,] "kabul"
97-
## [4,] "pakistan"
98-
## [5,] "taliban"
99-
## [6,] "afghans"
98+
## [3,] "taliban"
99+
## [4,] "kabul"
100+
## [5,] "afghans"
101+
## [6,] "pakistan"
100102
```
101103

102104
### Arithmetic operations of word vectors
@@ -106,25 +108,25 @@ head(similarity(wdv, c("amazon", "forests", "obama", "america", "afghanistan"),
106108
``` r
107109
# What is Amazon without forests?
108110
head(similarity(wdv, analogy(~ amazon - forests)))
109-
## [,1]
110-
## [1,] "choo"
111-
## [2,] "smash-hit"
112-
## [3,] "yahoo"
113-
## [4,] "tripadvisor"
114-
## [5,] "univision"
115-
## [6,] "dreamworks"
111+
## [,1]
112+
## [1,] "yahoo"
113+
## [2,] "smash-hit"
114+
## [3,] "gawker"
115+
## [4,] "aggregators"
116+
## [5,] "troll"
117+
## [6,] "globe-spanning"
116118
```
117119

118120
``` r
119121
# What is for Afghanistan as Obama for America?
120122
head(similarity(wdv, analogy(~ obama - america + afghanistan)))
121123
## [,1]
122-
## [1,] "taliban"
123-
## [2,] "afghanistan"
124-
## [3,] "karzai"
125-
## [4,] "hagel"
126-
## [5,] "hamid"
127-
## [6,] "obama"
124+
## [1,] "afghanistan"
125+
## [2,] "karzai"
126+
## [3,] "afghan"
127+
## [4,] "taliban"
128+
## [5,] "obama"
129+
## [6,] "nato"
128130
```
129131

130132
These examples replicates analogical tasks in the original word2vec
@@ -133,23 +135,23 @@ paper.
133135
``` r
134136
# What is for France as Berlin for Germany?
135137
head(similarity(wdv, analogy(~ berlin - germany + france)))
136-
## [,1]
137-
## [1,] "paris"
138-
## [2,] "berlin"
139-
## [3,] "bourget"
140-
## [4,] "brussels"
141-
## [5,] "amsterdam"
138+
## [,1]
139+
## [1,] "paris"
140+
## [2,] "strasbourg"
141+
## [3,] "brussels"
142+
## [4,] "berlin"
143+
## [5,] "amsterdam"
142144
## [6,] "france"
143145
```
144146

145147
``` r
146148
# What is for slowly as quick for quickly?
147149
head(similarity(wdv, analogy(~ quick - quickly + slowly)))
148-
## [,1]
149-
## [1,] "uneven"
150-
## [2,] "gravity-defying"
151-
## [3,] "slow"
152-
## [4,] "super-charged"
153-
## [5,] "buck"
154-
## [6,] "sideways"
150+
## [,1]
151+
## [1,] "uneven"
152+
## [2,] "stumble"
153+
## [3,] "backwards"
154+
## [4,] "fades"
155+
## [5,] "slow"
156+
## [6,] "upside"
155157
```

tests/misc/compare_doc2vec.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rownames(dat3) <- dat3$doc_id
1212

1313
# doc2vec package -----------------------------
1414

15-
d2v <- paragraph2vec(dat3, dim = 50, threads = 1, type = "PV-DM", trace = FALSE)
15+
d2v <- paragraph2vec(dat3, dim = 50, threads = 8, type = "PV-DBOW", trace = FALSE)
1616
mat_d2v <- as.matrix(d2v, which = "docs", normalize = FALSE)
1717
hist(mat_d2v["4362315",, drop = TRUE])
1818

@@ -42,7 +42,7 @@ corp <- corpus(dat3)
4242
toks <- tokens(corp)
4343
dfmt <- dfm(toks, remove_padding = TRUE)
4444
options(wordvector_threads = 8)
45-
wdv <- textmodel_doc2vec(toks, dim = 50, type = "cbow", min_count = 5, verbose = TRUE, iter = 5,
45+
wdv <- textmodel_doc2vec(toks, dim = 50, type = "dbow2", min_count = 5, verbose = TRUE, iter = 5,
4646
tolower = FALSE, alpha = 0.05)
4747
mat_wdv <- as.matrix(wdv, layer = "documents", normalize = FALSE)
4848
hist(mat_wdv["4362315",, drop = TRUE])
@@ -78,5 +78,7 @@ mean(diag(sim_wdv_all)) - mean(sim_wdv_all)
7878
plot(rowSums(sim_wdv), rowSums(sim_d2v))
7979
cor(rowSums(sim_wdv), rowSums(sim_d2v))
8080

81-
81+
hist(as.matrix(wdv, layer = "words", normalize = FALSE)[1,])
82+
hist(wdv$values$word[,1])
83+
hist(wdv$weights[,1])
8284

tests/misc/probability.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
library(quanteda)
2+
library(wordvector)
3+
options(wordvector_threads = 8)
4+
5+
seed <- LSX::seedwords("sentiment")
6+
7+
corp <- data_corpus_news2014
8+
toks <- tokens(corp, remove_punct = TRUE, remove_symbols = TRUE) %>%
9+
tokens_remove(stopwords("en", "marimo"), padding = TRUE) %>%
10+
tokens_select("^[a-zA-Z-]+$", valuetype = "regex", case_insensitive = FALSE,
11+
padding = TRUE) %>%
12+
tokens_tolower()
13+
14+
wov <- textmodel_word2vec(toks, dim = 100, type = "sg", min_count = 5, verbose = TRUE, iter = 10)
15+
dov <- textmodel_doc2vec(toks, dim = 100, type = "dbow", min_count = 5, verbose = TRUE, iter = 10)
16+
17+
doc <- probability(dov, seed, layer = "documents")[,1]
18+
print(toks[head(doc)])
19+
print(toks[tail(doc)])
20+
21+
cor(probability(wov, seed, layer = "words", mode = "numeric")[,1],
22+
probability(dov, seed, layer = "words", mode = "numeric")[,1])
23+
24+
lss0 <- LSX::as.textmodel_lss(wov, seed, spatial = FALSE)
25+
lss <- LSX:::as.textmodel_lss(dov, seed, spatial = FALSE)
26+
pred0 <- predict(lss0, newdata = dfm(toks, remove_padding = TRUE), rescale = FALSE,
27+
min_n = 40)
28+
pred <- predict(lss, newdata = dfm(toks, remove_padding = TRUE), rescale = FALSE,
29+
min_n = 40)
30+
pred2 <- probability(dov, seed, layer = "documents", mode = "numeric")[,1] / length(seed)
31+
32+
dat <- data.frame(pred0, pred, pred2, pred3 = pred + pred2, text = corp[])
33+
34+
plot(dat[,1:4])
35+
cor(dat[,1:4])
36+
View(dat)
37+
head(dat)

0 commit comments

Comments
 (0)