当前位置:网站首页>How gensim freezes some word vectors for incremental training
How gensim freezes some word vectors for incremental training
2022-07-02 07:52:00 【MezereonXP】
Gensim It can be used for topic model extraction , Word vector generated python The library of .
It's like something NLP Preprocessing , You can use this library to generate easily and quickly .
Like Word2Vec, We can generate word vectors with a few lines of code , As shown below :
import gensim
from numpy import float32 as REAL
import numpy as np
word_list = ["I", "love", "you", "."]
model = gensim.models.Word2Vec(sentences=word_list, vector_size=200, window=10, min_count=1, workers=4)
# Print word vector
print(model.wv["I"])
# Save the model
model.save("w2v.out")
The author uses Gensim Generate word vectors , But there is a need , There is already a word vector model , Now we want to expand the original vocabulary , But I don't want to modify the word vector of existing words .
Gensim There is no document describing how to freeze word vectors , But we check its source code , It is found that there is an experimental variable that can help us .
# EXPERIMENTAL lockf feature; create minimal no-op lockf arrays (1 element of 1.0)
# advanced users should directly resize/adjust as desired after any vocab growth
self.wv.vectors_lockf = np.ones(1, dtype=REAL)
# 0.0 values suppress word-backprop-updates; 1.0 allows
This code can be found in gensim Of word2vec.py You can find
therefore , We can use this vectos_lockf To meet our needs , The corresponding code is directly given here
# Read the old word vector model
model = gensim.models.Word2Vec.load("w2v.out")
old_key = set(model.wv.index_to_key)
new_word_list = ["You", "are", "a", "good", "man", "."]
model.build_vocab(new_word_list, update=True)
# Get the length of the updated vocabulary
length = len(model.wv.index_to_key)
# Freeze all the previous words
model.wv.vectors_lockf = np.zeros(length, dtype=REAL)
for i, k in enumerate(model.wv.index_to_key):
if k not in old_key:
model.wv.vectors_lockf[i] = 1.
model.train(new_word_list, total_examples=model.corpus_count, epochs=model.epochs)
model.save("w2v-new.out")
In this way, the word vector is frozen , It will not affect some existing models ( We may train some models based on old word vectors ).
边栏推荐
- MMDetection安装问题
- 【Mixed Pooling】《Mixed Pooling for Convolutional Neural Networks》
- 【深度学习系列(八)】:Transoform原理及实战之原理篇
- (15) Flick custom source
- How to turn on night mode on laptop
- 论文写作tip2
- Gensim如何冻结某些词向量进行增量训练
- Implementation of yolov5 single image detection based on onnxruntime
- 常见的机器学习相关评价指标
- 【Wing Loss】《Wing Loss for Robust Facial Landmark Localisation with Convolutional Neural Networks》
猜你喜欢

Remplacer l'auto - attention par MLP

【Mixup】《Mixup:Beyond Empirical Risk Minimization》
![[Sparse to Dense] Sparse to Dense: Depth Prediction from Sparse Depth samples and a Single Image](/img/05/bf131a9e2716c9147a5473db4d0a5b.png)
[Sparse to Dense] Sparse to Dense: Depth Prediction from Sparse Depth samples and a Single Image

Win10+vs2017+denseflow compilation

Translation of the paper "written mathematical expression recognition with bidirectionally trained transformer"

深度学习分类优化实战

【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》

【多模态】CLIP模型

Hystrix dashboard cannot find hystrix Stream solution

【FastDepth】《FastDepth:Fast Monocular Depth Estimation on Embedded Systems》
随机推荐
Installation and use of image data crawling tool Image Downloader
将恶意软件嵌入到神经网络中
Regular expressions in MySQL
【Programming】
TimeCLR: A self-supervised contrastive learning framework for univariate time series representation
CPU register
图片数据爬取工具Image-Downloader的安装和使用
Remplacer l'auto - attention par MLP
open3d学习笔记五【RGBD融合】
【Programming】
Machine learning theory learning: perceptron
【Mixup】《Mixup:Beyond Empirical Risk Minimization》
ModuleNotFoundError: No module named ‘pytest‘
【学习笔记】反向误差传播之数值微分
Faster-ILOD、maskrcnn_benchmark训练coco数据集及问题汇总
open3d学习笔记二【文件读写】
The difference and understanding between generative model and discriminant model
【Cutout】《Improved Regularization of Convolutional Neural Networks with Cutout》
【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》
深度学习分类优化实战