python random seed

Posted by neverset on March 31, 2020

numpy

np.random.seed() & np.random.RandomState()

  • np.random.seed()
    this is to set a global seed for np.random;
    it can be used for mist dataset iterator, mist.train.nextbatch(), since this nextbatch API is based on numpy

    np.random.uniform(min, max, size_of_array) #random float point number in range [min, max] np.random.rand(row, coloumn) # random float point number in range [0.0, 1,0)
    np.random.random([dim1, dim2…]) #random float in range [0.0, 1.0) numpy.random.normal(mean, stddev, size) #random float number with mean and stddev with shape of size

  • np.random.RanddomState() this is only a locally declared instance of randomstate, the global numpy state and other randomstates will not change

    instance=np.random.RandomState(1) instance.uniform(min, max, size_of_array) instance.rand(row, coloumn)

tensorflow

tf.set_random_seed()

this will create a random seed on graph level, that means that all operations (tf.random_uniform() or tf.random_normal()) in different sessions are reproducive

tf.random_uniform() or tf.random_normal()

random seed can also applied in this generator function

a=tf.random_uniform([1], seed=1)