Getting below error in the code built using tensorflow:

I'm trying to initialize a value to the variable '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()

Getting below errors while executing above code:

---------------------------------------------------------------------------
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 answer

  • answered 2018-07-12 12:38 Mohan Radhakrishnan

    This should work. The constant should be evaluated.

    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) )