word2vec
Implementation of the Incremental SkipGram and CBOW algorithms.
IWord2Vec
Bases: IWVBase
Word2Vec incremental architectures is an adaptation of the popular word2vec proposed by Mikolov et al. to the streaming scenario. To adapt these algorithms to a streaming setting, we rely on the Incremental SkipGram with Negative Sampling model proposed by Kaji et al. The main assumptions we consider are:
- The models must deal with the fact that the vocabulary is dynamic and unknown, so the structures are updated as it is trained.
- The unigram table is created incrementally using the algorithm proposed by Kaji et al.
- The internal structure of the architecture was programmed in Pytorch.
In this package, both CBOW and SG models were adapted using the incremental negative sampling technique to accelerate their training speed.
References
- Kaji, N., & Kobayashi, H. (2017). Incremental skip-gram model with negative sampling. arXiv preprint arXiv:1704.03956.
- Montiel, J., Halford, M., Mastelini, S. M., Bolmier, G., Sourty, R., Vaysse, R., ... & Bifet, A. (2021). River: machine learning for streaming data in Python.
Examples:
>>> from torch.utils.data import DataLoader
>>> from rivertext.models.iw2v import IWord2Vec
>>> from rivertext.utils import TweetStream
>>> ts = TweetStream("/path/to/tweets.txt")
>>> dataloader = DataLoader(ts, batch_size=32)
>>> iw2v = IWord2Vec(
... window_size=3,
... vocab_size=3
... emb_size=3,
... sg=0,
... neg_samples_sum=1,
... device="cuda:0"
... )
>>> for batch in dataloader:
... iw2v.learn_many(batch)
>>> iw2v.vocab2dict()
{'hello': [0.77816248, 0.99913448, 0.14790398],
'are': [0.86127345, 0.24901696, 0.28613529],
'you': [0.64463917, 0.9003653 , 0.26000987],
'this': [0.97007572, 0.08310498, 0.61532574],
'example': [0.74144294, 0.77877194, 0.67438642]
}
>>> iw2v.transform_one('hello')
[0.77816248, 0.99913448, 0.14790398]
Source code in rivertext/models/iw2v.py
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 |
|
__init__(vocab_size=1000000, emb_size=100, unigram_table_size=100000000, window_size=5, alpha=0.75, subsampling_threshold=0.001, neg_samples_sum=10, sg=1, lr=0.025, device=None, optimizer=SparseAdam, on=None, strip_accents=True, lowercase=True, preprocessor=None, tokenizer=None, ngram_range=(1, 1))
An instance of IWord2Vec class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vocab_size |
int
|
Vocab size, by default 1_000_000. |
1000000
|
emb_size |
int
|
Embdding size, by default 100. |
100
|
unigram_table_size |
int
|
Unigram table size, by default 100_000_000. |
100000000
|
window_size |
int
|
Window size, by default 5 |
5
|
alpha |
float
|
Smoother parameter, by default 0.75 |
0.75
|
subsampling_threshold |
Subsampling parameter, by default 1e-3 |
0.001
|
|
neg_samples_sum |
int
|
Number of negative sampling to used, by default 10. |
10
|
sg |
int
|
training algorithm, 1 for CBOW; otherwise SG. |
1
|
lr |
float
|
Learning rate of the optimizer, by default 0.025 |
0.025
|
device |
str
|
Device to run the wrapped model on. Can be "cpu" or "cuda", by default cuda. |
None
|
optimizer |
Optimizer
|
Optimizer to be used for training the model., by default SparseAdam. |
SparseAdam
|
on |
str
|
The name of the feature that contains the text to vectorize. If |
None
|
strip_accents |
bool
|
Whether or not to strip accent characters, by default True. lowercase: Whether or not to convert all characters to lowercase by default True. |
True
|
preprocessor |
An optional preprocessing function which overrides the
|
None
|
|
tokenizer |
Callable[[str], List[str]]
|
A function used to convert preprocessed text into a |
None
|
ngram_range |
Tuple[int, int]
|
The lower and upper boundary of the range n-grams to be
extracted. All values of n such that |
(1, 1)
|
Source code in rivertext/models/iw2v.py
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 |
|
learn_many(X, y=None, **kwargs)
Train a mini-batch of text features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
List[str]
|
A list of sentence features. |
required |
y |
A series of target values, by default None. |
None
|
Examples:
>>> from torch.utils.data import DataLoader
>>> from rivertext.models.iw2v import IWord2Vec
>>> from rivertext.utils import TweetStream
>>> ts = TweetStream("/path/to//tweets.txt")
>>> dataloader = DataLoader(ts, batch_size=32)
>>> iw2v = IWord2Vec(
... window_size=3,
... vocab_size=3
... emb_size=3,
... sg=0,
... neg_samples_sum=1,
... device="cuda:0"
... )
>>> for batch in dataloader:
... iw2v.learn_many(batch)
>>> iw2v.vocab2dict()
{'hello': [0.77816248, 0.99913448, 0.14790398],
'are': [0.86127345, 0.24901696, 0.28613529],
'you': [0.64463917, 0.9003653 , 0.26000987],
'this': [0.97007572, 0.08310498, 0.61532574],
'example': [0.74144294, 0.77877194, 0.67438642]
}
>>> wcm.transform_one('hello')
[0.77816248, 0.99913448, 0.14790398]
Source code in rivertext/models/iw2v.py
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 |
|
learn_one(x, **kwargs)
Train one instance of text feature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
str
|
one line of text. |
required |
Examples:
>>> from torch.utils.data import DataLoader
>>> from rivertext.models.iw2v import IWord2Vec
>>> from rivertext.utils import TweetStream
>>> ts = TweetStream("/path/to/tweets.txt")
>>> dataloader = DataLoader(ts)
>>> iw2v = IWord2Vec(
... window_size=3,
... vocab_size=3
... emb_size=3,
... sg=0,
... neg_samples_sum=1,
... device="cuda:0"
... )
>>> for tweet in dataloader:
... iw2v.learn_one(tweet)
>>> iw2v.vocab2dict()
{'hello': [0.77816248, 0.99913448, 0.14790398],
'are': [0.86127345, 0.24901696, 0.28613529],
'you': [0.64463917, 0.9003653 , 0.26000987],
'this': [0.97007572, 0.08310498, 0.61532574],
'example': [0.74144294, 0.77877194, 0.67438642]
}
>>> iw2v.transform_one('hello')
[0.77816248, 0.99913448, 0.14790398]
Source code in rivertext/models/iw2v.py
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 |
|
transform_one(x)
Obtain the vector embedding of a word.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
str
|
word to obtain the embedding. |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
The vector embedding of the word. |
Source code in rivertext/models/iw2v.py
174 175 176 177 178 179 180 181 182 183 184 |
|
vocab2dict()
Converts the vocabulary in a dictionary of embeddings.
Returns:
Type | Description |
---|---|
Dict[str, np.ndarray]
|
An dict where the words are the keys, and their values are the embedding vectors. |
Source code in rivertext/models/iw2v.py
162 163 164 165 166 167 168 169 170 171 172 |
|