SlideShare a Scribd company logo
1 of 22
TF GRAPH TO TF EAGER
Guy Hadash
IBM Research AI
WHY MOVE TO EAGER?
Eager Execution changes the core idea of TensorFlow.
Instead of describing the execution graph in Python, compiling it and then running it,
the framework is now imperative. This means it creates the graph on the fly and runs
operations immediately.
This brings some significant improvements:
• An intuitive interface
• Fast development iterations
• Easier debugging
• Natural control flow
WHY MOVE TO EAGER?
The main ability which we gain now is:
This will simply gives us the value of the tensor. As we said, this allow us much easier
debugging, and we can also control the model flow based on the tensors values.
There is no need for session anymore, and we can stop worry about graph
dependencies and etc.
tensor.numpy()
SESSION PROGRAM
• Data pipeline
• Classifier
• go over the necessary stuff for building and training custom model
• Autoencoder
• building custom layer
• Text classification
• controlling model flow with python, and working with sequence data
All the code inside colab: https://goo.gl/q3rHNT
BEST PRACTICES
Moving from graph mode to eager mode also makes it much more natural to now
work in OOPier way.
We will inherit from tf.keras.Model and tf.keras.layers.Layer.
We will use tf.Data for easy and fast data pipeline.
DATA PIPELINE
tf.Data is the current best practice for handling the data pipeline.
This will allow us easy and fast data pipeline. From my experience the most common
used initializers are:
from_tensor_slices – retrieve one sample at a time from given tensor. Best for
simple cases training.
from_tensors – returns the full dataset at once. Helpful for testing.
from_generator – a more flexible way, useful in more complicated use cases.
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train))
TIP: All dataset initializers works naturally with tuples and dictionaries (also nested)
DATA PIPELINE
Now that we have Dataset object we can quickly build pipeline:
By creating this pipeline, we allow TF to utilize the CPU in parallel to our training and
prepare the next batch in the GPU waiting for the next optimization step.
train_ds = train_ds.map(_normalize, num_parallel_calls=4)
train_ds = train_ds.apply(tf.contrib.data.shuffle_and_repeat(buffer_size, num_epochs))
# train_ds = train_ds.shuffle(buffer_size).repeat(num_epochs)
train_ds = train_ds .batch(batch_size).apply(tf.contrib.data.prefetch_to_device("/gpu:0"))
TIP: Even when running in Eager mode the pipeline runs as a graph
TIP: Buffer size should be big enough for effective shuffling
TIP: prefetch_to_device must be last operation
THE BASIC BUILDING BLOCKS
tf.keras.layers.Layer
Layer - a group of variables tied together.
tf.keras.Network
Network – a group of layers tied together
tf.keras.Model
Model – network with all the training utils
Each of them is callable.
BUILDING MODEL
When building a model in eager execution, we derive from tf.keras.Model.
This gives us a few important properties which we will use:
• model.variables - automatically returns all model’s variables.
• It does it by taking all variables from layers (inherit from tf.layers.Layer), and models
(inherit from tf.keras.Network)
• model.save_weights – allow us to save (and load) the model weights easily
• There is also an option to save the model itself and not only the weights. However, it
doesn’t work well when building custom models.
MNIST MODEL
We first initialize the layers we will use,
but not describing the model flow
(different from graph mode).
You can notice the real variables sized
are unknown, so it can’t be initialized
yet.
Here we override the call function, this
will be called each time our model is
activated.
class SimpleClassifier(tf.keras.Model):
def __init__(self):
super().__init__()
self.fc1 = tf.keras.layers.Dense(100, activation=tf.nn.relu)
self.fc2 = tf.keras.layers.Dense(50, activation=tf.nn.relu)
self.fc3 = tf.keras.layers.Dense(FLAGS.classes_amount)
self.optimizer = tf.train.AdamOptimizer()
def call(self, inputs, training=None, **kwargs):
x = tf.reshape(inputs, [inputs.shape[0], -1])
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
TIP: if you want to define list of layers use tf.contrib.checkpoint.List, for all APIs to work.
RUNNING THE MODEL
When we want to run the model, we treat it as runnable.
This will run the call function we wrote in the previous slide, with some extra logic.
model = SimpleClassifier()
results = model(inputs)
OPTIMIZATION PROCESS
Now in Eager mode, when we optimize, we need to clarify the model which variables
it should calculate gradients by.
This is what tf.GradientTape() as tape context is for. All intermediate results we need
for the gradients calculations of variables are saved. We can also use watch
command to tell the framework watch for any arbitrary tensor results.
We can later use tape.gradient(loss, variables) to get the gradients of the loss with
respect to each of the variables. This will automatically reset the tape and free the
memory.
TIP: If you need to call tape.gradient more the once use tf.GradientTape(persistent=True) - and use del later
MNIST MODEL
We define the loss function for the
model – nothing changed here.
No special reason to be in the model
class.
Here is the optimization process. As
described we run the forward in the
tape context, and then we calculate the
gradients and apply them.
Notice we use the model instance as
runnable, it will use the call function we
override before.
@staticmethod
def loss(logits, labels):
return tf.losses.sparse_softmax_cross_entropy(labels, logits)
def optimize(self, inputs, labels):
with tf.GradientTape() as tape:
logits = self(inputs)
batch_loss = self.loss(logits, labels)
gradients = tape.gradient(batch_loss, self.variables)
self.optimizer.apply_gradients(zip(gradients, self.variables))
return batch_loss
RUNNING TOGETHER
Now we just need to iterate over the data and optimize for each batch. This is done
very naturally in eager mode:
with tf.device('/gpu:0'):
model = SimpleClassifier()
for step, (batch_x, batch_y) in enumerate(train_ds):
loss = model.optimize(batch_x, batch_y)
if step % FLAGS.print_freq == 0:
print("Step {}: loss: {}".format(step, loss))
if step % FLAGS.validate_freq == 0:
accuracy = model.accuracy(x_test, y_test)
print("Step {}: test accuracy: {}".format(step, accuracy))
TIP: since tf1.8 there is automatically placement, however currently (tf1.9) it is still better to state the
device, performance wise
BUILDING CUSTOM LAYERS
We will now build MNIST autoencoder, where the reconstruct is the transpose of the
encoder layers.
In graph mode, we will have the variable w, and we can just use it again. This method
is also possible in eager mode, but since we try to work in a more OOP way, we will
construct a custom layer.
BUILDING CUSTOM LAYERS
To create custom layer, we need to implement:
• __Init__ - constructor, preparing everything we can without the input shape
• build – function which gets called in the first time the layer is running, here we will
know the input shape, and we can initialize all weights of the layer.
• call – the layer logic, will be called each time the layer runs on input.
BUILDING CUSTOM LAYERS
In init function, we define all the
information we need and variables that
possible.
This is where we define the layer logic.
You can see that the if statement
controls the flow and evaluates each
batch separately.
class InvDense(tf.keras.layers.Layer):
def __init__(self, dense_layer, activation=None, **kwargs):
super().__init__(**kwargs)
self.dense_layer = dense_layer
self.activation_func = activation
def build(self, input_shape):
out_hiddens = self.dense_layer.kernel.get_shape()[-2]
self.bias = self.add_variable("b", [out_hiddens],
initializer=tf.zeros_initializer)
super().build(input_shape)
def call(self, inputs, **kwargs):
x = tf.matmul(inputs, tf.transpose(self.dense_layer.kernel))
x += self.bias
if self.activation_func:
x = self.activation_func(x)
return x
BUILDING CUSTOM LAYERS
Now we initialize and use the custom
layers the same as any other
keras.layers.*.
class AutoEncoder(tf.keras.Model):
def __init__(self):
super().__init__()
self.fc1 = tf.keras.layers.Dense(100, activation=tf.nn.relu)
self.fc2 = tf.keras.layers.Dense(50, activation=tf.nn.relu)
self.fc2_t = InvDense(self.fc2, activation=tf.nn.relu)
self.fc1_t = InvDense(self.fc1)
self.optimizer = tf.train.AdamOptimizer()
def call(self, inputs, training=None, **kwargs):
x = tf.reshape(inputs, [inputs.shape[0], -1])
x = self.fc1(x)
x = self.fc2(x)
x = self.fc2_t(x)
x = self.fc1_t(x)
x = tf.reshape(x, inputs.shape)
return x
TEXT CLASSIFICATION
Now we do IMDb sentiment classification. We will start by building a suitable data
pipeline:
Notice we use from_generator since each example has different length
def _add_length(x, y):
x = x[:FLAGS.max_len]
x_dict = {"seq": x, "seq_len": tf.size(x)}
return x_dict, y
ds = tf.data.Dataset.from_generator(lambda: zip(x_train, y_train), output_types=(tf.int32, tf.int32),
output_shapes=([None], []))
ds = ds.map(_add_length, num_parallel_calls=4)
ds = ds.apply(tf.contrib.data.shuffle_and_repeat(len(x_train), FLAGS.num_epochs))
ds = ds.padded_batch(FLAGS.batch_size, padded_shapes=({"seq": [None], "seq_len": []}, []))
ds = ds.apply(tf.contrib.data.prefetch_to_device("/gpu:0"))
TIP: from_generator accept any callable object which returns __iter__ supporting object
CONTROLLING FLOW
One of the significant advantages that eager brings us is the ability to control our flow
using python and tensors values. No need for tf.while_loop and tf.cond no more!
TEXT CLASSIFICATION
Notice that we now go
through the words using
python for loop.
This method won’t work
in graph mode since each
batch the shape in axis=1
is different.
def call(self, inputs, training=None, **kwargs):
seqs = self.word_emb(inputs["seq"])
state = [tf.zeros([seqs.shape[0], self.rnn_cell.state_size])]
seqs = tf.unstack(seqs, num=int(seqs.shape[1]), axis=1)
hiddens = []
for word in seqs:
h, state = self.rnn_cell(word, state)
hiddens.append(h)
hiddens = tf.stack(hiddens, axis=1)
last_hiddens = self.get_last_relevant(hiddens, inputs["seq_len"])
x = self.fc1(last_hiddens)
x = self.fc2(x)
return x
ADDING REGULARIZATION
In order to add the common regularizations, we can use tf.contrib.layers.*_regularizer
as we did in tensorflow.
Now, in order to get the regularization loss, instead of using
tf.GraphKeys.REGULARIZATION_LOSSES we will use keras.Model losses
l2_reg = tf.contrib.layers.l2_regularizer(FLAGS.reg_factor)
self.fc1 = tf.keras.layers.Dense(100, kernel_regularizer=l2_reg, bias_regularizer=l2_reg)
loss += tf.reduce_sum(self.losses)

More Related Content

What's hot

Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialJin-Hwa Kim
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in HaskellIntroduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskellnebuta
 
Intel® Xeon® Phi Coprocessor High Performance Programming
Intel® Xeon® Phi Coprocessor High Performance ProgrammingIntel® Xeon® Phi Coprocessor High Performance Programming
Intel® Xeon® Phi Coprocessor High Performance ProgrammingBrian Gesiak
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlowOswald Campesato
 
[2A3]Big Data Launching Episodes
[2A3]Big Data Launching Episodes[2A3]Big Data Launching Episodes
[2A3]Big Data Launching EpisodesNAVER D2
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functionsankita44
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Sameer Rathoud
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISPDevnology
 
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from..."PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...Edge AI and Vision Alliance
 
Collection frame work
Collection  frame workCollection  frame work
Collection frame workRahul Kolluri
 
Node.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizationsNode.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizationsDawid Rusnak
 
TensorFlow 深度學習快速上手班--電腦視覺應用
TensorFlow 深度學習快速上手班--電腦視覺應用TensorFlow 深度學習快速上手班--電腦視覺應用
TensorFlow 深度學習快速上手班--電腦視覺應用Mark Chang
 
Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Jay Coskey
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data StructureChan Shik Lim
 

What's hot (20)

Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorial
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in HaskellIntroduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskell
 
Intel® Xeon® Phi Coprocessor High Performance Programming
Intel® Xeon® Phi Coprocessor High Performance ProgrammingIntel® Xeon® Phi Coprocessor High Performance Programming
Intel® Xeon® Phi Coprocessor High Performance Programming
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlow
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
[2A3]Big Data Launching Episodes
[2A3]Big Data Launching Episodes[2A3]Big Data Launching Episodes
[2A3]Big Data Launching Episodes
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
 
Scmad Chapter07
Scmad Chapter07Scmad Chapter07
Scmad Chapter07
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
Multirate sim
Multirate simMultirate sim
Multirate sim
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from..."PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
 
Collection frame work
Collection  frame workCollection  frame work
Collection frame work
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Memory management in c++
Memory management in c++Memory management in c++
Memory management in c++
 
Node.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizationsNode.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizations
 
TensorFlow 深度學習快速上手班--電腦視覺應用
TensorFlow 深度學習快速上手班--電腦視覺應用TensorFlow 深度學習快速上手班--電腦視覺應用
TensorFlow 深度學習快速上手班--電腦視覺應用
 
Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
 

Similar to TF GRAPH TO TF EAGER: Moving from TensorFlow Graph Mode to Eager Execution

TensorFlow Dev Summit 2018 Extended: TensorFlow Eager Execution
TensorFlow Dev Summit 2018 Extended: TensorFlow Eager ExecutionTensorFlow Dev Summit 2018 Extended: TensorFlow Eager Execution
TensorFlow Dev Summit 2018 Extended: TensorFlow Eager ExecutionTaegyun Jeon
 
Bring your neural networks to the browser with TF.js - Simone Scardapane
Bring your neural networks to the browser with TF.js - Simone ScardapaneBring your neural networks to the browser with TF.js - Simone Scardapane
Bring your neural networks to the browser with TF.js - Simone ScardapaneMeetupDataScienceRoma
 
Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...
Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...
Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...Codemotion
 
Anirudh Koul. 30 Golden Rules of Deep Learning Performance
Anirudh Koul. 30 Golden Rules of Deep Learning PerformanceAnirudh Koul. 30 Golden Rules of Deep Learning Performance
Anirudh Koul. 30 Golden Rules of Deep Learning PerformanceLviv Startup Club
 
Need help filling out the missing sections of this code- the sections.docx
Need help filling out the missing sections of this code- the sections.docxNeed help filling out the missing sections of this code- the sections.docx
Need help filling out the missing sections of this code- the sections.docxlauracallander
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3Max Kleiner
 
Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117Ganesan Narayanasamy
 
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Raffi Khatchadourian
 
slide-keras-tf.pptx
slide-keras-tf.pptxslide-keras-tf.pptx
slide-keras-tf.pptxRithikRaj25
 
A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...
A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...
A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...Databricks
 
TensorFlow example for AI Ukraine2016
TensorFlow example  for AI Ukraine2016TensorFlow example  for AI Ukraine2016
TensorFlow example for AI Ukraine2016Andrii Babii
 
TensorFlow for IITians
TensorFlow for IITiansTensorFlow for IITians
TensorFlow for IITiansAshish Bansal
 
Language translation with Deep Learning (RNN) with TensorFlow
Language translation with Deep Learning (RNN) with TensorFlowLanguage translation with Deep Learning (RNN) with TensorFlow
Language translation with Deep Learning (RNN) with TensorFlowS N
 
A Tour of Tensorflow's APIs
A Tour of Tensorflow's APIsA Tour of Tensorflow's APIs
A Tour of Tensorflow's APIsDean Wyatte
 
Viktor Tsykunov: Azure Machine Learning Service
Viktor Tsykunov: Azure Machine Learning ServiceViktor Tsykunov: Azure Machine Learning Service
Viktor Tsykunov: Azure Machine Learning ServiceLviv Startup Club
 
190111 tf2 preview_jwkang_pub
190111 tf2 preview_jwkang_pub190111 tf2 preview_jwkang_pub
190111 tf2 preview_jwkang_pubJaewook. Kang
 
Font classification with 5 deep learning models using tensor flow
Font classification with 5 deep learning models using tensor flowFont classification with 5 deep learning models using tensor flow
Font classification with 5 deep learning models using tensor flowDevatanu Banerjee
 

Similar to TF GRAPH TO TF EAGER: Moving from TensorFlow Graph Mode to Eager Execution (20)

TensorFlow Dev Summit 2018 Extended: TensorFlow Eager Execution
TensorFlow Dev Summit 2018 Extended: TensorFlow Eager ExecutionTensorFlow Dev Summit 2018 Extended: TensorFlow Eager Execution
TensorFlow Dev Summit 2018 Extended: TensorFlow Eager Execution
 
Keras and TensorFlow
Keras and TensorFlowKeras and TensorFlow
Keras and TensorFlow
 
Bring your neural networks to the browser with TF.js - Simone Scardapane
Bring your neural networks to the browser with TF.js - Simone ScardapaneBring your neural networks to the browser with TF.js - Simone Scardapane
Bring your neural networks to the browser with TF.js - Simone Scardapane
 
Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...
Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...
Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...
 
Anirudh Koul. 30 Golden Rules of Deep Learning Performance
Anirudh Koul. 30 Golden Rules of Deep Learning PerformanceAnirudh Koul. 30 Golden Rules of Deep Learning Performance
Anirudh Koul. 30 Golden Rules of Deep Learning Performance
 
Need help filling out the missing sections of this code- the sections.docx
Need help filling out the missing sections of this code- the sections.docxNeed help filling out the missing sections of this code- the sections.docx
Need help filling out the missing sections of this code- the sections.docx
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3
 
Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117
 
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
 
slide-keras-tf.pptx
slide-keras-tf.pptxslide-keras-tf.pptx
slide-keras-tf.pptx
 
A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...
A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...
A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...
 
TensorFlow example for AI Ukraine2016
TensorFlow example  for AI Ukraine2016TensorFlow example  for AI Ukraine2016
TensorFlow example for AI Ukraine2016
 
TensorFlow for IITians
TensorFlow for IITiansTensorFlow for IITians
TensorFlow for IITians
 
Deep Learning in theano
Deep Learning in theanoDeep Learning in theano
Deep Learning in theano
 
Language translation with Deep Learning (RNN) with TensorFlow
Language translation with Deep Learning (RNN) with TensorFlowLanguage translation with Deep Learning (RNN) with TensorFlow
Language translation with Deep Learning (RNN) with TensorFlow
 
Deep Learning for Computer Vision: Software Frameworks (UPC 2016)
Deep Learning for Computer Vision: Software Frameworks (UPC 2016)Deep Learning for Computer Vision: Software Frameworks (UPC 2016)
Deep Learning for Computer Vision: Software Frameworks (UPC 2016)
 
A Tour of Tensorflow's APIs
A Tour of Tensorflow's APIsA Tour of Tensorflow's APIs
A Tour of Tensorflow's APIs
 
Viktor Tsykunov: Azure Machine Learning Service
Viktor Tsykunov: Azure Machine Learning ServiceViktor Tsykunov: Azure Machine Learning Service
Viktor Tsykunov: Azure Machine Learning Service
 
190111 tf2 preview_jwkang_pub
190111 tf2 preview_jwkang_pub190111 tf2 preview_jwkang_pub
190111 tf2 preview_jwkang_pub
 
Font classification with 5 deep learning models using tensor flow
Font classification with 5 deep learning models using tensor flowFont classification with 5 deep learning models using tensor flow
Font classification with 5 deep learning models using tensor flow
 

Recently uploaded

Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一ffjhghh
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiSuhani Kapoor
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 

Recently uploaded (20)

Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Decoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionDecoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in Action
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 

TF GRAPH TO TF EAGER: Moving from TensorFlow Graph Mode to Eager Execution

  • 1. TF GRAPH TO TF EAGER Guy Hadash IBM Research AI
  • 2. WHY MOVE TO EAGER? Eager Execution changes the core idea of TensorFlow. Instead of describing the execution graph in Python, compiling it and then running it, the framework is now imperative. This means it creates the graph on the fly and runs operations immediately. This brings some significant improvements: • An intuitive interface • Fast development iterations • Easier debugging • Natural control flow
  • 3. WHY MOVE TO EAGER? The main ability which we gain now is: This will simply gives us the value of the tensor. As we said, this allow us much easier debugging, and we can also control the model flow based on the tensors values. There is no need for session anymore, and we can stop worry about graph dependencies and etc. tensor.numpy()
  • 4. SESSION PROGRAM • Data pipeline • Classifier • go over the necessary stuff for building and training custom model • Autoencoder • building custom layer • Text classification • controlling model flow with python, and working with sequence data All the code inside colab: https://goo.gl/q3rHNT
  • 5. BEST PRACTICES Moving from graph mode to eager mode also makes it much more natural to now work in OOPier way. We will inherit from tf.keras.Model and tf.keras.layers.Layer. We will use tf.Data for easy and fast data pipeline.
  • 6. DATA PIPELINE tf.Data is the current best practice for handling the data pipeline. This will allow us easy and fast data pipeline. From my experience the most common used initializers are: from_tensor_slices – retrieve one sample at a time from given tensor. Best for simple cases training. from_tensors – returns the full dataset at once. Helpful for testing. from_generator – a more flexible way, useful in more complicated use cases. (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)) TIP: All dataset initializers works naturally with tuples and dictionaries (also nested)
  • 7. DATA PIPELINE Now that we have Dataset object we can quickly build pipeline: By creating this pipeline, we allow TF to utilize the CPU in parallel to our training and prepare the next batch in the GPU waiting for the next optimization step. train_ds = train_ds.map(_normalize, num_parallel_calls=4) train_ds = train_ds.apply(tf.contrib.data.shuffle_and_repeat(buffer_size, num_epochs)) # train_ds = train_ds.shuffle(buffer_size).repeat(num_epochs) train_ds = train_ds .batch(batch_size).apply(tf.contrib.data.prefetch_to_device("/gpu:0")) TIP: Even when running in Eager mode the pipeline runs as a graph TIP: Buffer size should be big enough for effective shuffling TIP: prefetch_to_device must be last operation
  • 8. THE BASIC BUILDING BLOCKS tf.keras.layers.Layer Layer - a group of variables tied together. tf.keras.Network Network – a group of layers tied together tf.keras.Model Model – network with all the training utils Each of them is callable.
  • 9. BUILDING MODEL When building a model in eager execution, we derive from tf.keras.Model. This gives us a few important properties which we will use: • model.variables - automatically returns all model’s variables. • It does it by taking all variables from layers (inherit from tf.layers.Layer), and models (inherit from tf.keras.Network) • model.save_weights – allow us to save (and load) the model weights easily • There is also an option to save the model itself and not only the weights. However, it doesn’t work well when building custom models.
  • 10. MNIST MODEL We first initialize the layers we will use, but not describing the model flow (different from graph mode). You can notice the real variables sized are unknown, so it can’t be initialized yet. Here we override the call function, this will be called each time our model is activated. class SimpleClassifier(tf.keras.Model): def __init__(self): super().__init__() self.fc1 = tf.keras.layers.Dense(100, activation=tf.nn.relu) self.fc2 = tf.keras.layers.Dense(50, activation=tf.nn.relu) self.fc3 = tf.keras.layers.Dense(FLAGS.classes_amount) self.optimizer = tf.train.AdamOptimizer() def call(self, inputs, training=None, **kwargs): x = tf.reshape(inputs, [inputs.shape[0], -1]) x = self.fc1(x) x = self.fc2(x) x = self.fc3(x) return x TIP: if you want to define list of layers use tf.contrib.checkpoint.List, for all APIs to work.
  • 11. RUNNING THE MODEL When we want to run the model, we treat it as runnable. This will run the call function we wrote in the previous slide, with some extra logic. model = SimpleClassifier() results = model(inputs)
  • 12. OPTIMIZATION PROCESS Now in Eager mode, when we optimize, we need to clarify the model which variables it should calculate gradients by. This is what tf.GradientTape() as tape context is for. All intermediate results we need for the gradients calculations of variables are saved. We can also use watch command to tell the framework watch for any arbitrary tensor results. We can later use tape.gradient(loss, variables) to get the gradients of the loss with respect to each of the variables. This will automatically reset the tape and free the memory. TIP: If you need to call tape.gradient more the once use tf.GradientTape(persistent=True) - and use del later
  • 13. MNIST MODEL We define the loss function for the model – nothing changed here. No special reason to be in the model class. Here is the optimization process. As described we run the forward in the tape context, and then we calculate the gradients and apply them. Notice we use the model instance as runnable, it will use the call function we override before. @staticmethod def loss(logits, labels): return tf.losses.sparse_softmax_cross_entropy(labels, logits) def optimize(self, inputs, labels): with tf.GradientTape() as tape: logits = self(inputs) batch_loss = self.loss(logits, labels) gradients = tape.gradient(batch_loss, self.variables) self.optimizer.apply_gradients(zip(gradients, self.variables)) return batch_loss
  • 14. RUNNING TOGETHER Now we just need to iterate over the data and optimize for each batch. This is done very naturally in eager mode: with tf.device('/gpu:0'): model = SimpleClassifier() for step, (batch_x, batch_y) in enumerate(train_ds): loss = model.optimize(batch_x, batch_y) if step % FLAGS.print_freq == 0: print("Step {}: loss: {}".format(step, loss)) if step % FLAGS.validate_freq == 0: accuracy = model.accuracy(x_test, y_test) print("Step {}: test accuracy: {}".format(step, accuracy)) TIP: since tf1.8 there is automatically placement, however currently (tf1.9) it is still better to state the device, performance wise
  • 15. BUILDING CUSTOM LAYERS We will now build MNIST autoencoder, where the reconstruct is the transpose of the encoder layers. In graph mode, we will have the variable w, and we can just use it again. This method is also possible in eager mode, but since we try to work in a more OOP way, we will construct a custom layer.
  • 16. BUILDING CUSTOM LAYERS To create custom layer, we need to implement: • __Init__ - constructor, preparing everything we can without the input shape • build – function which gets called in the first time the layer is running, here we will know the input shape, and we can initialize all weights of the layer. • call – the layer logic, will be called each time the layer runs on input.
  • 17. BUILDING CUSTOM LAYERS In init function, we define all the information we need and variables that possible. This is where we define the layer logic. You can see that the if statement controls the flow and evaluates each batch separately. class InvDense(tf.keras.layers.Layer): def __init__(self, dense_layer, activation=None, **kwargs): super().__init__(**kwargs) self.dense_layer = dense_layer self.activation_func = activation def build(self, input_shape): out_hiddens = self.dense_layer.kernel.get_shape()[-2] self.bias = self.add_variable("b", [out_hiddens], initializer=tf.zeros_initializer) super().build(input_shape) def call(self, inputs, **kwargs): x = tf.matmul(inputs, tf.transpose(self.dense_layer.kernel)) x += self.bias if self.activation_func: x = self.activation_func(x) return x
  • 18. BUILDING CUSTOM LAYERS Now we initialize and use the custom layers the same as any other keras.layers.*. class AutoEncoder(tf.keras.Model): def __init__(self): super().__init__() self.fc1 = tf.keras.layers.Dense(100, activation=tf.nn.relu) self.fc2 = tf.keras.layers.Dense(50, activation=tf.nn.relu) self.fc2_t = InvDense(self.fc2, activation=tf.nn.relu) self.fc1_t = InvDense(self.fc1) self.optimizer = tf.train.AdamOptimizer() def call(self, inputs, training=None, **kwargs): x = tf.reshape(inputs, [inputs.shape[0], -1]) x = self.fc1(x) x = self.fc2(x) x = self.fc2_t(x) x = self.fc1_t(x) x = tf.reshape(x, inputs.shape) return x
  • 19. TEXT CLASSIFICATION Now we do IMDb sentiment classification. We will start by building a suitable data pipeline: Notice we use from_generator since each example has different length def _add_length(x, y): x = x[:FLAGS.max_len] x_dict = {"seq": x, "seq_len": tf.size(x)} return x_dict, y ds = tf.data.Dataset.from_generator(lambda: zip(x_train, y_train), output_types=(tf.int32, tf.int32), output_shapes=([None], [])) ds = ds.map(_add_length, num_parallel_calls=4) ds = ds.apply(tf.contrib.data.shuffle_and_repeat(len(x_train), FLAGS.num_epochs)) ds = ds.padded_batch(FLAGS.batch_size, padded_shapes=({"seq": [None], "seq_len": []}, [])) ds = ds.apply(tf.contrib.data.prefetch_to_device("/gpu:0")) TIP: from_generator accept any callable object which returns __iter__ supporting object
  • 20. CONTROLLING FLOW One of the significant advantages that eager brings us is the ability to control our flow using python and tensors values. No need for tf.while_loop and tf.cond no more!
  • 21. TEXT CLASSIFICATION Notice that we now go through the words using python for loop. This method won’t work in graph mode since each batch the shape in axis=1 is different. def call(self, inputs, training=None, **kwargs): seqs = self.word_emb(inputs["seq"]) state = [tf.zeros([seqs.shape[0], self.rnn_cell.state_size])] seqs = tf.unstack(seqs, num=int(seqs.shape[1]), axis=1) hiddens = [] for word in seqs: h, state = self.rnn_cell(word, state) hiddens.append(h) hiddens = tf.stack(hiddens, axis=1) last_hiddens = self.get_last_relevant(hiddens, inputs["seq_len"]) x = self.fc1(last_hiddens) x = self.fc2(x) return x
  • 22. ADDING REGULARIZATION In order to add the common regularizations, we can use tf.contrib.layers.*_regularizer as we did in tensorflow. Now, in order to get the regularization loss, instead of using tf.GraphKeys.REGULARIZATION_LOSSES we will use keras.Model losses l2_reg = tf.contrib.layers.l2_regularizer(FLAGS.reg_factor) self.fc1 = tf.keras.layers.Dense(100, kernel_regularizer=l2_reg, bias_regularizer=l2_reg) loss += tf.reduce_sum(self.losses)

Editor's Notes

  1. If possible, set buffer_size to train set size