TensorFlow All-Around
TensorFlow All-Around
13 September 2019
10:00 am Introduction to Python
06:00 pm End
2
Introduction to Python
Instructions
● Please move towards the front/center
● Try to find seats near power plugs
● Try to seat nearer to the aisles
● WIFI: SUTD_Guest
You can find today's material at http://bit.ly/tf_slides and this link.
Pigeonhole (to ask questions): https://pigeonhole.at/TENSORFLOW
3
Part 1
Introduction to Python
10:00 am - 12:30 pm
http://bit.ly/tf_slides
4
Outline
1. Preface
2. Python Basics
3. Python for Scientific Computing
4. Some additional tips (might skip depending on time)
http://bit.ly/tf_slides
5
Preface
6
Preface
7
Preface
9
Python vs other programming languages
Easier Harder
10
Major Python Users
11
Python Advantages
12
What does it mean to
"know" Python?
13
"Knowing English"
14
"Knowing Python"
15
How to get there?
16
Tools for Programming in Python
17
Jupyter Notebook
18
Jupyter Notebook
19
Google Colab
https://colab.research.google.com
20
Python Basics
● Statements
● Objects
● Functions
● Loops & Control Flow
● Object-oriented Programming
21
Python Statements
22
Python Objects
23
Python Functions
24
Python Functions
Arguments
Arguments with default value
25
Python Control Flow
● If-else-elif
○ Used in combination with logic to determine code path
● Try-except
○ Used to handle errors in code
● Loops
○ For-loop and while-loop
26
Python Logic
○ 2 == 2 -> True
27
Notebook: Python Basics
● (Go to Notebook)
● http://bit.ly/tf_slides -> Slide 28
28
Checkpoint
Next: object oriented programming in Python
Slides: http://bit.ly/tf_slides
Pigeonhole: https://pigeonhole.at/TENSORFLOW
29
Object Oriented Programming
30
Object Oriented Programming
○ With behaviors (methods! aka functions) like walking, talking, breathing, and running.
31
Python Classes
32
Inheritance
● Inheritance is the process by which one class takes on the attributes and
methods of another.
● Newly formed classes are called child classes, and the classes that child
classes are derived from are called parent classes.
● It’s important to note that child classes override or extend the functionality
(e.g., attributes and behaviors) of parent classes.
33
Object Oriented Programming
● Notebook
● http://bit.ly/tf_slides -> Slide 34
34
Checkpoint
Next: scientific computing in Python
Slides: http://bit.ly/tf_slides
Pigeonhole: https://pigeonhole.at/TENSORFLOW
35
Scientific Computing
36
Progress is "gated" by computation ability
The angular shape of the Have Blue prototype and production F-117 aircraft is a direct result of the lack
of computational ability to simulate more complex geometry
37
Scientific Computing
38
Climate Modelling (TF/Python @ Exascale)
41
Python for Scientific Computing
● Arrays in Python
● Google Colab tricks
● Some other widely used libraries:
○ Matplotlib (Plotting)
○ SciPy
42
Arrays
43
Array vs Python List
44
Numpy (TLDR version)
45
Numpy
46
Numpy
● All array elements have to be the same type (usually float or integer);
● Array elements can be accessed, sliced, and manipulated in the same way
as the lists;
● Arrays can be N-dimensional;
● The number of elements in the array is fixed;
● Shape of the array can be changed.
47
Brief History of Array Computing in Python
Pandas
Various incompatible libraries Manipulating numerical tables Dask
and time series
Numeric, numarray etc. Theano Dask scales Numpy workflows to
"Dark Ages" Manipulating and evaluating multiple cores or machines
mathematical expressions,
especially matrix-valued ones
2006 2012
48
Numpy
49
Checkpoint
Next: "extras" - we'll skip if there's no time
Slides: http://bit.ly/tf_slides
Pigeonhole: https://pigeonhole.at/TENSORFLOW
50
Google Colab Tricks
51
Plotting with Matplotlib
● Notebook:
https://colab.research.google.com/notebooks/charts.ipynb
52
Images in Python and OpenCV
● Notebook:
https://colab.research.google.com/github/OpenSUTD/machine-learning-w
orkshop/blob/master/labs/Lab%201D%20-%20Images.ipynb
53
SciPy
● statistics, optimization;
● integration, interpolation;
● linear algebra, solving non-linear equations;
● Fourier transforms;
● ODE solvers, special functions;
● signal and image processing.
54
Checkpoint
Next: Introduction to Machine Learning
Slides: http://bit.ly/tf_slides
Pigeonhole: https://pigeonhole.at/TENSORFLOW
55
Part 2
Introduction to Machine Learning
01:30 pm - 03:30 pm
Slides: http://bit.ly/tf_slides
Pigeonhole: https://pigeonhole.at/TENSORFLOW
56
Preface
● This is a ~2h session that aims to give a crash course on deep learning
basics. Where appropriate, additional links are provided for you to learn
more on your own (highly encouraged!)
● For conceptual questions, encouraged to ask on pigeonhole
Do not ask the TA conceptual questions! (TA reject questions pls)
You're unlikely the only person asking the question, asking on pigeonhole
lets everybody benefit and get their question answered.
57
Preface
● Slides: http://bit.ly/tf_slides
● Pigeonhole: https://pigeonhole.at/TENSORFLOW
58
What is Machine Learning?
59
Key reasons to use Machine Learning
60
What does ML look like
Traditional Programming Machine Learning
INPUT
[Training]
RULES
Algorithm
INPUT
RESULTS
Program /
RESULTS
Algorithm
Store as parameters
RULES
[Inference]
INPUT Algorithm RESULTS
+ rules
61
What is Machine Learning?
62
Exponential Growth of ML
63
Exponential Growth of ML
3× 30× 3×
Engineers Compute Resource Used Data Used for ML
Facebook Ranking system Facebook Facebook
64
https://venturebeat.com/2019/07/11/facebook-vp-ai-has-a-compute-dependency-problem/
Machine Learning Algorithms
65
66
Machine Learning Algorithms
67
Deep Learning (DL)
68
Deep Learning
69
Deep Learning vs Neuroscience
70
Deep Learning vs Neuroscience
71
Applications of Deep Learning
Computer Vision
72
Autonomous Driving
73
74
Breaking down Deep Learning
Components
75
DL Model
○ Transformer
76
DL Model
77
Multilayer Perceptron
● A hierarchy of relatively simple models
can model something more complex
y = σ( Σ(wixi) + b)
78
Convolutional Neural Network
● Relative positions of pixels (or any value) of an image (or other inputs)
encodes a certain meaning (feature)
○ Relative positions -> certain "formations" of pixels
79
Problem
The object is the same, but the
pixels are completely different!
80
Convolutional Neural Network
81
Convolutional Neural Network
82
Computer Vision
83
Recurrent Neural Network
84
Recurrent Neural Network
Outputs
Hidden
State
(changes
over time)
Inputs
85
Transformer
86
Dataset
87
Training
88
Training Metric
● The training algorithm will attempt to minimise this error with respect to
the model parameters
89
Training Algorithm
90
Training Algorithm
91
Training Algorithm (Forward Pass)
Loss
Ground
Truth
92
Training Algorithm (Backward Pass)
Model
Loss
minimise (model parameters) w.r.t. loss
93
Training Algorithm Visualisation
● https://playground.tensorflow.org/
94
Checkpoint
Slides: http://bit.ly/tf_slides
Pigeonhole: https://pigeonhole.at/TENSORFLOW
95
Downsides of Deep Learning
● You don't have enough compute, so you can't experiment fast enough
96
97
98
99
Hardware Requirements for DL
100
NVIDIA DGX SuperPOD (#22 on TOP500) 101
Google Cloud TPU v3 Pod 102
Hardware Requirements for DL
103
NVIDIA Tensor Core GPUs
Delivering cutting-edge Deep Learning performance
NVIDIA Tesla T4
Single Precision: 8.1 TFLOPS
Mixed Precision: 65 TFLOPS
300GB/s VRAM (GDDR6)
Ultra-efficient 70W
104
NVIDIA Tensor Core GPUs
Delivering cutting-edge Deep Learning performance
NVIDIA Tesla T4
(~ low power RTX 2070)
105
Tip: Don't be a hero
● Simpler is better
○ Keep up to date, but there's no need to chase the latest "state of the art"
106
Checkpoint
Slides: http://bit.ly/tf_slides
Pigeonhole: https://pigeonhole.at/TENSORFLOW
107
Part 3
Introduction to TensorFlow 2.0
04:00 pm - 06:00 pm
Slides: http://bit.ly/tf_slides
Pigeonhole: https://pigeonhole.at/TENSORFLOW
108
Keras in one slide
● Keras is a super simple Python library for building deep learning models
● Keras provides 99% of the building blocks you need
○ Layers, Activations
○ Optimizers
○ Nice extras
■ Datasets
■ Callbacks
109
TensorFlow in one slide
110
tf.keras
111
TensorFlow Lattice
Components for interpolated
look-up tables in TensorFlow
TensorFlow Data Validation
Library for exploring and validating machine TensorFlow Federated
learning data Machine learning framework for
decentralized data
TensorFlow Transform
Library for preprocessing and manipulating TensorFlow Privacy
data with TensorFlow Training and analysing models with
differential privacy
TensorFlow Model Analysis
Evaluate models using a variety of metrics TensorFlow Probability
and techniques Probabilistic reasoning and
statistical analysis in TensorFlow
TensorFlow Model Optimization
Suite of tools for optimizing ML models for Mesh TensorFlow TensorFlow Agents
deployment and execution Large-scale distributed model Efficient batched reinforcement
parallelism toolkit for TensorFlow learning in TensorFlow
TensorFlow Serving
Scalable, high-performance serving system TensorFlow Hub TensorFlow Graphics
for models Library for transfer learning by Differentiable graphics layers for
reusing parts of TensorFlow models TensorFlow
TensorFlow Extended (TFX)
TensorFlow Datasets TensorFlow Ranking
Provides various public datasets in Components for Learning-to-Rank
for form of tf.data.Datasets (LTR) techniques in TensorFlow
112
Tensor
a multidimensional array (aka ndarray)
Flow
a computational graph
113
import tensorflow as tf
mnist = tf.keras.datasets.mnist
Load Dataset (x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
Define Model tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
Compile into loss='sparse_categorical_crossentropy',
optimized graph
metrics=['accuracy'])
114
tf.keras
● "Low Bar"
Easy to piece together cutting edge models that work
● "High Ceiling"
Write custom pieces and training loops
Do cutting edge research work
https://www.tensorflow.org/beta/tutorials/quickstart/advanced
115
A bit more about Keras
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
116
Dense layer - a fully connected layer
117
Dense layer - a fully connected layer
activation
function
weights
bias
118
A bit more about Keras
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
119
Activation Functions
120
A bit more about Keras
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
121
Dropout
122
A bit more about Keras
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
123
Keras Optimizers
● Adam(lr=0.001, decay=0.0)
○ Good first choice, just pick this
● RMSprop(lr=0.001, decay=0.0)
○ Maybe good choice for RNNs. Try Adam, and then this, and compare.
124
Keras Optimizers
● Learning Rate (lr)
○ Too small == no fitting; too large == diverge/explode
● Decay
○ Multiply learning rate by (1-rate) after every step (not epoch!!)
● Other parameters
○ Usually Optimizers have some other parameters. Unless you know why, there is no real
reason to tweak those. "Sane defaults" are set.
125
A bit more about Keras
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
126
Loss
127
Loss
● binary_crossentropy
○ Possible to have multiple correct labels (e.g. tweet is both sad and angry)
Check if each category (individually) is correct
● categorical_crossentropy
○ Only one category is correct (e.g. picture is a cat, not a dog)
Check if the category is correct (only one out of many)
● mean_square_error
○ Compare input and output for differences (e.g. image reconstruction)
128
Dataset: MNIST
● Handwritten digits
● "Hello world"
129
Dataset: CIFAR
130
Checkpoint
Slides: http://bit.ly/tf_slides
Pigeonhole: https://pigeonhole.at/TENSORFLOW
131
Code Walkthrough
132