Machine Learning for Beginners

Niki Manoledaki
Analytics Vidhya
Published in
5 min readMar 2, 2020

--

How to set up an AWS EC2 machine learning instance to learn more about Keras models and jupyter notebooks.

The four learning areas of machine learning

According to TensorFlow’s website, there are four learning areas to machine learning: “coding, math, ML theory, and how to build your own ML project from start to finish.”

You will find three curriculums on their website — the first one one goes over the basics of machine learning, the second one delves deeper into the theoretical aspect of ML, and the third one focuses on JavaScript for ML.

I started with the first curriculum, Basics of machine learning with TensorFlow, to get a solid understanding of the four learning areas (coding, math, ML theory, building your own project).

You might want to make your way towards the third curriculum once you feel comfortable with these building blocks. If you have some proficiency in JavaScript and MVC frameworks, you could make use of pre-trained models on TensorFlow’s website and build some simple web apps with them!

“Step 1: Understand what ML is all about”

The recommended reading listed on TensorFlow’s first curriculum is the book “Deep Learning with Python” by Francois Chollet, the creator of Keras.

Cover of the book “Deep Learning with Python”, by Francois Chollet
TL;DR: The first four chapters of this book should be enough to get you started.

Chapter 1 introduces important deep learning concepts, such as inputs, layers, tags, targets, backpropagation, etc.

“Deep learning is a specific subfield of machine learning: a new take on learning representations from data that puts an emphasis on learning successive layers of increasingly meaningful representations.” — Francois Chollet, Deep Learning with Python, p. 8.

You will also learn about the relation between the field of ML and statistics (such as probabilistic modeling), which is an important lesson to learn early on. Much of what ML tries to achieve has been the aim of the field of statistics for a long time, but as computing power increases, so does our ability to increase the speed of our research methods. I suggest that you finish this chapter in its entirety before moving on.

Chapter 2 goes over the mathematical building blocks of neural networks through tensors, matrices, and calculus for ML, while Chapter 3 introduces some of the tools available to train your own neural network.

It was quite useful to read through 2.3 in order to understand how data is represented in machine learning (hint: tensors). However, I found myself not understanding the increasingly complex topics without applying this knowledge, for example, by trying out the many examples provided by Chollet.

At this point, I found it helpful to jump over to Chapter 3 and read up to section 3, “Setting up a deep-learning workstation.” Before jumping back into the book, there are a few instructions to follow on how to set up the right environment.

Setting up your AWS workspace

Unless you have a gaming computer and/or an NVIDIA GPU, your computer may not be the ideal environment to bear the brunt of machine learning experiments. I decided to opt for an AWS EC2 GPU instance. My first step was to follow this tutorial on how to connect to an AWS instance. An instance is a server located somewhere in the world. Mine is in Ohio. Nice!

Make sure you do create a pem key — this step is very important. The pem key will allow you to access the AWS server through SSH magic. I fiddled with it a bit — which was great hacky fun — but when I tried to move on to the next step in Amazon’s tutorial, I got an error message.

WARNING: UNPROTECTED PRIVATE KEY FILE!

I wanted to test my private key, such as retrieving the public key of the key pair for the AWS instance.

To do this, I reverted to safe settings.

$ sudo chmod 600 ~/downloads/key.pem
$ sudo chmod 755 ~/.ssh
Try this reboot if you’re experiencing pem key errors.

It worked! I connected to my AWS instance.

Make sure to hold on to the SSH tunnelling command to connect to future instances.

$ ssh -L localhost:8888:localhost:8888 -i key.pem ubuntu@[the-public-DNS-address-changes-every-time]

Follow the last steps in the AWS tutorial, and you should soon have access to a jupyter notebook in your browser. 🎉

Now, clone into the textbook’s Github repo to access all the exercises.

$ git clone https://github.com/fchollet/deep-learning-with-python-notebooks.git cd deep-learning-with-python-notebooks

I wanted to change the backend of my EC2 instance to tensorFlow, so I followed the steps in this tutorial provided by Keras. I began by implementing the steps in this tutorial on my local machine, just in case I have to test something on a jupyter notebook without having to open up the EC2 instance. My local machine had a .keras folder, but no keras.json, so I had to make one.

$ cd $HOME/.keras
$ touch keras.json
$ vim keras.json

A quick reminder about vim — press I to insert text, then ESC when you’re finished, and :wq (write/quit) to save your work and exit vim.

{ 
“image_data_format”: “channels_last”,
“epsilon”: 1e-07,
“floatx”: “float32”,
“backend”: “tensorflow”
}

Back on my EC2 instance, I was getting errors when I tried running the first jupyter notebook code. I realised that tensorFlow was not installed (or activated?) on my EC2 instance. If you find yourself in the same boat, first check that your instance has Keras as well as tensorFlow by listing the pip packages installed for Python 2 (pip) and Python 3 (pip3).

$ pip list
$ pip3 list

Then, if it isn’t there, activate tensorFlow with the following command.

$ source activate tensorflow_p36

You should now be able to follow the examples in the textbook and train the models bit by bit in your jupyter notebook. Nice! 🙌

--

--

Niki Manoledaki
Analytics Vidhya

Software engineer with a cloud native focus. Currently building backend services and maintaining eksctl @ WeaveWorks.