Tensorflow is a popular framework for machine learning.
The latest version tensorflow 2 runs on python 3.
My default python version in OSX is 2.7.18
To avoid messing the system, I am going to install tensorflow in a python virtual environment.
* * * * *
# get the latest python 3
$ brew install python3 # or
$ brew upgrade python3
# mine installed to /usr/local/opt/python@3.9@ -> ../Cellar/python@3.9/3.9.2_1
# upgrade pip
/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip
(pls refer to official doc for installing virtualenv)
# upgrade virtualenv
/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade virtualenv
# if permission error, append '-user' option to the end
/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade virtualenv -user
# also upgrade the wrapper
/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade virtualenvwrapper
# create new virtual environment
mkvirtualenv -p python3 tf
# enter venv
workon tf
# upgrade pip
~/.virtualenvs/tf/bin/python -m pip install --upgrade pip
# install tensorflow
$ ~/.virtualenvs/tf/bin/python -m pip install tensorflow
# launch python; note version 3.x will be in greeting text
$ python
# check tensorflow installation successful
>>> import tensorflow as tf
>>> tf.__version__
'2.4.1'
# first tensorflow (v1) program
>>> import tensorflow.compat.v1 as tf
>>> tf.disable_v2_behavior()
>>> a = tf.constant(1, name='a')
>>> b = tf.constant(1, name='b')
>>> c = a + b
>>> with tf.compat.v1.Session() as sess:
... print(sess.run(c))
...
2021-03-07 12:05:15.104500: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-03-07 12:05:15.106227: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:196] None of the MLIR optimization passes are enabled (registered 0 passes)
2
>>>
a, b, and c are objects
>>> print(a)
Tensor("a:0", shape=(), dtype=int32)
when done,
ctrl+d to quit python
then deactivate venv
$ deactivate
Labels: macOS, osx, python, tensorflow