Category Archives: Uncategorized

Deep Learning on AWS GPU instances with Python and TensorFlow – Part 1

This multipart tutorial will show you how to:

  • Launch a GPU instance on AWS, SSH into it and set up TensorFlow.
  • Blah

I will assume that you have an AWS user account with admin rights and have downloaded the accessKeys.csv file.

Setting up AWS on your local machine

First, install the AWS command line tool using the python pip installer (for other options, see here).

 sudo pip3 install awscli 

Now you should be able to use the aws command:

$ aws
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: too few arguments

In order to In order to use the aws account, we have to provide the right credentials. We can do so running aws configure and inputting the details from you accessKey.csv file:

 $ aws configure
 AWS Access Key ID: <acces_key_id>
 AWS Secret Access Key: <secret_access_key>
 Default region name [us-east-1]: us-east-1
 Default output format [None]: <ENTER>

Your config will be stored in ~/.aws

You can test whether you user has admin by running:

$ aws ec2 describe-instances --output table
-------------------
|DescribeInstances|
+-----------------+

If successful you should see the above. Now create an access group ‘my-sg’ and set access rights with ssh access:

$ aws ec2 create-security-group --group-name my-sg --description "My security group"

$ aws ec2 authorize-security-group-ingress --group-name my-sg \
  --protocol tcp --port 22 --cidr 0.0.0.0/0

Next, Create the ssh access key and save it to  ~/.aws/my_aws_key.pem

$ aws ec2 create-key-pair --key-name my_aws_key \
  --query 'KeyMaterial' --output text &amp;amp;amp;gt; ~/.aws/my_aws_key.pem

  chmod 400 ~/.aws/my_aws_key.pem

Now we are ready to launch our EC2 instance 🙂

Launch Ubuntu 14.04 GPU instance

I like to use a basic Ubuntu 14.04 instance (image-id = ami-fce3c696)

$ aws ec2 run-instances --image-id ami-fce3c696 \
  --count 1 --instance-type g2.2xlarge \
  --key-name my_aws_key

Assuming all has gone well, you should now have an instance up and running!

Increase the size of the file system

The first time i did this, I ran into issues with not having i=enough space on the file system to install everything I needed. Follow the steps below to resize the filesystem.

  1. From aws console, stop the instance
  2. From aws console, detach the volume (though note the mount point under attachment info, eg /dev/sda1)
  3. From aws console, take a snapshot of the volume
  4. From aws console, create a new volume using the snapshot (for my g2.2xlarge i went with 800GB)
  5. From aws console, attach the new volume to original mount point /dev/sda1
  6. From aws console, restart the instance

SSH in to the Instance

The command for getting the IP of all running instances is a little clunky so it can be useful to create and alias in the ~/.bashrc file:

alias aws_get_ip='aws ec2 describe-instances --query "Reservations[*].Instances[*].PublicIpAddress" --output=text'

Now, we can ssh in to the instance like so:

alias aws_get_ip='aws ec2 describe-instances --query "Reservations[*].Instances[*].PublicIpAddress" --output=text'

Install CUDA 7.5

sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get -y install linux-headers-$(uname -r) linux-image-extra-`uname -r`
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_7.5-18_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1404_7.5-18_amd64.deb
rm cuda-repo-ubuntu1404_7.5-18_amd64.deb
sudo apt-get update
sudo apt-get install -y cuda

You should now reboot your machine

sudo reboot

Install cuDNN v4

Register and download the cuDNN v4 from here. You can then put it into your Google Drive folder and share the link:

wget https://www.dropbox.com/s/.../cudnn-7.5-linux-x64-v5.1.tgz
tar xvzf cudnn-7.5-linux-x64-v5.1.tgz
rm cudnn-7.5-linux-x64-v5.1.tgz
sudo cp cuda/include/cudnn.h /usr/local/cuda/include # move library files to /usr/local/cuda
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
rm -rf ~/cuda

Install Anaconda with python 3.5

wget http://repo.continuum.io/archive/Anaconda3-4.2.0-Linux-x86_64.sh
bash Anaconda3-4.2.0-Linux-x86_64.sh -b -p ~/bin/anaconda3
rm Anaconda3-4.2.0-Linux-x86_64.sh
echo 'export PATH="$HOME/bin/anaconda3/bin:$PATH"' >> ~/.bashrc

Setup tensorFlow

pip install tensorflow

Run a MNIST classifier and monitor the system usage

To finish the installation process, let’s run a MNIST classifier and monitor the system usage.

First, install the required packages:

sudo apt-get install htop
pip install gpustat
sudo nvidia-smi daemon  ## run daemon to make monitoring faster

Now start byobu (terminal multiplexer, similar to tmux or GNU screen):

byobu

Next, press Ctrl-F2 to split the window vertically and run htop:

htop

Press Shift-F2 to split the window horizontally and run the continous GPU monitor gpustat:

watch --color -n1.0 gpustat -cp

With Shift-<left> move to the left pannel, download the MNIST classification script and execute it:

wget https://raw.githubusercontent.com/tensorflow/models/master/tutorials/image/mnist/convolutional.py
python convolutional.py

Limit Order Books – An introduction

This is an extract from a draft version of my PhD thesis:

“…For many years, the majority of the worlds financial markets have been driven by a style of auction, very similar to the basic process of haggling, known as the continuous double auction (CDA). In a CDA a seller may announce an offer or accept a bid at any time and a buyer may announce a bid or accept an offer at any time. This continuous and asynchronous process does away with any need for a centralised auctioneer, but does need a system for recording bids and offers and clearing trades. In modern financial markets, this function is performed by a uniform trading protocol known as the limit order book (LOB), whose universal adoption was a major factor in the transformation of financial exchanges.

The most common type of order submitted to a LOB is the limit order – an instruction to buy or sell a given quantity of an asset, that species a limit (worst acceptable) price which cannot be surpassed. Upon receiving a limit order, the exchange’s matching engine compares the order’s price and quantity with opposing orders from the book. If there is is a book order that matches the incoming order then a trade is executed. The new order is termed aggressive (or marketable) because it initiated the trade, while the existing order from the book is deemed passive. If, on the other hand, the there are no matches for the incoming order it is placed in the book along with the other unmatched orders, waiting for an opposing aggressive order to arrive (or until it is cancelled). A visualisation of the structure and mechanism of a LOB is given in Figure 1.1.

limit order book

Figure 1.1: An illustration of LOB structure and dynamics.

The details of oder matching vary across exchanges and assets classes. However, most modern equity markets operate using a price-time priority protocol. That is, the lowest offers and highest bids are considered first, while orders of the same price are differentiated by the time they arrive (with priority given to orders that arrive first). Thus, limit orders with identical prices form a first-in first-out (FIFO) queues.

Most LOB-driven exchanges offer many more order types than the simple limit order. Another particularly common order type is the market order, which ensures a trade executes immediately at the best available price for a given quantity. As a result, market orders demand liquidity and risk uncertainty. Many more order types are available that allow control over whether an order may be partially filled, when an order should become active and how visible the order is. Such order types include: conditional orders, hybrid orders, iceberg orders, stop orders and pegged orders, but the intricacies of these order types are beyond the scope of this report.

Traders interact with a screen-based LOB that summarises all of the “live” (outstanding)
bid and offers that have not yet been cancelled or matched. The LOB has two sides: the
ask book and the bid book. The ask book contains the prices of all outstanding asks, along with the quantity available at each price level, in ascending order. The bid book, on the other hand shows the corresponding information for bids but in descending order; this way traders see the “best” prices at the top of both books. A simplified example of what a trader may see when looking at a LOB is given below.

lob_example

A simplified example of how a trader would view the LOB shown in Figure 1.1.

The amount of information available about the LOB at any given time depends on the needs and resources of the traders. Usually the only information that is publicly available (in real time) is the last traded price or the mid-price (the point between the current best prices). Professional traders may chose to subscribe to receive information on the price and size for the best prices, along with the price and size of the last recorded transaction, of an asset of interest; this is known as “level 1” market data. The most informative information, “level 2” or “market depth” data, includes the complete contents of the book (except for certain types of hidden orders) but this comes at a premium. For individual subscribers, the current cost of receiving real time level 2 data for equities from just the New York Stock Exchange (NYSE) exchange is $5000/month.

At first glance, the rules or limit order trading seem simple but trading in a LOB is a highly complex optimisation problem. Traders may submit buy and/or sell orders at different times, prices, quantities and – in today’s highly fragmented markets – often to multiple order books. Order may also be modi ed or cancelled at any time. The complexity of LOB strategies presents significant challenges for those attempting to model, understand and predicts behaviours. Nonetheless, the well-de ned framework and the vast volumes of data generated by the use of LOBs presents an exciting and value opportunity for computational modeling…”