该
tf.constant()运算需要花费numpy的阵列(或其它隐式转换为一个numpy的阵列),并返回一个
tf.Tensor其值是相同的数组。它并
没有 接受
tf.Tensor作为其参数。
另一方面,
tf.random_normal()op
tf.Tensor每次运行时都会根据给定的分布返回其值是随机生成的。由于它返回a
tf.Tensor,因此不能用作的参数
tf.constant()。这说明了
TypeError(与的使用无关
tf.InteractiveSession,因为它在构建图形时发生)。
我假设您希望您的图包括一个张量,该张量是(i)首次使用时随机生成的,而(ii)之后是常量。有两种方法可以做到这一点:
使用NumPy生成随机值,然后将其放入
tf.constant()
,就像您在问题中所做的那样:some_test = tf.constant(np.random.normal(loc=0.0, scale=1.0, size=(2, 2)).astype(np.float32))
(可能更快,因为它可以使用GPU生成随机数)使用TensorFlow生成随机值并将其放在
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



