Word2Vec
Word2VecAlign
Wrapper class for gensim.models.Word2Vec to align Word2Vec models.
Methods
__init__(model_paths)
Initialize the Word2VecAlign object with a list of paths to the Word2Vec models.
load_models()
Load the models
align_models(reference_index, output_dir, method)
Align the models
Source code in semantics/feature_extraction/word2vec.py
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 |
|
__init__(model_paths)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_paths |
List[str]
|
List of paths to the models |
required |
Attributes:
Name | Type | Description |
---|---|---|
model_paths |
List[str]
|
List of paths to the models |
reference_model |
Word2Vec
|
The reference model |
models |
List[Word2Vec]
|
List of models |
model_names |
List[str]
|
List of model names |
aligned_models |
List[Word2Vec]
|
List of aligned models |
Source code in semantics/feature_extraction/word2vec.py
align_models(reference_index=-1, output_dir=None, method='procrustes')
Align the models
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference_index |
int
|
Index of the reference model, by default -1 |
-1
|
output_dir |
str
|
Path to save the aligned models, by default None |
None
|
method |
str
|
Alignment method, by default "procrustes" |
'procrustes'
|
Returns:
Name | Type | Description |
---|---|---|
aligned_models |
List[Word2Vec]
|
List of aligned models |
Examples:
>>> from semantics.feature_extraction.word2vec import Word2VecAlign
>>> model_paths = ['model1.model', 'model2.model']
>>> Word2VecAlign(model_paths).align_models(reference_index=0, output_dir='aligned_models')
>>> print('Aligned models: ', Word2VecAlign(model_paths).aligned_models)
Aligned models: [Word2Vec(vocab=5, vector_size=100, alpha=0.025), Word2Vec(vocab=5, vector_size=100, alpha=0.025)]
Source code in semantics/feature_extraction/word2vec.py
Word2VecEmbeddings
Wrapper class for gensim.models.Word2Vec to infer word vectors.
Methods
__init__(pretrained_model_path)
Initialize the Word2VecEmbeddings object with a pretrained model.
_word2vec_case_preparation()
Prepare the Word2Vec model
infer_vector(word, norm)
Infer the vector of a word
Source code in semantics/feature_extraction/word2vec.py
__init__(pretrained_model_path=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretrained_model_path |
str
|
Path to a pretrained model, by default None |
None
|
Attributes:
Name | Type | Description |
---|---|---|
model_path |
str
|
Path to a pretrained model, by default None |
model |
Word2Vec
|
The Word2Vec model |
vocab |
bool
|
Whether the model has been initialized |
Source code in semantics/feature_extraction/word2vec.py
infer_vector(word, norm=False)
Infer the vector of a word
Parameters:
Name | Type | Description | Default |
---|---|---|---|
word |
str
|
The word to infer the embedding vector of |
required |
norm |
bool
|
Whether to normalize the vector, by default False |
False
|
Returns:
Name | Type | Description |
---|---|---|
embedding |
List[float]
|
The embedding vector of the word |
Source code in semantics/feature_extraction/word2vec.py
Word2VecInference
Wrapper class for gensim.models.Word2Vec for Inference.
Methods
__init__(pretrained_model_path)
Initialize the Word2VecInference object with a pretrained model.
get_embedding(word, norm)
Infer the vector of a word
get_similarity(word1, word2)
Get the cosine similarity between two words
get_top_k_words(word, k)
Get the top k most similar words to a word in the vocabulary of the model.
Source code in semantics/feature_extraction/word2vec.py
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 |
|
__init__(pretrained_model_path=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretrained_model_path |
str
|
Path to a pretrained model, by default None |
None
|
Attributes:
Name | Type | Description |
---|---|---|
word_vectorizor |
Word2VecEmbeddings
|
The Word2VecEmbeddings object |
Source code in semantics/feature_extraction/word2vec.py
get_embedding(word, norm=False)
Infer the vector of a word
Parameters:
Name | Type | Description | Default |
---|---|---|---|
word |
str
|
The word to infer the embedding vector of |
required |
norm |
bool
|
Whether to normalize the vector, by default False |
False
|
Returns:
Name | Type | Description |
---|---|---|
embedding |
List[float]
|
The embedding vector of the word |
Examples:
>>> from semantics.feature_extraction.word2vec import Word2VecInference
>>> Word2VecInference('model.model').get_embedding('test', norm=False)
array([-0.00460768, -0.00460768, ..., -0.00460768, -0.00460768])
Source code in semantics/feature_extraction/word2vec.py
get_similarity(word1, word2)
Get the cosine similarity between two words' embedding vectors
Parameters:
Name | Type | Description | Default |
---|---|---|---|
word1 |
str
|
The first word |
required |
word2 |
str
|
The second word |
required |
Returns:
Name | Type | Description |
---|---|---|
similarity |
float
|
The cosine similarity between the two words |
Examples:
>>> from semantics.feature_extraction.word2vec import Word2VecInference
>>> Word2VecInference('model.model').get_similarity('test', 'another')
0.99999994
Source code in semantics/feature_extraction/word2vec.py
get_top_k_words(word, k=10)
Get the top k most similar words to a word in the vocabulary of the model. Default k = 10
Parameters:
Name | Type | Description | Default |
---|---|---|---|
word |
str
|
The word to get the top k most similar words of |
required |
k |
int
|
The number of words to return, by default 10 |
10
|
Returns:
Name | Type | Description |
---|---|---|
topk |
Tuple[List[str], List[float]]
|
Tuple of lists of the top k most similar words and their cosine similarities |
Examples:
>>> from semantics.feature_extraction.word2vec import Word2VecInference
>>> Word2VecInference('model.model').get_top_k_words('test', k=1)
(['another'], [0.9999999403953552])
Source code in semantics/feature_extraction/word2vec.py
Word2VecTrainer
Wrapper class for gensim.models.Word2Vec to train a Word2Vec model.
Methods
__init__(model_path, min_count, window, negative, ns_exponent, vector_size, workers, sg, **kwargs)
Initialize the Word2Vec model
train(data, output_path, epochs, start_alpha, end_alpha, compute_loss, **kwargs)
Train the Word2Vec model on the given data
Source code in semantics/feature_extraction/word2vec.py
__init__(model_path=None, min_count=0, window=15, negative=5, ns_exponent=0.75, vector_size=100, workers=1, sg=1, **kwargs)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_path |
str
|
Path to a pretrained model, by default None. |
None
|
min_count |
int
|
Ignores all words with total frequency lower than this, by default 0 |
0
|
window |
int
|
The maximum distance between the current and predicted word within a sentence, by default 15 |
15
|
negative |
int
|
If > 0, negative sampling will be used, by default 5 |
5
|
ns_exponent |
float
|
The exponent used to shape the negative sampling distribution, by default 0.75 |
0.75
|
vector_size |
int
|
Dimensionality of the word vectors, by default 100 |
100
|
workers |
int
|
Number of worker threads to train the model, by default 1 |
1
|
sg |
int
|
Training algorithm: 1 for skip-gram; otherwise CBOW, by default 1 |
1
|
**kwargs |
optional
|
Additional arguments to pass to the gensim.models.Word2Vec constructor |
{}
|
Attributes:
Name | Type | Description |
---|---|---|
model |
Word2Vec
|
The Word2Vec model |
Source code in semantics/feature_extraction/word2vec.py
train(data, output_dir=None, epochs=5, start_alpha=0.025, end_alpha=0.0001, compute_loss=True, **kwargs)
Train the Word2Vec model on the given data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
List[str]
|
List of documents |
required |
output_dir |
(Union[str, Path], None)
|
Path to save the trained model, by default None |
None
|
epochs |
int
|
Number of epochs, by default 5 |
5
|
start_alpha |
float
|
Learning rate, by default 0.025 |
0.025
|
end_alpha |
float
|
Minimum learning rate, by default 0.0001 |
0.0001
|
compute_loss |
bool
|
Whether to compute the loss, by default True |
True
|
**kwargs |
optional |
{}
|
Examples:
>>> from semantics.feature_extraction.word2vec import Word2VecTrainer
>>> texts = ['This is a test.', 'This is another test.', 'This is a third test.']
>>> Word2VecTrainer().train(texts, epochs=1)
>>> print('Trained model: ', Word2VecTrainer().model)
Trained model: Word2Vec(vocab=5, vector_size=100, alpha=0.025)