Workshop – Introduction to Neural Networks using TensorFlow and Keras

Executing these notebooks on Azure is fine, however, for improved performance and reliability it is best to run it on a local machine or, ideally, on more powerful GPU hardware

You can download a complete library from Azure Notebooks as a zip

(local) installation goes like this:

  1. Install Anaconda 3 for Python 3.6: https://www.anaconda.com/download/

If all checks below pass and you can download the training material you are good to go, no matter what platform you are on

Notebook Basics

  • This is a Jupyter Notebook
  • You type commands which will be executed on a server (possibly remote)
  • A notebook saves all output of the commands executed, thus you can view it including results without executing it
  • Before using a notebook to type or edit commands make sure you execute all code
  • Do this by selecting Kernel, Restart & Run All in the menu

Tips

  • This is your personal notebook, add Python comments or markdown to take notes

Executing Cells

  • Everthing can be down using the Menu and Toolbar
  • Using the keyboard you can be faster, click on the Keyboard Symbol in the toolbar to learn about more short cuts
  • The most important shortcut is Shit+Enter which executes a cell and navigates to the next one

Preparations

Import and check required dependencies


In [1]:
import warnings
warnings.filterwarnings('ignore')

In [2]:
%matplotlib inline
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [3]:
from distutils.version import StrictVersion

In [4]:
import sklearn
print(sklearn.__version__)

assert StrictVersion(sklearn.__version__ ) >= StrictVersion('0.18.1')


0.18.1

In [5]:
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
print(tf.__version__)

assert StrictVersion(tf.__version__) >= StrictVersion('1.1.0')


1.3.0

In [6]:
import keras
print(keras.__version__)

assert StrictVersion(keras.__version__) >= StrictVersion('2.0.0')


Using TensorFlow backend.
2.0.8

In [7]:
import pandas as pd
print(pd.__version__)

assert StrictVersion(pd.__version__) >= StrictVersion('0.20.0')


0.20.1

How does Tensorflow Low Level API look like?


In [8]:
# graph definition
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)

# launching the graph in a session
with tf.Session() as sess:
    result = sess.run([product])
    print(result)


[array([[ 12.]], dtype=float32)]

Interactive usage of Low Level API


In [9]:
sess = tf.InteractiveSession()

x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])

# Initialize 'x' using the run() method of its initializer op.
x.initializer.run()

# Add an op to subtract 'a' from 'x'.  Run it and print the result
sub = tf.subtract(x, a)
print(sub.eval())
# ==> [-2. -1.]

# Close the Session when we're done.
sess.close()


[-2. -1.]

Calling a TensorFlow Model deployed on Google Cloud ML Service


In [16]:
!cat sample_iris.json
# Example for iris, model exported as Tensorflow
# gsutil cp -R 1 gs://irisnn
# create model and version at https://console.cloud.google.com/mlengine
# in a DOS shell on local machine in this folder
# gcloud ml-engine predict --model=irisnn --json-instances=./sample_iris.json
# SCORES
# [0.9954029321670532, 0.004596732556819916, 3.3544753819114703e-07]


{"inputs": [ 5.1,  3.5,  1.4,  0.2]}

What can Keras do for us?

  • Abstracting from defining operations on tensors
  • Just define the network architecture and you are done
  • Keras compile the description of the model down to TensorFlow operations
  • Many other backends are supported

https://github.com/fchollet/keras#getting-started-30-seconds-to-keras

Our task for today

Train a system to classify speed limit signs:

Getting the data


In [1]:
!curl -O https://raw.githubusercontent.com/DJCordhose/speed-limit-signs/master/data/speed-limit-signs.zip
from zipfile import ZipFile
zip = ZipFile(r'speed-limit-signs.zip')
zip.extractall('.')


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1810k  100 1810k    0     0  5225k      0 --:--:-- --:--:-- --:--:-- 5518k

In [11]:
# Calling a Keras based model to classify speed limit signs
!curl -H "Content-Type: application/json" -X GET -d '{"url": "https://github.com/DJCordhose/speed-limit-signs/raw/master/data/real-world/4/100-sky-cutoff-detail.jpg", "model": "default" }' http://ec2-52-43-39-37.us-west-2.compute.amazonaws.com:8888


{
  "category": [
    4
  ], 
  "model": "default", 
  "prediction": [
    [
      5.188912632902998e-10, 
      1.7854775817560675e-12, 
      6.658332507774913e-23, 
      1.2231632354087196e-05, 
      0.9999877214431763, 
      5.86981815167291e-16
    ]
  ]
}

Types of Machine Learning

What do you mean by Machine Learning?

Books