0

I have one question about random variables in TensorFlow. Let's suppose I need a random variable inside my loss function. In TensorFlow tutorials I find random functions used for initialize variables, like weights that in a second time are modified by training process. In my case I need a random vector of floats (let's say 128 values), that follows a particular distribution (uniform or Gaussian) but that can change in each loss calculation.

Defining this variable in my loss function, is this the simple thing that I need to do, since at each epoch I get new values (that anyway follow the selected distribution) or do I get that the values that are always the same in all the iterations?

E_net4
  • 27,810
  • 13
  • 101
  • 139
D.Giunchi
  • 1,900
  • 3
  • 19
  • 23
  • You might also want to check the docs on tf.set_random_seed() where they discuss how randomization is controlled at the Graph level and more granular levels: https://www.tensorflow.org/api_docs/python/tf/set_random_seed – VS_FF May 09 '17 at 16:38

2 Answers2

3

A random node in TensorFlow always takes a different value each time it is called, as you can verify by calling it several times

import tensorflow as tf
x = tf.random_uniform(shape=())
sess = tf.Session()
sess.run(x)
# 0.79877698
sess.run(x)
# 0.76016617

It is not a Variable in the tensorflow terminology, as you can check from the code above, which runs without calling variable initialization.

E_net4
  • 27,810
  • 13
  • 101
  • 139
P-Gn
  • 23,115
  • 9
  • 87
  • 104
1

If you assign the values randomly generated to a Variable then this value will remain fixed until you update this variable.

If you, instead, put in the loss function directly the "generation" (tf.random_*) of the numbers, then they'll be different at each call.

Just try this out:

import tensorflow as tf

# generator
x = tf.random_uniform((3,1), minval=0, maxval=10)

# variable
a = tf.get_variable("a", shape=(3,1), dtype=tf.float32)

# assignment
b = tf.assign(a, x)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(5):
        # 5 different values
        print(sess.run(x))

    # assign the value
    sess.run(b)
    for i in range(5):
        # 5 equal values
        print(sess.run(a))
E_net4
  • 27,810
  • 13
  • 101
  • 139
nessuno
  • 26,493
  • 5
  • 83
  • 74