106. Installing and using TensorFlow in Anaconda

TensorFlow is a very powerful Open Source Deep Learning environment.

To use TensorFlow is just a little more complicated than other Python libraries, as it may conflict with other versions of libraries present. In order to avoid potential conflicts we set up a ‘virtual environment’. That environment will hold a version of Python that is compatible with TensorFlow itself, and other packages like NumPy, Pandas, and MatPlotLib.

First open up a terminal or command line environment. And install the TensorFlow environment with the following line.

Installing TensorFlow

conda create -n tensorflow_env tensorflow

The Anaconda TensorFlow environment does not access Python/Anaconda packages you have installed elsewhere. Though it installs NumPy, three other common packages need separate installation.

We are going to ‘activate’ the TensorFlow environment, install some packages, and then deactivate the environment.

conda activate tensorflow_env

You should now see (tensorflow_env) at the start of the command line. Now install these commonly used packages, with:

conda install -n tensorflow_env scikit-learn pandas matplotlib

Then deactivate the environment with:

conda deactivate

Using tensorflow

If you are comfortable using the terminal then you may use TensorFlow directly from there by using conda activate tensorflow_env to activate the environment and deactivate to end the session.

But you may also use Anaconda Navigator. Open up Anaconda Navigator. Click on ‘environments’, and select ‘tensorflow_env’ (here is where you can switch back to your ‘normal’ environment by selecting ‘base (root)’.

Now click back on ‘Home’. If you use Visual Studio code for editing and running code you can launch directly here. If you use Spyder you will see that you need to install Spyder into this environment first.

That’s it! Launch your code editor of choice, and you should now be able to import TensorFlow as with any other package. The normal convention is:

import tensorflow as tf

1. Introduction, and installing python for healthcare modelling


This site is a collection of code snippets that help me use Python for health services research, modelling and analysis. When learning something new I always work on a small code example to understand how something works, and to keep as a handy reference.

I will be looking at data handling, some statistics, data plotting, discrete event simulation, and machine learning.

I will be including use of pure Python as well as commonly used libraries such as NumPy, Pandas, MatPlotLib, SciPy, SciKitLearn, TensorFlow and Simpy.

Everything described here is performed in Python 3, based on the Anaconda Scientific Python environment, available for free (for Windows, Mac or Linux).

Anaconda comes with its own environments for writing the code: Spyder or Jypyter Notebooks. Both are nice. Other Free and Open Source options are PyCharm for more functionality, Atom or Visual Studio Code for a modern code text editor, or Vim for a more old-fashion but very fast and lightweight code text editor.

Occasionally other free libraries may be installed. If so they will be described where appropriate.

I choose to do all my work in GNU/Linux, but everything should also work in Microsoft Windows or Mac OS.

If you are new to this I would recommend installing the Anaconda Scientific Python environment, and then look for ‘Spyder’ in your computer’s application listing. That will open up an ‘Integrated Development Environment’ (IDE): a posh phrase that means a place where you can both write and run code.

For a quick introduction to using Spyder to code Python, see:

Happy coding!