2

In previous code, I tried the below and it worked but it's not working now. Can anyone see the problem?

with tf.Session() as session():
    session.run(init)

But now I am getting this error. From looking at other posts, it seems to be because other people are using brackets when they should be using parenthesis. But I'm not using brackets and I can't see what is wrong.

    with tf.Session() as session():
        ^
SyntaxError: can't assign to function call
Dan
  • 253
  • 2
  • 3
  • 10

3 Answers3

2
with tf.Session() as session():

is somewhat same as:

session() = tf.Session()

which produces the same error because parentheses () after session makes Python think left-hand side is an expression which is syntactically and semantically wrong, thus you should remove them:

with tf.Session() as session:
    ...
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
0

In previous code, I tried the below and it worked but it's not working now.

Well that's simply impossible because

with tf.Session() as session():

is a syntax error in every Python. What you want is

with tf.Session() as session:

without those brackets () at the end.

freakish
  • 54,167
  • 9
  • 132
  • 169
0
import tensorflow as tf
with tf.compat.v1.Session() as sess:

    a = tf.constant(3.0)
    b = tf.constant(4.0)
    c = a+b

    print(sess.run(c))

    sess.close()

7.0

anaconda
  • 45
  • 2