Book Image

Hands-On Neural Networks

By : Leonardo De Marchi, Laura Mitchell
Book Image

Hands-On Neural Networks

By: Leonardo De Marchi, Laura Mitchell

Overview of this book

Neural networks play a very important role in deep learning and artificial intelligence (AI), with applications in a wide variety of domains, right from medical diagnosis, to financial forecasting, and even machine diagnostics. Hands-On Neural Networks is designed to guide you through learning about neural networks in a practical way. The book will get you started by giving you a brief introduction to perceptron networks. You will then gain insights into machine learning and also understand what the future of AI could look like. Next, you will study how embeddings can be used to process textual data and the role of long short-term memory networks (LSTMs) in helping you solve common natural language processing (NLP) problems. The later chapters will demonstrate how you can implement advanced concepts including transfer learning, generative adversarial networks (GANs), autoencoders, and reinforcement learning. Finally, you can look forward to further content on the latest advancements in the field of neural networks. By the end of this book, you will have the skills you need to build, train, and optimize your own neural network model that can be used to provide predictable solutions.
Table of Contents (16 chapters)
Free Chapter
1
Section 1: Getting Started
4
Section 2: Deep Learning Applications
9
Section 3: Advanced Applications

Environment setup

There are only a few viable programming language options when creating ML software. The most popular ones are Python and R, but Scala is also quite popular. There are other languages, but the better ones in terms of use in ML are Julia, JavaScript, Java, and a few others. In this book, we will be using Python only. The motivation behind this choice is its widespread adoption, its simplicity of use, and the vast ecosystem of libraries that are possible to use.

In particular, we will be using Python 3.7 and a few of its following libraries:

  • numpy: For fast vectorized numerical computation
  • scipy: Built on top of numpy, with many mathematical functionalities
  • pandas: For data manipulation
  • scikit-learn: The main Python library for ML
  • tensorflow: The engine that powers our deep learning algorithms
  • keras: The library we are going to use to develop our deep learning algorithms, which sits on top of TensorFlow

Let's focus on the last two libraries for the moment. There are a few libraries that are useful nowadays for Neural Networks (NNs). The most widespread is TensorFlow. It is a symbolic math library that uses a directed graph to model the dataflow between operations.

It's particularly suitable for matrix multiplications as it can use all the power of the GPU's architecture, composed by many, but not particularly powerful, cores that can execute many operations simultaneously. TensorFlow is also quite versatile as it works on a few different platforms; it's possible to run models on mobile devices using TensorFlow Lite and even on a browser using TensorFlow.js (https://www.tensorflow.org/js).

One of the libraries we are going to use for most of this book will be Keras. It is a layer that sits on the top of libraries such as TensorFlow to provide a more abstract interface to the end users. In this regard, Keras is narrower than TensorFlow as it focuses on neural networks, but it's also more generic as it can be used in conjunction with TensorFlow's alternatives such as the Microsoft Cognitive Toolkit.

Now, these libraries don't guarantee backward compatibility. Because of this, when working with Python, it's good practice to work in virtual environments. This allows us to isolate the different projects we are working on, but also to distribute our setup to different machines in an easy way and to avoid compatibility issues.

There are a few ways we can create these environments; in this book, we will cover the following:

  • Virtual environment
  • Anaconda
  • Docker

We will go through each one of them to show how they work and explain their advantages and disadvantages.

Understanding virtual environments

While working with Python, you will probably use a multitude of libraries or packages. The virtual environment called venv is the first and most immediate way of making a working setup that is easy to reproduce.

From Python 3.3, the venv module is Python's built-in module, meaning you don't need to install any external components.

To create an environment in an automated way, it's necessary to create a list with all the libraries you want to install. Pip has a very simple way of defining this list, and it's enough to create a .txt file and specify one library per line.

To create the environment, you will need to perform the following steps:

  1. Install Python 3.7.
  2. Create a new environment called dl_venv_pip by using the following command:
python3.7 -m venv dl_venv_pip
  1. Specify the required libraries in a file that we will call requirements.txt, with the following content:
numpy==1.15.4
scipy==1.1.0
pandas==0.23.4
tensorboard==1.12.1
tensorflow==1.11.0
scipy==1.1.0
scikit-learn==0.20.1
Keras==2.2.4

  1. It's possible to now install all the required libraries with this command:
pip install -r /path/to/requirements/requirements.txt
  1. Now we can activate our environment by typing in the following command:
source dl_venv_pip/bin/activate

Once the environment is activated, all your calls to the Python interpreter will be redirected to the environment's interpreter. It's a quick and easy way to distribute the requirements, but it's still possible to have compatibility issues due to different operating systems, and also it might require some time to install all libraries as many data science projects rely on many of them.

Anaconda

One of the main drawbacks encountered while using Python for data science is the amount of libraries that are necessary to install. Also, when provisioning instances for deploying your models, you will need to install all the necessary libraries to run your program, which might be problematic if you deploy to different platforms and operating systems.

Luckily, there are few alternatives to venv. One of them is Anaconda: a free and open source Python distribution for data science and machine learning. It aims to simplify package management and deployment. Anaconda's package manager is called conda and it installs, runs, and updates packages and their dependencies.

It's possible to have a smaller version of conda with a subset of the main libraries, which is called miniconda. This version is quite handy when only the main libraries are needed as it reduces the size of the distribution and the time to install it.

To create an environment in an automated way, it's necessary to create a list of dependencies, as we did with venv. Conda is compatible with the pip format, but it also supports the more expressive YAML format. Let's see how this is done by performing the following steps:

  1. For example, let's create a dl.yaml file with the following content:
name: dl_env          # default is root
channels:
- conda
dependencies: # everything under this, installed by conda
- numpy
- scipy
- pandas
- Tensorflow
- matplotlib
- keras
- pip: # everything under this, installed by pip
- gym
  1. Place dl.yaml in a directory of your choice, and from the terminal in the same location of the file, enter the following command:
conda env create python=3.7 --file dl_env.yaml
  1. It's also necessary to activate what. It's possible to do this at any point after installation by typing activate:
conda activate dl_env

Now all the Python calls will be directed to the Python's in the conda environment that we created.

Docker

Docker is something in between a virtual environment and a virtual machine. It performs operating system-level virtualization, also known as containerization, that isolates applications from the operating system to reduce compatibility problems because of libraries or system versions.

As the name suggests, a good analogy for Docker is shipping containers. They solve the following two problems:

  • Transporting different goods, that are sometimes incompatible with each other—for example, food and chemicals
  • Standardizing the dimension of packages of different sizes

Docker works in a similar way by isolating applications from each other and makes use of a standard layer to allow the spinning up of containers rapidly without much overhead.

One of the main advantages of data science is that it will (almost) solve the it was working on my machine problem and will make it easier and faster to distribute the environment to other people or to production systems.

To install Docker, please visit https://www.docker.com/get-started, where you can find the appropriate installer for your system. Then it's possible to install the libraries we installed before.