smallKing
smallKing

Reputation: 31

Tensor.graph is meaningless when eager execution is enabled

a = tf.compat.v1.constant(5.0)
b = tf.compat.v1.constant(6.0)

sum1 = a + b
g = tf.compat.v1.Graph()
with g.as_default():
    # Define operations and tensors in `g`.
    hello = tf.compat.v1.constant('hello')
    assert hello.graph is g

sess = tf.compat.v1.Session(graph=g)

print(sess.run(sum1))


tensorflow-gpu2.0 i don't know why. i am a beginner of tensorflow

Upvotes: 3

Views: 8360

Answers (3)

Benyamin
Benyamin

Reputation: 87

I didn't know what you want to do! But I made some guesses and this is what I came up with as a result.

import tensorflow as tf

g = tf.compat.v1.Graph()
with g.as_default():
    a = tf.compat.v1.constant(5.0)
    b = tf.compat.v1.constant(6.0)
    sum1 = tf.add(a, b) # instead of sum1 = a + b
    
    hello = tf.compat.v1.constant('hello')
    assert hello.graph is g

sess = tf.compat.v1.Session(graph=g)

print(sess.run(sum1))

I believe it works with Tensorflow 2.x, If you still get eager exception then you should just add:

tf.compat.v1.disable_eager_execution()

Before

assert hello.graph is g

Upvotes: 0

Stas Oknedis
Stas Oknedis

Reputation: 344

After import tensorflow need disable eager execution, like below:

import tensorflow as tf
tf.compat.v1.disable_eager_execution()

It's worked for me.

Upvotes: 6

user13171160
user13171160

Reputation: 1

I tried tensorflow version 1.14.0

Do the following

pip install tensorflow==1.14..0

Try the following code

import tensorflow as tf
a = tf.compat.v1.constant(5.0)
b = tf.compat.v1.constant(6.0)

sum1 = a + b
g = tf.compat.v1.Graph()
with g.as_default():
# Define operations and tensors in `g`.
    hello = tf.compat.v1.constant('hello')
    assert hello.graph is g
sess = tf.compat.v1.Session()
#sess = tf.compat.v1.Session(graph=g)
print(sess.run(sum1))

Upvotes: -1

Related Questions