Learn practical skills, build real-world projects, and advance your career

Exercise 8 - Introduction to Neural Networks

Originally hypothesised in the 1940s, neural networks are now one of the main tools used in modern AI. Neural networks can be used for both regression and categorisation applications. Recent advances with storage, processing power, and open-source tools have allowed many successful applications of neural networks in medical diagnosis, filtering explicit content, speech recognition and machine translation.

In this exercise we will compare three dog breeds, using their age, weight, and height. We will make a neural network model to classify the breeds of the dogs based on these features.

Note: It's extremely common for AI practitioners to use a template such as the one below for making neural networks quickly. After you are done, feel free to play around with the template to get a feel of how you can easily adjust a neural network to your problems using Keras.

Run the code in the cell below.

# Run this!
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
import keras
print('keras using %s backend'%keras.backend.backend())
import pandas as pd
import numpy as np
import matplotlib.pyplot as graph
%matplotlib inline
graph.rcParams['figure.figsize'] = (15,5)
graph.rcParams["font.family"] = 'DejaVu Sans'
graph.rcParams["font.size"] = '12'
graph.rcParams['image.cmap'] = 'rainbow'
Using TensorFlow backend.
keras using tensorflow backend
pip --version
pip 19.1.1 from C:\Users\Philip\Anaconda3\lib\site-packages\pip (python 3.6) Note: you may need to restart the kernel to use updated packages.
pip install keras
Collecting keras Downloading https://files.pythonhosted.org/packages/ad/fd/6bfe87920d7f4fd475acd28500a42482b6b84479832bdc0fe9e589a60ceb/Keras-2.3.1-py2.py3-none-any.whl (377kB) Requirement already satisfied: numpy>=1.9.1 in c:\users\philip\anaconda3\lib\site-packages (from keras) (1.16.3) Requirement already satisfied: pyyaml in c:\users\philip\anaconda3\lib\site-packages (from keras) (5.1) Requirement already satisfied: six>=1.9.0 in c:\users\philip\anaconda3\lib\site-packages (from keras) (1.12.0) Requirement already satisfied: keras-preprocessing>=1.0.5 in c:\users\philip\anaconda3\lib\site-packages (from keras) (1.1.0) Requirement already satisfied: h5py in c:\users\philip\anaconda3\lib\site-packages (from keras) (2.9.0) Requirement already satisfied: scipy>=0.14 in c:\users\philip\anaconda3\lib\site-packages (from keras) (1.2.1) Requirement already satisfied: keras-applications>=1.0.6 in c:\users\philip\anaconda3\lib\site-packages (from keras) (1.0.8) Installing collected packages: keras Successfully installed keras-2.3.1 Note: you may need to restart the kernel to use updated packages.
pip install tensorflow==2.0
Collecting tensorflow==2.0 Downloading https://files.pythonhosted.org/packages/d3/af/296748d4c8d8987423231b93aecce5ab5952f6f2243cb6cedb88dd425397/tensorflow-2.0.0-cp36-cp36m-win_amd64.whl (48.1MB) Collecting keras-preprocessing>=1.0.5 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/28/6a/8c1f62c37212d9fc441a7e26736df51ce6f0e38455816445471f10da4f0a/Keras_Preprocessing-1.1.0-py2.py3-none-any.whl (41kB) Collecting absl-py>=0.7.0 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/1a/53/9243c600e047bd4c3df9e69cfabc1e8004a82cac2e0c484580a78a94ba2a/absl-py-0.9.0.tar.gz (104kB) Collecting keras-applications>=1.0.8 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/71/e3/19762fdfc62877ae9102edf6342d71b28fbfd9dea3d2f96a882ce099b03f/Keras_Applications-1.0.8-py3-none-any.whl (50kB) Collecting google-pasta>=0.1.6 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/c3/fd/1e86bc4837cc9a3a5faf3db9b1854aa04ad35b5f381f9648fbe81a6f94e4/google_pasta-0.1.8-py3-none-any.whl (57kB) Collecting tensorboard<2.1.0,>=2.0.0 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/76/54/99b9d5d52d5cb732f099baaaf7740403e83fe6b0cedde940fabd2b13d75a/tensorboard-2.0.2-py3-none-any.whl (3.8MB) Collecting opt-einsum>=2.3.2 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/b8/83/755bd5324777875e9dff19c2e59daec837d0378c09196634524a3d7269ac/opt_einsum-3.1.0.tar.gz (69kB) Collecting gast==0.2.2 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/4e/35/11749bf99b2d4e3cceb4d55ca22590b0d7c2c62b9de38ac4a4a7f4687421/gast-0.2.2.tar.gz Collecting grpcio>=1.8.6 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/c8/06/b48791b09038afc53ade40acb4a22b68cb8953ddff6b001df0ed06220baa/grpcio-1.26.0-cp36-cp36m-win_amd64.whl (1.9MB) Requirement already satisfied: wheel>=0.26 in c:\users\philip\anaconda3\lib\site-packages (from tensorflow==2.0) (0.33.4) Collecting protobuf>=3.6.1 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/90/c8/4e194807d23092a1bcfe0f2690b39b1b93ab3c4cdb3515d658c7420198a8/protobuf-3.11.2-cp36-cp36m-win_amd64.whl (1.1MB) Collecting tensorflow-estimator<2.1.0,>=2.0.0 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/fc/08/8b927337b7019c374719145d1dceba21a8bb909b93b1ad6f8fb7d22c1ca1/tensorflow_estimator-2.0.1-py2.py3-none-any.whl (449kB) Collecting astor>=0.6.0 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/c3/88/97eef84f48fa04fbd6750e62dcceafba6c63c81b7ac1420856c8dcc0a3f9/astor-0.8.1-py2.py3-none-any.whl Requirement already satisfied: numpy<2.0,>=1.16.0 in c:\users\philip\anaconda3\lib\site-packages (from tensorflow==2.0) (1.16.3) Requirement already satisfied: six>=1.10.0 in c:\users\philip\anaconda3\lib\site-packages (from tensorflow==2.0) (1.12.0) Requirement already satisfied: wrapt>=1.11.1 in c:\users\philip\anaconda3\lib\site-packages (from tensorflow==2.0) (1.11.1) Collecting termcolor>=1.1.0 (from tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.tar.gz Requirement already satisfied: h5py in c:\users\philip\anaconda3\lib\site-packages (from keras-applications>=1.0.8->tensorflow==2.0) (2.9.0) Collecting google-auth<2,>=1.6.3 (from tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/73/38/99d8c4db8788e050f2024032ecaa5e6df276e5befaabdd7e1bad17fe9b00/google_auth-1.10.2-py2.py3-none-any.whl (76kB) Requirement already satisfied: setuptools>=41.0.0 in c:\users\philip\anaconda3\lib\site-packages (from tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) (41.0.1) Collecting markdown>=2.6.8 (from tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/c0/4e/fd492e91abdc2d2fcb70ef453064d980688762079397f779758e055f6575/Markdown-3.1.1-py2.py3-none-any.whl (87kB) Requirement already satisfied: requests<3,>=2.21.0 in c:\users\philip\anaconda3\lib\site-packages (from tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) (2.21.0) Requirement already satisfied: werkzeug>=0.11.15 in c:\users\philip\anaconda3\lib\site-packages (from tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) (0.15.2) Collecting google-auth-oauthlib<0.5,>=0.4.1 (from tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/7b/b8/88def36e74bee9fce511c9519571f4e485e890093ab7442284f4ffaef60b/google_auth_oauthlib-0.4.1-py2.py3-none-any.whl Collecting pyasn1-modules>=0.2.1 (from google-auth<2,>=1.6.3->tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/95/de/214830a981892a3e286c3794f41ae67a4495df1108c3da8a9f62159b9a9d/pyasn1_modules-0.2.8-py2.py3-none-any.whl (155kB) Collecting cachetools<5.0,>=2.0.0 (from google-auth<2,>=1.6.3->tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/08/6a/abf83cb951617793fd49c98cb9456860f5df66ff89883c8660aa0672d425/cachetools-4.0.0-py3-none-any.whl Collecting rsa<4.1,>=3.1.4 (from google-auth<2,>=1.6.3->tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/02/e5/38518af393f7c214357079ce67a317307936896e961e35450b70fad2a9cf/rsa-4.0-py2.py3-none-any.whl Requirement already satisfied: idna<2.9,>=2.5 in c:\users\philip\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) (2.8) Requirement already satisfied: urllib3<1.25,>=1.21.1 in c:\users\philip\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) (1.24.2) Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\philip\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) (3.0.4) Requirement already satisfied: certifi>=2017.4.17 in c:\users\philip\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) (2019.11.28) Collecting requests-oauthlib>=0.7.0 (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/a3/12/b92740d845ab62ea4edf04d2f4164d82532b5a0b03836d4d4e71c6f3d379/requests_oauthlib-1.3.0-py2.py3-none-any.whl Collecting pyasn1<0.5.0,>=0.4.6 (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/62/1e/a94a8d635fa3ce4cfc7f506003548d0a2447ae76fd5ca53932970fe3053f/pyasn1-0.4.8-py2.py3-none-any.whl (77kB) Collecting oauthlib>=3.0.0 (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.1.0,>=2.0.0->tensorflow==2.0) Downloading https://files.pythonhosted.org/packages/05/57/ce2e7a8fa7c0afb54a0581b14a65b56e62b5759dbc98e80627142b8a3704/oauthlib-3.1.0-py2.py3-none-any.whl (147kB) Building wheels for collected packages: absl-py, opt-einsum, gast, termcolor Building wheel for absl-py (setup.py): started Building wheel for absl-py (setup.py): finished with status 'done' Stored in directory: C:\Users\Philip\AppData\Local\pip\Cache\wheels\8e\28\49\fad4e7f0b9a1227708cbbee4487ac8558a7334849cb81c813d Building wheel for opt-einsum (setup.py): started Building wheel for opt-einsum (setup.py): finished with status 'done' Stored in directory: C:\Users\Philip\AppData\Local\pip\Cache\wheels\2c\b1\94\43d03e130b929aae7ba3f8d15cbd7bc0d1cb5bb38a5c721833 Building wheel for gast (setup.py): started Building wheel for gast (setup.py): finished with status 'done' Stored in directory: C:\Users\Philip\AppData\Local\pip\Cache\wheels\5c\2e\7e\a1d4d4fcebe6c381f378ce7743a3ced3699feb89bcfbdadadd Building wheel for termcolor (setup.py): started Building wheel for termcolor (setup.py): finished with status 'done' Stored in directory: C:\Users\Philip\AppData\Local\pip\Cache\wheels\7c\06\54\bc84598ba1daf8f970247f550b175aaaee85f68b4b0c5ab2c6 Successfully built absl-py opt-einsum gast termcolor Installing collected packages: keras-preprocessing, absl-py, keras-applications, google-pasta, protobuf, grpcio, pyasn1, pyasn1-modules, cachetools, rsa, google-auth, markdown, oauthlib, requests-oauthlib, google-auth-oauthlib, tensorboard, opt-einsum, gast, tensorflow-estimator, astor, termcolor, tensorflow Successfully installed absl-py-0.9.0 astor-0.8.1 cachetools-4.0.0 gast-0.2.2 google-auth-1.10.2 google-auth-oauthlib-0.4.1 google-pasta-0.1.8 grpcio-1.26.0 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.1.1 oauthlib-3.1.0 opt-einsum-3.1.0 protobuf-3.11.2 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-oauthlib-1.3.0 rsa-4.0 tensorboard-2.0.2 tensorflow-2.0.0 tensorflow-estimator-2.0.1 termcolor-1.1.0 Note: you may need to restart the kernel to use updated packages.