Tensorflow 1
Tensorflow 1
CS 20SI:
TensorFlow for Deep Learning Research
Lecture 2
1/18/2017
1
2
Agenda
Basic operations
Tensor types
Lazy loading
3
Your first TensorFlow program
import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
x = tf.add(a, b)
print sess.run(x)
4
Visualize it with TensorBoard
import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
Create the summary writer after graph
x = tf.add(a, b)
definition and before running your session
with tf.Session() as sess:
$ python [yourprogram].py
6
Go here
7
Visualize it with TensorBoard
import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
x = tf.add(a, b)
print sess.run(x)
8
Visualize it with TensorBoard
import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
x = tf.add(a, b)
9
Explicitly name them
import tensorflow as tf
a = tf.constant(2, name="a")
b = tf.constant(3, name="b")
x = tf.add(a, b, name="add")
10
Explicitly name them
import tensorflow as tf
a = tf.constant(2, name="a")
b = tf.constant(3, name="b")
x = tf.add(a, b, name="add")
11
Learn to use TensorBoard
well and often.
It will help a lot when you build
complicated models.
12
More constants
13
More constants
import tensorflow as tf
tf.constant(value, dtype=None, shape=None,
a = tf.constant([2, 2], name="a") name='Const', verify_shape=False)
x, y = sess.run([x, y])
print x, y
# >> [5 8] [6 12]
14
Tensors filled with a specific value
tf.zeros(shape, dtype=tf.float32, name=None)
creates a tensor of shape and all elements will be zeros (when ran in session)
Similar to numpy.zeros
15
Tensors filled with a specific value
tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)
creates a tensor of shape and type (unless type is specified) as the input_tensor but all elements are zeros.
Similar to numpy.zeros_like
# input_tensor is [0, 1], [2, 3], [4, 5]]
16
Tensors filled with a specific value
Same:
Similar to numpy.ones,
numpy.ones_like
17
Tensors filled with a specific value
tf.fill(dims, value, name=None)
18
Constants as sequences
tf.linspace(start, stop, num, name=None) # slightly different from np.linspace
# 'limit' is 5
19
Randomly Generated Constants
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.set_random_seed(seed)
21
Operations
22
*Table from “Fundamental of Deep Learning”
Operations
a = tf.constant([3, 6])
Pretty standard, quite similar to numpy.
b = tf.constant([2, 2]) See TensorFlow documentation
tf.add(a, b) # >> [5 8]
tf.div(a, b) # >> [1 3]
tf.mod(a, b) # >> [1 0]
23
TensorFlow Data Types
TensorFlow takes Python natives types: boolean, numeric (int, float), strings
24
TensorFlow Data Types
TensorFlow takes Python natives types: boolean, numeric (int, float), strings
25
TensorFlow Data Types
TensorFlow takes Python natives types: boolean, numeric (int, float), strings
26
TensorFlow Data Types
TensorFlow takes Python natives types: boolean, numeric (int, float), strings
28
TF vs NP Data Types
TensorFlow integrates seamlessly with NumPy
For tf.Session.run(fetches):
If the requested fetch is a Tensor , then the output of will be a NumPy ndarray.
29
TensorFlow Data Types
Do not use Python native types for tensors because TensorFlow has to infer Python
type
30
TensorFlow Data Types
Beware when using NumPy arrays because NumPy and TensorFlow might become
not so compatible in the future!
31
What’s wrong with constants?
32
Other than being constant ...
33
What’s wrong with constants?
34
Print out the graph def
import tensorflow as tf
print sess.graph.as_graph_def()
35
This makes loading graphs expensive
when constants are big
36
Only use constants for primitive types.
Use variables or readers for more data that
requires more memory
37
Variables?
# create variable a with scalar value
a = tf.Variable(2, name="scalar")
38
Variables?
# create variable a with scalar value
a = tf.Variable(2, name="scalar") Why tf.constant but tf.Variable and not
tf.variable?
# create variable b as a vector
b = tf.Variable([2, 3], name="vector")
39
How about variables?
# create variable a with scalar value
a = tf.Variable(2, name="scalar") tf.Variable is a class, but tf.constant is an op
40
How about variables?
# create variable a with scalar value
a = tf.Variable(2, name="scalar") tf.Variable holds several ops:
x = tf.Variable(...)
# create variable b as a vector
b = tf.Variable([2, 3], name="vector") x.initializer # init op
x.value() # read op
# create variable c as a 2x2 matrix x.assign(...) # write op
c = tf.Variable([[0, 1], [2, 3]], name="matrix") x.assign_add(...) # and more
41
You have to initialize your variables
The easiest way is initializing all variables at once:
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
42
You have to initialize your variables
The easiest way is initializing all variables at once:
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
43
You have to initialize your variables
The easiest way is initializing all variables at once:
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
W = tf.Variable(tf.zeros([784,10]))
with tf.Session() as sess:
sess.run(W.initializer)
44
Eval() a variable
# W is a random 700 x 100 variable object
W = tf.Variable(tf.truncated_normal([700, 10]))
with tf.Session() as sess:
sess.run(W.initializer)
print W
45
Eval() a variable
# W is a random 700 x 100 variable object
W = tf.Variable(tf.truncated_normal([700, 10]))
with tf.Session() as sess:
sess.run(W.initializer)
print W.eval()
47
tf.Variable.assign()
W = tf.Variable(10)
W.assign(100) Uh, why?
with tf.Session() as sess:
sess.run(W.initializer)
print W.eval() # >> 10
48
tf.Variable.assign()
W = tf.Variable(10)
W.assign(100) W.assign(100) doesn’t assign the value 100
with tf.Session() as sess: to W. It creates an assign op, and that op
sess.run(W.initializer) needs to be run to take effect.
print W.eval() # >> 10
49
tf.Variable.assign()
W = tf.Variable(10)
W.assign(100)
with tf.Session() as sess:
sess.run(W.initializer)
print W.eval() # >> 10
--------
W = tf.Variable(10)
assign_op = W.assign(100)
with tf.Session() as sess:
sess.run(W.initializer)
sess.run(assign_op)
print W.eval() # >> 100
50
tf.Variable.assign()
W = tf.Variable(10)
W.assign(100)
with tf.Session() as sess:
sess.run(W.initializer)
print W.eval() # >> 10
--------
W = tf.Variable(10)
assign_op = W.assign(100)
with tf.Session() as sess: You don’t need to initialize variable
sess.run(assign_op) because assign_op does it for you
print W.eval() # >> 100
51
tf.Variable.assign()
W = tf.Variable(10)
W.assign(100)
with tf.Session() as sess:
sess.run(W.initializer)
print W.eval() # >> 10
--------
W = tf.Variable(10)
assign_op = W.assign(100) In fact, initializer op is the assign op that
with tf.Session() as sess: assigns the variable’s initial value to the
sess.run(assign_op) variable itself.
print W.eval() # >> 100
52
tf.Variable.assign()
# create a variable whose original value is 2
my_var = tf.Variable(2, name="my_var")
53
tf.Variable.assign()
# create a variable whose original value is 2
my_var = tf.Variable(2, name="my_var")
54
tf.Variable.assign()
# create a variable whose original value is 2
my_var = tf.Variable(2, name="my_var")
55
assign_add() and assign_sub()
my_var = tf.Variable(10)
sess.run(my_var.initializer)
assign_add() and assign_sub() can’t
# increment by 10 initialize the variable my_var for you
because these ops need the original value
sess.run(my_var.assign_add(10)) # >> 20 of my_var
# decrement by 2
sess.run(my_var.assign_sub(2)) # >> 18
56
Each session maintains its own copy of
W = tf.Variable(10)
variable
sess1 = tf.Session()
sess2 = tf.Session()
sess1.run(W.initializer)
sess2.run(W.initializer)
57
Each session maintains its own copy of
W = tf.Variable(10)
variable
sess1 = tf.Session()
sess2 = tf.Session()
sess1.run(W.initializer)
sess2.run(W.initializer)
58
Each session maintains its own copy of
W = tf.Variable(10)
variable
sess1 = tf.Session()
sess2 = tf.Session()
sess1.run(W.initializer)
sess2.run(W.initializer)
sess1.close()
sess2.close()
59
Use a variable to initialize another variable
Want to declare U = 2 * W
60
Use a variable to initialize another variable
Want to declare U = W * 2
61
Session vs InteractiveSession
62
Control Dependencies
tf.Graph.control_dependencies(control_inputs)
63
Project speed dating
64
Project Speed Dating
65
Placeholder
66
A quick reminder
67
Placeholders
⇒ Can assemble the graph first without knowing the values needed for
computation
68
Placeholders
⇒ Can assemble the graph first without knowing the values needed for
computation
Analogy:
Can define the function f(x, y) = x*2 + y without knowing value of x or y. x, y are
placeholders for the actual values.
69
Why placeholders?
We, or our clients, can later supply their own data when they
need to execute the computation.
70
Placeholders
71
Feed the values to placeholders using a
dictionary
72
Placeholders
# >> [6, 7, 8]
73
Placeholders
# >> [6, 7, 8]
75
Placeholders are valid ops
# >> [6, 7, 8]
76
What if want to feed multiple data points in?
We feed all the values in, one at a time
with tf.Session() as sess:
for a_value in list_of_values_for_a:
print sess.run(c, {a: a_value})
77
You can feed_dict any feedable tensor.
Placeholder is just a way to indicate that
something must be fed
78
tf.Graph.is_feedable(tensor)
# True if and only if tensor is feedable.
79
Feeding values to TF ops
80
Extremely helpful for testing too
81
The trap of lazy loading*
83
Defer creating/initializing an object
until it is needed
84
Lazy loading Example
Normal loading:
x = tf.Variable(10, name='x')
y = tf.Variable(20, name='y')
z = tf.add(x, y) # you create the node for add node before executing the graph
85
Lazy loading Example
Lazy loading:
x = tf.Variable(10, name='x')
y = tf.Variable(20, name='y')
86
Both give the same value of z
What’s the problem?
87
TensorBoard
Normal loading
88
TensorBoard
Lazy loading (just missing the node Add, bad for reading graph, but not a bug)
89
TensorBoard
Lazy loading
90
tf.get_default_graph().as_graph_def()
Normal loading:
node {
name: "Add"
op: "Add"
input: "x/read"
Node “Add” added once to the graph definition
input: "y/read"
attr {
key: "T"
value {
type: DT_INT32
}
}
}
91
tf.get_default_graph().as_graph_def()
Lazy loading:
node {
name: "Add"
op: "Add"
... Node “Add” added 10 times to the graph
definition
}
... Or as many times as you want to compute z
node {
name: "Add_9"
op: "Add"
...
}
92
Imagine you want to compute an op
thousands of times!
93
Your graph gets bloated
Slow to load
Expensive to pass around
94
One of the most common TF non-bug bugs
I’ve seen on GitHub
95
Solution
97
98
We will construct this
model next time!!
99
Next class
Linear regression in TensorFlow
Optimizers
Feedback: huyenn@stanford.edu
Thanks!
100