2

I am having this code:

import tensorflow as tf
import numpy as np

data = np.random.randint(1000, size=10000)
x = tf.Variable(data, name='x')
y = tf.Variable(5*x*x-3*x+15, name='y')

model = tf.initialize_all_variables();

with tf.Session() as s:
    s.run(model)
    print (s.run(y))

I am trying to implement an exercise related to tensorflow variables but it fails with the following error:

Attempting to use uninitialized value x_20 [[Node: x_20/read = IdentityT=DT_INT64, _class=["loc:@x_20"], _device="/job:localhost/replica:0/task:0/cpu:0"]]

I also tried to initialize x with a constant but it still fails. What am I missing here ?

0

2 Answers 2

2

I think it's your definition of y that's a bit funny.

Your code currently makes a variable y and initializes it to 5*x*x-3*x+15

Maybe you just mean that the value of y is calculated from the value of x:

y=5*x*x-3*x+15

If you actually want to initialize a new variable y with the initial value of that expression over x, then you need to use x.initialized_value():

x = tf.Variable(data, name='x')
x0 = x.initialized_value()
y = tf.Variable(5*x0*x0-3*x0+15, name='y')

The traceback you're getting is coming from the fact that the initialize operation is trying to initialize y, before initializing x.

The .initialized_value() method enforces the order.

See: https://www.tensorflow.org/versions/r0.11/how_tos/variables/index.html#initialization-from-another-variable

Sign up to request clarification or add additional context in comments.

Comments

0

To solve this issue, I had to provide a constant value to the variable x. So I changed this line:

x = tf.Variable(data, name='x')

to the following line:

x = tf.constant(data, name='x')

It seems that a variable must be given a constant value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.