After installing Python 3.7.x on macOS and pip3 install tensorflow
, 2.1 series came in. I was in trouble because the code I found on the net didn't work.
If you set up Python 3.6.x and Tensorflow 1.x, it seems that you can run it with the original source, but I would like to use Tensorflow 2.x because it is a big deal.
Then save the common code with a name like tf-hello.py
and try running it.
import tensorflow as tf
hello = tf.constant('Hello, Hello World!')
sess = tf.Session()
print(sess.run(hello))
You should see something like this:
$ python3 tf-hello.py
...
Traceback (most recent call last):
File "tf-hello.py", line 6, in <module>
sess = tf.Session()
AttributeError: module 'tensorflow' has no attribute 'Session'
It seems that in Tensorflow 2.x, tf.Session and tf.placeholder are no longer used (see Migrate your TensorFlow 1 code to TensorFlow 2).
There are ways to modify the code as follows:
However, it seems that tf.print
can be used for output.
import tensorflow as tf
hello = tf.constant('Hello, Hello World!')
#sess = tf.Session()
#print(sess.run(hello))
tf.print(hello)
There is a way to modify the import line as follows: This method also works, but it will not work anymore, so the method of countermeasure 1 seems to be better.
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
hello = tf.constant('Hello, Hello World!')
sess = tf.Session()
print(sess.run(hello))
Numerical values can be output in the same way.
$ cat calc.py
import tensorflow as tf
a = tf.constant(1234)
b = tf.constant(5000)
total = a + b
tf.print(total)
$ python3.7 calc.py
...
2020-02-13 00:34:45.897058: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7f9b08d70740 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-02-13 00:34:45.897083: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
6234
that's all.
Recommended Posts