jeudi 18 janvier 2018

Weird results when use numpy.random.seeds(0)?

I found something weird when using numpy.random.seed(0) to get reproducible results.

import numpy as np
def a():
    print('a', np.random.rand())
    b()
def b():
    print('b', np.random.rand())
    c()
def c():
    print('c', np.random.rand())
np.random.seed(0)
a()

The result is:

a 0.5488135039273248
b 0.7151893663724195
c 0.6027633760716439

And if I change the code to:

import numpy as np
def a():
    np.random.seed(0)
    print('a', np.random.rand())
    b()
    print('aa', np.random.rand())
def b():
    np.random.seed(0)
    print('b', np.random.rand())
    c()
def c():
    np.random.seed(0)
    print('c', np.random.rand())

a()

The results would be as follow:

a 0.5488135039273248
b 0.5488135039273248
c 0.5488135039273248
aa 0.7151893663724195

It's really weird, I was using tensorflow to train my finance sequence predict model, and I want to get reproducible results. It seems that I need to call np.random.seeds() before every time I use random function. Anyone has better solution?




Aucun commentaire:

Enregistrer un commentaire