1

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

Vibhutha Kumarage
  • 1,372
  • 13
  • 26

2 Answers2

3

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.

nnnmmm
  • 7,964
  • 4
  • 22
  • 41
  • This is how random seeds work in general... https://stackoverflow.com/questions/22639587/random-seed-what-does-it-do – Him Jan 18 '18 at 17:00
1

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  ]]
binjip
  • 534
  • 3
  • 7
  • I'm building a network but I don't know if it's performing well because every time it trains I get different results. How can I be certain? I'm defining my weights as `'weights': tf.Variable(tf.truncated_normal([22, n_nodes_hl1], seed=1))` – jinchuika Jun 26 '18 at 12:58