如何在python3环境使Word2Vec每次运行结果一致
我们经常使用gensim的Word2Vec来进行单词转向量,但是这个包存在一个问题,每次训练结果都不同,这就导致了特征无法复现。
查阅Word2Vec的官方文档,在seed参数哪里可以发现这样的解释:
- seed (int) – Seed for the random number generator. Initial vectors for each word are seeded with a hash of the concatenation of word + str(seed). Note that for a fully deterministically-reproducible run, you must also limit the model to a single worker thread (workers=1), to eliminate ordering jitter from OS thread scheduling. (In Python 3, reproducibility between interpreter launches also requires use of the PYTHONHASHSEED environment variable to control hash randomization)
在python2中,只要设置seed(随机种子)参数,以及worker=1即可保证每次训练一致,但python3还需要设置PYTHONHASHSEED 参数,
经验证使用os.environ['PYTHONHASHSEED '] =‘**'无法起作用,必须修改系统环境变量。
- windows: 右击计算机(我的电脑)-》选择属性-》在弹出的面板中选择高级系统设置-》选择环境变量-添加PYTHONHASHSEED=0
- linux: vim /etc/profile 或者 .bash_profile 然后添加PYTHONHA