Tensor Flow 101
Tensor Flow 101
CS 20SI:
TensorFlow for Deep Learning Research
Lecture 1
1/13/2017
1
2
Agenda
Welcome
Overview of TensorFlow
3
Instructor
Chip Huyen
4
You
5
Whats TensorFlow?
Open source software library for numerical computation using data flow
graphs
Originally developed by Google Brain Team to conduct machine learning and
deep neural networks research
General enough to be applicable in a wide variety of other domains as well
TensorFlow provides an extensive suite of functions and classes that allow users to
build various models from scratch.
6
Launched Nov 2015
7
TF is not the only deep learning library
From students signed up for this class
8
Why TensorFlow?
9
Why TensorFlow?
Python API
Portability: deploy computation to one or more CPUs or GPUs in a desktop,
server, or mobile device with a single API
Flexibility: from Raspberry Pi, Android, Windows, iOS, Linux to server farms
Visualization (TensorBoard is da bomb)
Checkpoints (for managing experiments)
Auto-differentiation autodiff (no more taking derivatives by hand. Yay)
Large community (> 10,000 commits and > 3000 TF-related repos in 1 year)
Awesome projects already using TensorFlow
10
Companies using Tensorflow
Google
OpenAI
DeepMind
Snapchat
Uber
Airbus
eBay
Dropbox
A bunch of startups
11
Some cool projects using
TensorFlow
12
Neural Style Translation
Image Style Transfer Using Convolutional Neural Networks by Leon A. Gatys et al. (2016) 13
Tensorflow adaptation by Cameroon Smith (cysmith@github)
Generative Handwriting
Generative Handwriting using LSTM Mixture Density Network with TensorFlow by hardmaru@GitHub (2016) 14
WaveNet: Text to Speech
It takes several hours to synthesize 1 second!
Wavenet: A generative model for raw audio by Aaron van den Oord et al. (2016) 15
I hope that this class will
give you the tool to build
cool projects like those
16
Goals
Understand TFs computation graph approach
Explore TFs built-in functions
Learn how to build and structure models best suited for a deep learning
project.
17
Introduction
18
Logistics
Piazza
Emails:
(cs20si-win1617-staff/cs20si-win1617-students/cs20si-win1617-guests)
Assignments (3)
Participation is a big chunk of grades
A lot of you are ahead of me in your academic career so I probably need more
of your help than you do mine . Feedback is greatly appreciated!
19
Books
TensorFlow for Machine Intelligence (TFFMI)
Hands-On Machine Learning with Scikit-Learn and TensorFlow. Chapter 9:
Up and running with TensorFlow
Fundamentals of Deep Learning. Chapter 3: Implementing Neural Networks
in TensorFlow (FODL)
20
Getting Started
21
import tensorflow as tf
22
Simplified TensorFlow?
1. TF Learn (tf.contrib.learn): simplified interface that helps users
transition from the the world of one-liner such as scikit-learn
2. TF Slim (tf.contrib.slim): lightweight library for defining, training and
evaluating complex models in TensorFlow.
3. High level API: Keras, TFLearn, Pretty Tensor
23
But we dont need baby TensorFlow ...
Off-the-shelf models are not the main purpose of TensorFlow.
TensorFlow provides an extensive suite of functions and classes that allow users to
define models from scratch.
24
Graphs and Sessions
25
Data Flow Graphs
Graph by TFFMI 26
Data Flow Graphs
Graph by TFFMI 27
Whats a tensor?
28
Whats a tensor?
An n-dimensional array
and so on
29
Data Flow Graphs
Visualized by TensorBoard
import tensorflow as tf
a = tf.add(3, 5)
Why x, y?
30
Data Flow Graphs
Interpreted?
import tensorflow as tf
a = tf.add(3, 5) 3
5 a
31
Data Flow Graphs
Interpreted?
import tensorflow as tf
a = tf.add(3, 5) 3
5 a
32
Data Flow Graphs
import tensorflow as tf
a = tf.add(3, 5) 3
5 a
print a
(Not 8)
33
How to get the value of a?
34
How to get the value of a?
a = tf.add(3, 5)
sess = tf.Session()
print sess.run(a)
sess.close()
The session will look at the graph, trying to think: hmm, how can I get the value of a,
then it computes all the nodes that leads to a. 35
How to get the value of a?
a = tf.add(3, 5)
sess = tf.Session() 8
print sess.run(a) >> 8
sess.close()
The session will look at the graph, trying to think: hmm, how can I get the value of a,
then it computes all the nodes that leads to a. 36
How to get the value of a?
a = tf.add(3, 5)
sess = tf.Session() 8
with tf.Session() as sess:
print sess.run(a)
sess.close()
37
tf.Session()
38
More graphs
Visualized by TensorBoard
x = 2
y = 3
op1 = tf.add(x, y)
op2 = tf.mul(x, y)
op3 = sess.run(op3)
39
Subgraphs
x = 2
y = 3
useless pow_op
add_op = tf.add(x, y)
mul_op = tf.mul(x, y)
z = sess.run(pow_op) Because we only want the value of pow_op and pow_op doesnt
depend on useless, session wont compute value of useless
save computation
40
Subgraphs
x = 2
y = 3
useless pow_op
add_op = tf.add(x, y)
mul_op = tf.mul(x, y)
Example: AlexNet
Graph from the book Hands-On Machine Learning with Scikit-Learn and 42
TensorFlow
Distributed Computation
To put part of a graph on a specific CPU or GPU:
# Creates a graph.
with tf.device('/gpu:2'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name='b')
c = tf.matmul(a, b)
43
What if I want to build more
than one graph?
44
You can
but you dont need more than one graph
The session runs the default graph
45
But what if I really want to?
46
URGH, NO
47
Multiple graphs require multiple sessions, each will try to use all available
resources by default
Can't pass data between them without passing them through
python/numpy, which doesn't work in distributed
Its better to have disconnected subgraphs within one graph
48
I insist ...
49
tf.Graph()
create a graph:
g = tf.Graph()
50
tf.Graph()
to add operators to a graph, set it as default:
g = tf.Graph()
with g.as_default():
x = tf.add(3, 5)
sess = tf.Session(graph=g)
sess.run(x)
51
tf.Graph()
to add operators to a graph, set it as default:
g = tf.Graph()
with g.as_default():
a = 3
Same as previous
b = 5
x = tf.add(a, b)
# run session
sess.close()
52
tf.Graph()
To handle the default graph:
g = tf.get_default_graph()
53
tf.Graph()
Do not mix default graph and user created graphs
g = tf.Graph()
a = tf.constant(3)
Prone to errors
# add ops to the user created graph
with g.as_default():
b = tf.constant(5)
54
tf.Graph()
Do not mix default graph and user created graphs
g1 = tf.get_default_graph()
g2 = tf.Graph()
with g2.as_default():
b = tf.Constant(5)
55
56
Why graphs
Feeding inputs
Feedback: [email protected]
Thanks!
58