Run Tensorflow 2.x on Python 3.7

Preface

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.

How to use Hello Tensorflow with this configuration?

Try Hello Tensorflow without thinking about anything

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).

Measure 1

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) 

Measure 2

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)) 

By the way

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.

reference

Recommended Posts

Run Tensorflow 2.x on Python 3.7
Run Zookeeper x python (kazoo) on Mac OS X
TensorFlow: Run data learned in Python on Android
Run Openpose on Python (Windows)
Put Python 3.x on Ubuntu
Run Python CGI on CORESERVER
Run unix command on python
Introducing TensorFlow on Ubuntu + Python 2.7
Run Tensorflow natively supported on windows
Run Python on Schedule on AWS Lambda
Run TensorFlow2 on a VPS server
Periodically run Python on Heroku Scheduler
Run servo with Python on ESP32 (Windows)
Run TensorFlow on a GPU instance on AWS
Python on Windows
twitter on python3
[Python] Run Flask on Google App Engine
Run AzureKinect in Python on Christmas Eve.
How to erase Python 2.x on Mac.
Use without installing python 2.x on Windows
python on mac
Run servomotor on Raspberry Pi 3 using python
[Python] Run Headless Chrome on AWS Lambda
Run Python code on A2019 Community Edition
Python on Windbg
Installing PIL with Python 3.x on macOS
Run Python in C ++ on Visual Studio 2017
Run NASA CEA on Mac OS X
Run python wsgi server on NGINX Unit
Periodically run a python program on AWS Lambda
Install and run Python3.5 + NumPy + SciPy on Windows 10
Put MicroPython on Windows to run ESP32 on Python
Run Python YOLOv3 in C ++ on Visual Studio 2017
How to run MeCab on Ubuntu 18.04 LTS Python
Run Python web apps on NGINX + NGINX Unit + Flask
Run pip install on MacOS Python 3.7 or later
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Installing TensorFlow 0.11.0rc2 on OS X El Capitan (10.11.6)
Installing TensorFlow on Windows Easy for Python beginners
Shpinx (Python documentation builder) on Mac OS X
Run Python on Apache to view InfluxDB data
Install Tensorflow on Mac
Python conda on cygwin
Install TensorFlow on Ubuntu
Install python on WSL
Run Python with VBA
PyOpenGL setup on Python 3
Install Python on Pidora.
Run prepDE.py with python3
Install Scrapy on python3
Run Django on PythonAnywhere
Install Python on Mac
Install Python 3 on Mac
Run python from excel
Install Python3.4 on CentOS 6.6
Run mysqlclient on Lambda
Run Blender with python
Installing pandas on python2.6
python basic on windows ②
Install python on windows