2

I am writing a rather complex Tensorflow model/program and it seems to be working for my purposes as it is supposed to. However, now I am in the process of changing my model to accept dynamically shaped data, and here is where I am running into problems. Basically, my problems boils down to this code (which is not running on TF 1.3):

def foo():
    # !!! Here is where we get the error !!!
    myVar = myVar_t.eval()

    # Now myVar should be an int
    # In this example it should be 38
    return myVar

# Placeholder Scalar Tensor
myVar_t = tf.placeholder(tf.int32)

# Something to call foo()
result = foo()

# Initialize global variables
init = tf.global_variables_initializer()

# Get the session
sess = tf.Session()

# Set it as default so eval() (in theory) will work
with sess.as_default():

    # Run the global variables initializer
    sess.run(init)

    # Following should feed 38 into placeholder and then get result
    out = sess.run([result], feed_dict={myVar_t: 38})

    # Should print 38
    print(out)

Here, as you may have guessed, I get the ValueError("Cannot evaluate tensor using 'eval()': No default session is registered. I found this related question, and while the answer to that question does produce valid code, I am unable to adapt it to 'fit' my code. I think the error has something to do with the fact that I am calling sess.run() twice, but in my real code I need to do this.

I have tried rearranging things like putting the placeholder initialization within the with block, but then the program complains with InvalidArgumentError: You must feed a value for placeholder tensor to which I found this pertinent question, but again I could not figure out what was wrong with this.

I have also tried explicitly giving the session when calling eval(), as in I do eval(session=sess) but this does not seem to help.

I'm sure it's probably just a small mistake somewhere, but this one small thing has kept me occupied for more time than I care to admit. Any help would be appreciated, thank you.

Alerra
  • 1,479
  • 11
  • 25

0 Answers0