Vibhutha Kumarage
Vibhutha Kumarage

Reputation: 1399

Why tensorflow random_normal function gives different outputs with a fixed seed value

I set fixed a seed value and run the session using following line of codes.

with tf.Session() as sess:
    matrix = tf.random_normal([2,2], mean=1, stddev=1, seed=1)
    print(matrix.eval())
    print(matrix.eval())
    print(matrix.eval())
    print(sess.run(matrix))
    print(sess.run(matrix))
    print(sess.run(matrix))
    print(sess.run(matrix))

But the code gave me the output like this,

[[1.2818339 3.3878284]
 [3.2804048 0.691338 ]]
[[0.84588486 1.3642604 ]
 [1.6235023  0.96976113]]
[[2.3133767 2.4734092]
 [1.6430309 0.7378005]]
[[ 1.8944461   0.39336425]
 [ 0.1861412  -0.01721728]]
[[-0.6642921   2.3849297 ]
 [-0.06870818 -0.1625154 ]]
[[ 1.0668459   0.45170426]
 [ 2.4276698  -0.24925494]]
[[1.8668368  1.3444978 ]
 [1.5144594  0.31290668]]

I expect to print exact values because i fixed the seed value. Why does it print out a different values? Thanks

Upvotes: 1

Views: 598

Answers (2)

binjip
binjip

Reputation: 544

Seed is used only for initializing the matrix. Every time you run the session the matrix will be initialized with the same values.

However, every time you call matrix.eval() it will give you the next random value in the chain of random values.

If you wanted always the same number you would have to do the following although I doubt its usefulness.

with tf.Session() as sess:
   matrix = tf.random_normal([2,2], mean=1, stddev=1, seed=1)
   print(matrix.eval())
   matrix = tf.random_normal([2,2], mean=1, stddev=1, seed=1)
   print(matrix.eval())

Returns:

[[ 1.28183389  3.38782835]
[ 3.28040481  0.691338  ]]
[[ 1.28183389  3.38782835]
[ 3.28040481  0.691338  ]]

Upvotes: 1

nnnmmm
nnnmmm

Reputation: 8744

In each run, your computational graph is evaluated anew, thereby generating new random numbers, but not resetting the seed.

I think running the entire Python file again should give the same output as before.

Upvotes: 3

Related Questions