a lot of older code, you might want to use the Python2 version, as Python2 and Python3 codes do not always play well together. If you are working from a clean slate, however, I recommend that you use the Python3 installation as this “future proofs” you somewhat against libraries which make the switch, and no longer support Python2 (the inverse is much rarer, now).
So long as you have chosen Anaconda version (not Mini‐coda), you can just double click the pkg file, and the installation will commence. Once installation is finished (unless you have specific reasons, accept any defaults during installation) you should be able to run.
$ > conda list
If the installation is successful, a list of installed packages will be printed to screen.
But I already have Python installed on my computer? Do I need to uninstall? Anaconda can run alongside any other versions of Python (including any which are installed by the system). In order to make sure that Anaconda is being used, you simply have to make sure that the system knows where it is. This is achieved by editing the PATH environment variable. In order to see whether Anaconda is in your path, run the following command in a Terminal $> echo $PATH To check that Anaconda is set to be the default Python run: $> which python NB the PATH variable should be set by the Anaconda installer, so there is normally no need to do anything.
From here, installing packages is easy. First, search your package on Anaconda's cloud (https://anaconda.org/), and you will be able to choose your package. For example, scikit‐learn's page is at https://anaconda.org/anaconda/scikit‐learn. On each page, the command for installing is given. For scikit‐learn, it looks like this:
$> conda install –c anaconda scikit-learn
Here, the –c flag denotes a specific channel for the conda installer to search to locate the package binaries to install. Usefully, this page also shows all the different operating systems which the package has been built from, so you can be sure that the binary has been built for your system.
Task: Install Anaconda, and use the instructions below to ensure that you have TensorFlow installed on your system
2.3.2.1 Installing TensorFlow
2.3.2.1.1 Without GPU
Neural network training can be significantly accelerated through the use of graphical processing units (GPUs), however they are not strictly necessary. When using smaller architectures and/or working with small amounts of data, a typical central processing unit (CPU) will be sufficient. As such, GPU acceleration will not be required for many of the tasks in this book. Installing Tensorflow without CPU involves a single conda install command:
$> conda install –c conda-forge tensorflow
2.3.2.1.2 With GPU
To make use of TensorFlow's GPU acceleration, you will need to ensure that you have a compute unified device architecture (CUDA)‐capable GPU and all of the required drivers installed on your system. More information on setting up your system for GPU support can be found here: https://www.tensorflow.org/install/gpu
If you are using Linux, you can greatly simplify the configuration process by using the TensorFlow Docker image with GPU support: https://www.tensorflow.org/install/docker
Once you have the prerequisites installed, you can install TensorFlow via:
$> pip install tensorflow
We recommend sticking to conda install commands to ensure package compatibility with your conda environment, however a few earlier examples made use of TensorFlow 1's low‐level application programming interface (API) to illustrate lower‐level concepts. For compatibility, the earlier low‐level API can be used by including the following at the top of your script:
import tensorflow.compat.v1 as tf tf.compat.v1.disable_eager_execution()
This has been included in the code examples wherever necessary.
Note: with the latest version of TensorFlow, this command will install TensorFlow with both CPU and GPU (if available) support.
2.4 Jupyter Notebooks
Jupyter notebooks provide a method to easily create interactive documents capable of hosting and running Python code. This is a great way to work for many scientific applications, allowing you to incorporate descriptive text alongside executable code – helping others to understand and reproduce your work. In this way, they are an excellent tool for collaboration, and can be used to build living documents in which code can be updated, and visualisations can be easily rerun with new data. To use Jupyter notebooks, you will need to first install Jupyter by running:
$> conda install notebook
2.4.1 Why Use a Notebook?
Standard Python scripts have their advantages – the code can be contained within a single file that can easily be run and debugged, without the need to step through and run multiple cells of code. Comments can be added to describe blocks of code, and docstrings can be used to provide more comprehensive descriptions of the script's functionality. So why use a notebook?
While Python scripts have a number of advantages while writing code, there are several shortfalls for their use in sharing your work. Comments and docstrings easily become overwhelming when detailed descriptions are required – turning a clean script into an awkward mess of comments and code blocks. This can make it difficult for others to read and understand what's going on. Furthermore, it does not allow you to incorporate plotting or other visualisations alongside your text. This is where notebooks come in.
Notebooks allow you to create a well‐structured document that incorporates executable code. The structuring allows you to create a comprehensive, flowing document – in the same way that you would for a paper or report. Unlike a traditional document, notebooks are enriched by their ability to incorporate executable code; code that can be used to run real experiments alongside the text and/or to produce interactive data visualisations. This is particularly important for sharing code and facilitating usability – providing a far more intuitive method of sharing code than a collection of Python scripts.
2.4.2 Starting a Jupyter Notebook Server
To use Jupyter notebooks, you will need to run the Jupyter notebook server. This can be done simply by executing the following:
$> jupyter notebook
It is recommended that you execute this from the directory you wish to work from, as this makes accessing existing project content and creating new content easy. Once the above command is executed, Jupyter should prompt your browser to open Jupyter's web User interface (UI), taking you to a page that looks like this:
You can then click “new,” which will give the option of creating a new file.
To create a new Python notebook, simple click on the Python option (in this case, Python 3). You will then be presented with a new empty notebook.
The Jupyter notebook is connected to an iPython kernel, which will allow you to execute Python code in