TensorFlow: generating a random constant

后端 未结 1 911
南旧
南旧 2021-01-05 08:07

In ipython I imported tensorflow as tf and numpy as np and created an TensorFlow InteractiveSession. When I am running or initializing

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 08:44

    The tf.constant() op takes a numpy array (or something implicitly convertible to a numpy array), and returns a tf.Tensor whose value is the same as that array. It does not accept a tf.Tensor as its argument.

    On the other hand, the tf.random_normal() op returns a tf.Tensor whose value is generated randomly according to the given distribution each time it runs. Since it returns a tf.Tensor, it cannot be used as the argument to tf.constant(). This explains the TypeError (which is unrelated to the use of tf.InteractiveSession, since it occurs when you build the graph).

    I'm assuming you want your graph to include a tensor that (i) is randomly generated on its first use, and (ii) constant thereafter. There are two ways to do this:

    1. Use NumPy to generate the random value and put it in a tf.constant(), as you did in your question:

      some_test = tf.constant(
          np.random.normal(loc=0.0, scale=1.0, size=(2, 2)).astype(np.float32))
      
    2. (Potentially faster, as it can use the GPU to generate the random numbers) Use TensorFlow to generate the random value and put it in a tf.Variable:

      some_test = tf.Variable(
          tf.random_normal([2, 2], mean=0.0, stddev=1.0, dtype=tf.float32)
      sess.run(some_test.initializer)  # Must run this before using `some_test`
      

    0 讨论(0)
提交回复
热议问题