{"id":1393,"date":"2018-10-24T04:05:59","date_gmt":"2018-10-24T04:05:59","guid":{"rendered":"https:\/\/www.codeastar.com\/?p=1393"},"modified":"2018-10-24T04:05:59","modified_gmt":"2018-10-24T04:05:59","slug":"deep-learning-amazon-web-services-aws","status":"publish","type":"post","link":"https:\/\/www.codeastar.com\/deep-learning-amazon-web-services-aws\/","title":{"rendered":"AWS Tutorial: Deep Learning on Amazon Web Services"},"content":{"rendered":"
So far, we have entered several Kaggle’s machine learning competitions<\/a>. Most of the time, we use Kaggle’s free kernel to solve the puzzles. From the Deal Probability Prediction challenge<\/a>, we reached the limit of Kaggle’s kernel — the 17 GB RAM limit, while we were training our model. When we added more training data or ran more training cycles, it turned out using up the kernel memory and stopped processing. At the end, we had to trim our training scale for the limitation. Is there a better way to handle this case? Of course. We can buy a high end computer with powerful graphics cards as a deep learning machine. But it may be costly and bulky. So we have another option: we use Amazon Web Services (AWS) as our machine learning platform.<\/p>\n <\/p>\n There are 2 ways to run machine learning on AWS. First, Amazon SageMaker. The SageMaker is a fully managed service for machine learning. In this service, Amazon will provide ML optimized instances and algorithms for developers. So developers can focus on training their models (the grey part in the following diagram). Then leave server management, endpoint interface and deployment stuff to Amazon.<\/p>\n <\/p>\n (image source:\u00a0https:\/\/docs.aws.amazon.com\/sagemaker\/ )<\/p>\n Other than SageMaker, we can use the traditional way to run ML on AWS — the EC2 way. i.e. We open an EC2 instance on AWS, then run ML on it.<\/p>\n If you are looking for a straight to production data science solution, SageMaker will be the service you want. Since we are looking for a ML platform to replace Kaggle’s kernel, the “ALL-YOU-CAN-DO” EC2 instance can port our ML work easily.<\/p>\n Like the last time we deployed our Flask app docker<\/a> on Amazon, we must have an AWS account to run following exercise.<\/p>\n Once the account is ready, let’s go to the AWS admin console.<\/p>\n First, we go to EC2 service page\u00a0 by clicking “Services<\/em>” and then “EC2<\/em>” at the top of the menu bar.<\/p>\n <\/p>\n Then we click the “Launch Instance<\/em>” button to create our instance.<\/p>\n <\/p>\n We are now in the Amazon Machine Image (AMI) selecting page. As we are creating a Deep Learning instance, so we enter “Deep” as the image keyword. Amazon will then show us a\u00a0 list of related AMIs.<\/p>\n <\/p>\n I select “Deep Learning AMI (Ubuntu) Version 16.0<\/em>” as our image, because it is integrated with deep learning frameworks we need. You can also select other images to build and customize your deep learning frameworks.<\/p>\n Then we are at the instance type selection page. There are different kinds of instance, some of them are for general purpose, some of them are memory optimized and some of them, well, are GPU included. And once my uncle told me, “With great power comes great…. pricing<\/em>“. We can check out the EC2 pricing\u00a0here<\/a>, and pick a valuable and useful instance for our projects.<\/p>\n As we are making a Kaggle’s kernel replacement, we should find an instance with similar config. So I pick “t2.xlarge<\/em>” instance with 16GB RAM which costs USD 0.1856 per hour. We then click “Next: Configure Instance Details<\/em>” to continue.<\/p>\n <\/p>\n After that, we keep clicking “Next<\/em>” buttons until we go to “Step 6:\u00a0Configure Security Group<\/em>“.<\/p>\n It is an important step, don’t miss it<\/p>\n Since we will use jupyter notebook on our deep learning instance. We must open the jupyer notebook port (default: 8888) for us to access. On “Step 6<\/em>” interface, let’s click “Add Rule<\/em>” to add new network security setting, then enter “8888<\/em>” in port range. I select “Anywhere<\/em>” on source for easy access. You can change the source to “My IP<\/em>“, so only you can access the jupyter notebook.<\/p>\n <\/p>\n After that, we click “Review and Launch<\/em>” at the bottom to the review page. Finally, we are just one step away from starting our deep learning EC2 instance.<\/p>\n <\/p>\n What are you waiting for? Let’s click the “Launch<\/em>” button!<\/p>\n Amazon will then ask us to create or use existing key pair to access our instance. Remember to pay extra attention on the private key file, as it is our only key to open the door of the instance.<\/p>\n <\/p>\n After all of the setup process, it is now the time to connect our deep learning instance. It will be more straight forward if you are using Mac OS \/ Linux. From the Instance home page, we can click the “Connect<\/em>” button, Amazon will display all the necessary commands for connection (remember to replace the private key file and the public DNS to your own values).<\/p>\n <\/p>\n For Windows OS users, we can use PuTTY to change the PEM file to PPK file , then connect the instance via the PuTTY client. For more details, you can check the official Amazon document here<\/a>.<\/p>\n Once you have connected the instance, no matter using Mac OS way or Windows OS way, you will see similar screen from your SSH client:<\/p>\n <\/p>\n Ohhhhhhhh Yeah~ We are now in the deep learning instance we have created. There so many deep learning frameworks installed. Let’ start our journey from our\u00a0familiar framework, TensorFlow with Keras. So we run following commands:<\/p>\n The jupyter notebook server is now started at our instance:<\/p>\n <\/p>\n Our jupyter server is started, now we can code\u00a0our machine learning on a browser. But wait, since the jupyter server is started on an EC2 instance, we need to create a client connecting to the instance from our machine. Depending on the OS you are using, there are different ways to create a client to EC2. You can take a look on Amazon’s official document here<\/a>. I have tried both Windows and Mac ways, it should be no problem for everyone.<\/p>\n Once the client is ready, we connect to the jupyter notebook by typing http:\/\/127.0.0.1:8157 (Mac OS) or http:\/\/127.0.0.1:8888 (Windows OS) on a browser. Following screen will show up for asking your jupyter notebook token.<\/p>\n <\/p>\n We can run following command from the client on the EC2 instance to get the juypter token.<\/p>\n After entering the right token, finally, we are here:<\/p>\n <\/p>\n The next things we do are the usual things we do in Kaggle’s kernel \/ self-hosted Anaconda<\/a>: creating a new notebook, uploading input datasets, training and testing our model.<\/p>\nMachine Learning on a Cloud<\/h3>\n
EZ EC2 setup for Machine Learning<\/h3>\n
For Jupyter<\/h3>\n
Running Deep Learning on EC2<\/h3>\n
$ source activate tensorflow_p36\r\n$ jupyter notebook --no-browser\r\n<\/pre>\n
Training on the Cloud<\/h3>\n
$jupyter notebook list<\/pre>\n
AWS Deep Learning Hands-On<\/h3>\n