SlideShare a Scribd company logo
Introduction to
TensorFlow 2.0
Brad Miro - @bradmiro
Google
Spark + AI Summit Europe - October 2019
Deep Learning
Intro to TensorFlow
TensorFlow @ Google
2.0 and Examples
Getting Started
TensorFlow
Deep Learning
Doodles courtesy of @dalequark
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
Weight
Height
Examples of cats Examples of dogs
Introduction to TensorFlow 2.0
rgb(89,133,204)
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
You have lots of data (~ 10k+ examples)
Use Deep Learning When...
You have lots of data (~ 10k+ examples)
The problem is “complex” - speech, vision, natural language
Use Deep Learning When...
You have lots of data (~ 10k+ examples)
The problem is “complex” - speech, vision, natural language
The data is unstructured
Use Deep Learning When...
You have lots of data (~ 10k+ examples)
The problem is “complex” - speech, vision, natural language
The data is unstructured
You need the absolute “best” model
Use Deep Learning When...
You don’t have a large dataset
Don’t Use Deep Learning When...
You don’t have a large dataset
You are performing sufficiently well with traditional ML methods
Don’t Use Deep Learning When...
You don’t have a large dataset
You are performing sufficiently well with traditional ML methods
Your data is structured and you possess the proper domain knowledge
Don’t Use Deep Learning When...
You don’t have a large dataset
You are performing sufficiently well with traditional ML methods
Your data is structured and you possess the proper domain knowledge
Your model should be explainable
Don’t Use Deep Learning When...
Introduction to TensorFlow 2.0
Open source deep learning library
Utilities to help you write neural networks
GPU / TPU support
Released by Google in 2015
>2200 Contributors
2.0 released September 2019
TensorFlow
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
41,000,000+ 69,000+ 12,000+ 2,200+
downloads commits pull requests contributors
TensorFlow @ Google
AI-powered data
center efficiency
Global localization
in Google Maps
Portrait Mode on
Google Pixel
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
2.0
Scalable
Tested at Google-scale.
Deploy everywhere
Easy
Simplified APIs.
Focused on Keras and
eager execution
Powerful
Flexibility and performance.
Power to do cutting edge research
and scale to > 1 exaflops
TensorFlow 2.0
Deploy anywhere
JavaScriptEdge devicesServers
Introduction to TensorFlow 2.0
TF Probability
TF Agents
Tensor2Tensor
TF Ranking
TF Text
TF Federated
TF Privacy
...
import tensorflow as tf # Assuming TF 2.0 is installed
a = tf.constant([[1, 2],[3, 4]])
b = tf.matmul(a, a)
print(b)
# tf.Tensor( [[ 7 10] [15 22]], shape=(2, 2), dtype=int32)
print(type(b.numpy()))
# <class 'numpy.ndarray'>
You can use TF 2.0 like NumPy
What’s Gone
Session.run
tf.control_dependencies
tf.global_variables_initializer
tf.cond, tf.while_loop
tf.contrib
Specifics
What’s Gone
Session.run
tf.control_dependencies
tf.global_variables_initializer
tf.cond, tf.while_loop
tf.contrib
What’s New
Eager execution by default
tf.function
Keras as main high-level api
Specifics
tf.keras
Introduction to TensorFlow 2.0
Fast prototyping, advanced research, and production
keras.io = reference implementation
import keras
tf.keras = TensorFlow’s implementation (a superset, built-in to TF, no
need to install Keras separately)
from tensorflow import keras
Keras and tf.keras
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
For Beginners
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, 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'])
For Beginners
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, 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'])
model.fit(x_train, y_train, epochs=5)
For Beginners
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, 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'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
For Beginners
class MyModel(tf.keras.Model):
def __init__(self, num_classes=10):
super(MyModel, self).__init__(name='my_model')
self.dense_1 = layers.Dense(32, activation='relu')
self.dense_2 = layers.Dense(num_classes, activation='sigmoid')
For Experts
class MyModel(tf.keras.Model):
def __init__(self, num_classes=10):
super(MyModel, self).__init__(name='my_model')
self.dense_1 = layers.Dense(32, activation='relu')
self.dense_2 = layers.Dense(num_classes, activation='sigmoid')
def call(self, inputs):
# Define your forward pass here,
x = self.dense_1(inputs)
return self.dense_2(x)
For Experts
What’s the difference?
Symbolic (For Beginners)
Your model is a graph of layers
Any graph you compile will run
TensorFlow helps you debug by catching errors at compile time
Symbolic vs Imperative APIs
Symbolic (For Beginners)
Your model is a graph of layers
Any graph you compile will run
TensorFlow helps you debug by catching errors at compile time
Imperative (For Experts)
Your model is Python bytecode
Complete flexibility and control
Harder to debug / harder to maintain
Symbolic vs Imperative APIs
tf.function
lstm_cell = tf.keras.layers.LSTMCell(10)
def fn(input, state):
return lstm_cell(input, state)
input = tf.zeros([10, 10]); state = [tf.zeros([10, 10])] * 2
lstm_cell(input, state); fn(input, state) # warm up
# benchmark
timeit.timeit(lambda: lstm_cell(input, state), number=10) # 0.03
Let’s make this faster
lstm_cell = tf.keras.layers.LSTMCell(10)
@tf.function
def fn(input, state):
return lstm_cell(input, state)
input = tf.zeros([10, 10]); state = [tf.zeros([10, 10])] * 2
lstm_cell(input, state); fn(input, state) # warm up
# benchmark
timeit.timeit(lambda: lstm_cell(input, state), number=10) # 0.03
timeit.timeit(lambda: fn(input, state), number=10) # 0.004
Let’s make this faster
@tf.function
def f(x):
while tf.reduce_sum(x) > 1:
x = tf.tanh(x)
return x
# you never need to run this (unless curious)
print(tf.autograph.to_code(f))
AutoGraph makes this possible
def tf__f(x):
def loop_test(x_1):
with ag__.function_scope('loop_test'):
return ag__.gt(tf.reduce_sum(x_1), 1)
def loop_body(x_1):
with ag__.function_scope('loop_body'):
with ag__.utils.control_dependency_on_returns(tf.print(x_1)):
tf_1, x = ag__.utils.alias_tensors(tf, x_1)
x = tf_1.tanh(x)
return x,
x = ag__.while_stmt(loop_test, loop_body, (x,), (tf,))
return x
Generated code
tf.distribution.Strategy
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(64, input_shape=[10]),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
Going big: tf.distribute.Strategy
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(64, input_shape=[10]),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
Going big: Multi-GPU
tensorflow_datasets
# Load data
import tensorflow_datasets as tfds
dataset = tfds.load(‘cats_vs_dogs', as_supervised=True)
mnist_train, mnist_test = dataset['train'], dataset['test']
def scale(image, label):
image = tf.cast(image, tf.float32)
image /= 255
return image, label
mnist_train = mnist_train.map(scale).batch(64)
mnist_test = mnist_test.map(scale).batch(64)
TensorFlow Datasets
● audio
○ "nsynth"
● image
○ "cifar10"
○ "diabetic_retinopathy_detection"
○ "imagenet2012"
○ "mnist"
● structured
○ "titanic"
● text
○ "imdb_reviews"
○ "lm1b"
○ "squad"
● translate
○ "wmt_translate_ende"
○ "wmt_translate_enfr"
● video
○ "bair_robot_pushing_small"
○ "moving_mnist"
○ "starcraft_video"
More at tensorflow.org/datasets
Transfer Learning
Introduction to TensorFlow 2.0
Introduction to TensorFlow 2.0
import tensorflow as tf
base_model = tf.keras.applications.SequentialMobileNetV2(
input_shape=(160, 160, 3),
include_top=False,
weights=’imagenet’)
base_model.trainable = False
model = tf.keras.models.Sequential([
base_model,
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(1)
])
# Compile and fit
Transfer Learning
Image of TensorFlow Hub and serialized
Saved Model
Upgrading
Migration guides
tf.compat.v1 for backwards compatibility
tf_upgrade_v2 script
Upgrading
Introduction to TensorFlow 2.0
Getting Started
pip install tensorflow
TensorFlow 2.0
Introduction to TensorFlow 2.0
Intro to TensorFlow
for Deep Learning
Introduction to TensorFlow
for AI, ML and DL
coursera.org/learn/introduction-tensorflow udacity.com/tensorflow
New Courses
Introduction to TensorFlow 2.0
github.com/orgs/tensorflow/projects/4
Go build.
pip install tensorflow
tensorflow.org
tf.thanks!
Brad Miro - @bradmiro
tensorflow.org
Spark + AI Summit Europe - October 2019
80

More Related Content

PPTX
What is TensorFlow? | Introduction to TensorFlow | TensorFlow Tutorial For Be...
PDF
Tensorflow presentation
PDF
Introduction to TensorFlow
PDF
Introduction to Deep Learning, Keras, and TensorFlow
PPTX
Deep learning with tensorflow
PDF
Introduction To TensorFlow
PPTX
Introduction to Keras
PDF
TensorFlow and Keras: An Overview
What is TensorFlow? | Introduction to TensorFlow | TensorFlow Tutorial For Be...
Tensorflow presentation
Introduction to TensorFlow
Introduction to Deep Learning, Keras, and TensorFlow
Deep learning with tensorflow
Introduction To TensorFlow
Introduction to Keras
TensorFlow and Keras: An Overview

What's hot (20)

PDF
PyTorch Introduction
PDF
Deep learning - A Visual Introduction
PDF
PyTorch Python Tutorial | Deep Learning Using PyTorch | Image Classifier Usin...
PDF
Training Neural Networks
PPTX
Batch normalization presentation
PPTX
Introduction to Deep Learning
PPTX
What Is Deep Learning? | Introduction to Deep Learning | Deep Learning Tutori...
PPTX
Deep Learning Tutorial | Deep Learning Tutorial For Beginners | What Is Deep ...
PPTX
Pytorch
PPTX
PDF
Convolutional Neural Networks (CNN)
PPTX
Activation functions and Training Algorithms for Deep Neural network
PPTX
Overfitting & Underfitting
PPTX
Convolution Neural Network (CNN)
PPTX
Deep learning presentation
PPTX
Deep learning
PDF
An Introduction to Deep Learning
PDF
Keras Tutorial For Beginners | Creating Deep Learning Models Using Keras In P...
PDF
An introduction to Deep Learning
PyTorch Introduction
Deep learning - A Visual Introduction
PyTorch Python Tutorial | Deep Learning Using PyTorch | Image Classifier Usin...
Training Neural Networks
Batch normalization presentation
Introduction to Deep Learning
What Is Deep Learning? | Introduction to Deep Learning | Deep Learning Tutori...
Deep Learning Tutorial | Deep Learning Tutorial For Beginners | What Is Deep ...
Pytorch
Convolutional Neural Networks (CNN)
Activation functions and Training Algorithms for Deep Neural network
Overfitting & Underfitting
Convolution Neural Network (CNN)
Deep learning presentation
Deep learning
An Introduction to Deep Learning
Keras Tutorial For Beginners | Creating Deep Learning Models Using Keras In P...
An introduction to Deep Learning
Ad

Similar to Introduction to TensorFlow 2.0 (20)

PPTX
Introduction to TensorFlow 2
PDF
TensorFlow meetup: Keras - Pytorch - TensorFlow.js
PDF
Anirudh Koul. 30 Golden Rules of Deep Learning Performance
PPTX
190111 tf2 preview_jwkang_pub
PDF
Tensorflow 2.0 and Coral Edge TPU
PDF
Neural Networks from Scratch - TensorFlow 101
PPTX
Introduction to TensorFlow 2
PPTX
Introduction to TensorFlow 2 and Keras
PDF
Overview of TensorFlow For Natural Language Processing
PPTX
From Tensorflow Graph to Tensorflow Eager
PPTX
slide-keras-tf.pptx
PPTX
Demystifying-AI-Frameworks-TensorFlow-PyTorch-JAX-and-More (1).pptx
PPTX
Deep Learning and TensorFlow
PDF
Machine Learning with TensorFlow 2
PPTX
Introduction to Deep Learning and TensorFlow
PDF
Introduction To Using TensorFlow & Deep Learning
PPTX
TensorFlow in Your Browser
PPTX
H2 o berkeleydltf
PDF
TensorFlow example for AI Ukraine2016
PDF
A Tour of Tensorflow's APIs
Introduction to TensorFlow 2
TensorFlow meetup: Keras - Pytorch - TensorFlow.js
Anirudh Koul. 30 Golden Rules of Deep Learning Performance
190111 tf2 preview_jwkang_pub
Tensorflow 2.0 and Coral Edge TPU
Neural Networks from Scratch - TensorFlow 101
Introduction to TensorFlow 2
Introduction to TensorFlow 2 and Keras
Overview of TensorFlow For Natural Language Processing
From Tensorflow Graph to Tensorflow Eager
slide-keras-tf.pptx
Demystifying-AI-Frameworks-TensorFlow-PyTorch-JAX-and-More (1).pptx
Deep Learning and TensorFlow
Machine Learning with TensorFlow 2
Introduction to Deep Learning and TensorFlow
Introduction To Using TensorFlow & Deep Learning
TensorFlow in Your Browser
H2 o berkeleydltf
TensorFlow example for AI Ukraine2016
A Tour of Tensorflow's APIs
Ad

More from Databricks (20)

PPTX
DW Migration Webinar-March 2022.pptx
PPTX
Data Lakehouse Symposium | Day 1 | Part 1
PPT
Data Lakehouse Symposium | Day 1 | Part 2
PPTX
Data Lakehouse Symposium | Day 2
PPTX
Data Lakehouse Symposium | Day 4
PDF
5 Critical Steps to Clean Your Data Swamp When Migrating Off of Hadoop
PDF
Democratizing Data Quality Through a Centralized Platform
PDF
Learn to Use Databricks for Data Science
PDF
Why APM Is Not the Same As ML Monitoring
PDF
The Function, the Context, and the Data—Enabling ML Ops at Stitch Fix
PDF
Stage Level Scheduling Improving Big Data and AI Integration
PDF
Simplify Data Conversion from Spark to TensorFlow and PyTorch
PDF
Scaling your Data Pipelines with Apache Spark on Kubernetes
PDF
Scaling and Unifying SciKit Learn and Apache Spark Pipelines
PDF
Sawtooth Windows for Feature Aggregations
PDF
Redis + Apache Spark = Swiss Army Knife Meets Kitchen Sink
PDF
Re-imagine Data Monitoring with whylogs and Spark
PDF
Raven: End-to-end Optimization of ML Prediction Queries
PDF
Processing Large Datasets for ADAS Applications using Apache Spark
PDF
Massive Data Processing in Adobe Using Delta Lake
DW Migration Webinar-March 2022.pptx
Data Lakehouse Symposium | Day 1 | Part 1
Data Lakehouse Symposium | Day 1 | Part 2
Data Lakehouse Symposium | Day 2
Data Lakehouse Symposium | Day 4
5 Critical Steps to Clean Your Data Swamp When Migrating Off of Hadoop
Democratizing Data Quality Through a Centralized Platform
Learn to Use Databricks for Data Science
Why APM Is Not the Same As ML Monitoring
The Function, the Context, and the Data—Enabling ML Ops at Stitch Fix
Stage Level Scheduling Improving Big Data and AI Integration
Simplify Data Conversion from Spark to TensorFlow and PyTorch
Scaling your Data Pipelines with Apache Spark on Kubernetes
Scaling and Unifying SciKit Learn and Apache Spark Pipelines
Sawtooth Windows for Feature Aggregations
Redis + Apache Spark = Swiss Army Knife Meets Kitchen Sink
Re-imagine Data Monitoring with whylogs and Spark
Raven: End-to-end Optimization of ML Prediction Queries
Processing Large Datasets for ADAS Applications using Apache Spark
Massive Data Processing in Adobe Using Delta Lake

Recently uploaded (20)

PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
1_Introduction to advance data techniques.pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PDF
.pdf is not working space design for the following data for the following dat...
PDF
Business Analytics and business intelligence.pdf
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
Database Infoormation System (DBIS).pptx
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPT
ISS -ESG Data flows What is ESG and HowHow
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
1_Introduction to advance data techniques.pptx
Miokarditis (Inflamasi pada Otot Jantung)
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
.pdf is not working space design for the following data for the following dat...
Business Analytics and business intelligence.pdf
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
climate analysis of Dhaka ,Banglades.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
Database Infoormation System (DBIS).pptx
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
IB Computer Science - Internal Assessment.pptx
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Reliability_Chapter_ presentation 1221.5784
Acceptance and paychological effects of mandatory extra coach I classes.pptx
ISS -ESG Data flows What is ESG and HowHow

Introduction to TensorFlow 2.0