Skip to content

leriomaggio/deep-learning-keras-tensorflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deep Learning with Keras and Tensorflow


Author: Valerio Maggio

PostDoc Data Scientist @ FBK/MPBA

Contacts:

@leriomaggio +ValerioMaggio
valeriomaggio vmaggio_at_fbk_dot_eu
git clone https://github.com/leriomaggio/deep-learning-keras-tensorflow.git

Outline (in ten-ish notebooks)

  1. Multi-layer Fully Connected Networks (and the backends)
  2. Hidden Layers features and Embeddings
  3. Convolutional Networks
  4. Hyperparameter Tuning
  5. Cutsom Layers
  6. Deep CNN and Residual Networks
  7. Transfer Learning and Fine Tuning
  8. Recursive Neural Networks
  9. AutoEncoders
  10. Multi-Modal Networks

Requirements

This tutorial requires the following packages:

(Optional but recommended):

The easiest way to get (most) these is to use an all-in-one installer such as Anaconda from Continuum. These are available for multiple architectures.


Python Version

I'm currently running this tutorial with Python 3 on Anaconda

!python --version
Python 3.5.2

Setting the Environment

In this repository, files to re-create virtual env with conda are provided for Linux and OSX systems, namely deep-learning.yml and deep-learning-osx.yml, respectively.

To re-create the virtual environments (on Linux, for example):

conda env create -f deep-learning.yml

For OSX, just change the filename, accordingly.

Notes about Installing Theano with GPU support

NOTE: Read this section only if after pip installing theano, it raises error in enabling the GPU support!

NVIDIA Drivers and CuDNN must be installed and configured before hand. Please refer to the official Theano documentation for further details.

Since version 0.9 Theano introduced the libgpuarray in the stable release (it was previously only available in the development version).

The goal of libgpuarray is (from the documentation) make a common GPU ndarray (n dimensions array) that can be reused by all projects that is as future proof as possible, while keeping it easy to use for simple need/quick test.

Here are some useful tips (hopefully) I came up with to properly install and configure theano on (Ubuntu) Linux with GPU support:

  1. [If you're using Anaconda] conda install theano pygpu should be just fine!

Sometimes it is suggested to install pygpu using the conda-forge channel:

conda install -c conda-forge pygpu

  1. [Works with both Anaconda Python or Official CPython]

After Theano is installed:

echo "[global]
device = cuda
floatX = float32

[lib]
cnmem = 1.0" > ~/.theanorc

Installing Tensorflow

To date tensorflow comes in two different packages, namely tensorflow and tensorflow-gpu, whether you want to install the framework with CPU-only or GPU support, respectively.

For this reason, tensorflow has not been included in the conda envs and has to be installed separately.

Tensorflow for CPU only:

pip install tensorflow

Tensorflow with GPU support:

pip install tensorflow-gpu

Note: NVIDIA Drivers and CuDNN must be installed and configured before hand. Please refer to the official Tensorflow documentation for further details.

Important Note:

All the code provided+ in this tutorial can run even if tensorflow is not installed, and so using theano as the (default) backend!

This is exactly the power of Keras!

Therefore, installing tensorflow is not stricly required!

+: Apart from the Theano and Tensorflow example that are very specific. Anyway you can skip executing those parts, of course.

Configure Keras with tensorflow

By default, Keras is configured with theano as backend.

If you want to use tensorflow instead, these are the simple steps to follow:

  1. Create the keras.json (if it does not exist):
touch $HOME/.keras/keras.json
  1. Copy the following content into the file:
{
    "epsilon": 1e-07,
    "backend": "tensorflow",
    "floatx": "float32",
    "image_data_format": "channels_last"
}
  1. Verify it is properly configured:
!cat ~/.keras/keras.json
{
	"epsilon": 1e-07,
	"backend": "tensorflow",
	"floatx": "float32",
	"image_data_format": "channels_last"
}

Test if everything is up&running

1. Check import

import numpy as np
import scipy as sp
import pandas as pd
import matplotlib.pyplot as plt
import sklearn
import keras
Using TensorFlow backend.

2. Check installed Versions

import numpy
print('numpy:', numpy.__version__)

import scipy
print('scipy:', scipy.__version__)

import matplotlib
print('matplotlib:', matplotlib.__version__)

import IPython
print('iPython:', IPython.__version__)

import sklearn
print('scikit-learn:', sklearn.__version__)
numpy: 1.11.1
scipy: 0.18.0
matplotlib: 1.5.2
iPython: 5.1.0
scikit-learn: 0.18
import keras
print('keras: ', keras.__version__)

# optional
import theano
print('Theano: ', theano.__version__)

import tensorflow as tf
print('Tensorflow: ', tf.__version__)
keras:  2.0.2
Theano:  0.9.0
Tensorflow:  1.0.1

If everything worked till down here, you're ready to start!