在使用tensorflow构建的代码中获得以下错误:

时间:2018-07-12 07:13:10

标签: python tensorflow

我正在尝试将变量'theta'的值初始化:

n=tf.constant(8)
theta = tf.Variable(tf.random_uniform([n + 1, 1],-1.0, 1.0))
with tf.Session() as sess:
    theta.initializer.run()

执行上述代码时遇到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-34-6b7fabbea243> in <module>()
      1 n=tf.constant(8)
----> 2 theta = tf.Variable(tf.random_uniform([n + 1, 1],-1.0, 1.0))
      3 with tf.Session() as sess:
      4     theta.initializer.run()

1 个答案:

答案 0 :(得分:0)

这应该有效。该常数应该被求值。

n = tf.constant(8) 

with tf.Session() as sess:

    init_n = sess.run(n) # Evaluate

    theta = tf.Variable(tf.random_uniform([init_n, 1], -1.0, 1.0))
    theta.initializer.run()
    print( sess.run(theta) )