Search code examples
pythonword2vec

How to set PYTHONHASHSEED in python file


I'm trying to set PYTHONHASHSEED=0 in my python file and my python version is 3.6. I'm using Word2Vec model "(Word2Vec(description, min_count=1, size= 100, workers=3, window =3, sg = 1, seed=0))", i am not getting the consistent result.

Is there any way I can set the PYTHONHASHSEED for my python filename.py?


Solution

  • You need to set the environment variable PYTHONHASHSEED to 0 before Python is even running. If you start Python from the terminal you can do something like:

    export PYTHONHASHSEED=0
    python ...
    

    We can test it works by looking at the hash of a string and how it changes between runs of Python:

    $ python -c 'print(hash("hi"))'
    -6850579690611595074
    $ python -c 'print(hash("hi"))'
    -5185907786673828222
    
    $ export PYTHONHASHSEED=0
    
    $ python -c 'print(hash("hi"))'
    -8951030814243160003
    $ python -c 'print(hash("hi"))'
    -8951030814243160003
    

    Note that this is likely not the only cause of possible indeterminism in a deep learning model. For example, for PyTorch, see Reproducibility.