Tensorflow 2 - 0 Slides PDF
Tensorflow 2 - 0 Slides PDF
0
MBL, August 2019
1
Agenda 1 of 2
Exercises
Games
● QuickDraw
Agenda 2 of 2
Walkthroughs and new tutorials
Games
● Sketch RNN
Learning more
● Book recommendations
Deep Learning is representation learning
Image link
Image link
Latest tutorials and guides
tensorflow.org/beta
bit.ly/mnist-seq
...
...
...
Softmax
model = Sequential()
model.add(Dense(256, activation='relu',input_shape=(784,)))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
Linear model
Neural network
...
Softmax activation
After training, select all the
weights connected to this
output.
model.layers[0].get_weights()
img = weights.reshape(28,28)
plt.imshow(img, cmap = plt.get_cmap('seismic'))
...
...
Softmax activation
After training, select all the
weights connected to this
output.
Exercise 1 (option #1)
Exercise: bit.ly/mnist-seq
Reference:
tensorflow.org/beta/tutorials/keras/basic_classification
TODO:
Add a validation set. Add code to plot loss vs epochs (next slide).
Exercise 1 (option #2)
bit.ly/ijcav_adv
bit.ly/ijcai_adv_answer
About TensorFlow 2.0
19
Install
# GPU
!pip install tensorflow-gpu==2.0.0-beta1
# CPU
!pip install tensorflow==2.0.0-beta1
In either case, check your installation (in Colab, you may need to use runtime -> restart after installing).
import tensorflow as tf
print(tf.__version__) # 2.0.0-beta1
Nightly is available too, but best bet: stick with a named release for stability.
TF2 is imperative by default
import tensorflow as tf
print(tf.__version__) # 2.0.0-beta1
x = tf.constant(1)
y = tf.constant(2)
z = x + y
x = tf.constant(1)
y = tf.constant(2)
z = tf.add(x, y)
print(z)
TF1: Build a graph, then run it.
import tensorflow as tf # 1.14.0
print(tf.__version__)
x = tf.constant(1)
y = tf.constant(2)
z = tf.add(x, y)
Example
When in doubt, copy the imports from one of the tutorials on tensorflow.org/beta
Notes
A superset of the reference implementation. Built-in to TensorFlow 2.0 (no need to install Keras
separately).
● GPU support
● Autodiff
● Distributed training
● JIT compilation
● A portable format (train in Python on Mac, deploy on iOS using Swift, or in a browser using
JavaScript)
29
Sequential models
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'])
35
Use a built-in training loop
model.fit(x_train, y_train, epochs=5)
Or, define your own
model = MyModel()
38
A vector of partial derivatives.
Gradient points in
Gradient descent
direction of steepest Calculate the gradient.
ascent, so we step in Take a step.
reverse direction. Repeat.
Loss
t=1
t=2
t=3
Parameter
With more than one variable
Loss (w0, w1)
w
w
1
The gradient points 0in the direction of steepest ascent. We usually want to
minimize a function (like loss), so we take a step in the opposite direction..
40
Training models with gradient descent
Forward pass
Calculate loss
Backward pass
bit.ly/tf-ws1
12
1.4 0.5 0.7 1.2
12 48 0.5 130.1 Plane
48
96
+ =
96 18
18
w x b Output
One image and two classes
Multiple inputs; multiple outputs
12
1.4 0.5 0.7 1.2
12 48 0.5 130.1 Plane
48
-2.0 0.1 0.2 -0.7
96
+ 1.2 = -11.4 Car
96 18
18
46
Two images and two classes
12 48 Image 1 Image 2
96 18
1.4 0.5 0.7 1.2 12 4
+ 0.5
= 130.1 131.7 Plane
18 96
4 18
2 96
W x b Output
...
...
Softmax activation
After training, select all the
weights connected to this
output.
model.layers[0].get_weights()
img = weights.reshape(28,28)
plt.imshow(img, cmap = plt.get_cmap('seismic'))
...
...
Softmax activation
After training, select all the
weights connected to this
output.
A neural network
ReLU
Output
Applied piecewise
Output
Activation functions introduce non-linearities
Notes
- You can make similar plots (and more) with this example. Note: from an older version of TF, but should work out of the box in Colab.
- Each of our convolutional layers used an activation as well (not shown in previous slides).
- You can make a demo of this in TensorFlow Playground by setting activation = Linear (or none)
Without activation, many layers are equivalent to one
# If you replace 'relu' with 'None', this model ...
model = Sequential([
Dense(256, activation='relu', input_shape=(2,)),
Dense(256, activation='relu'),
Dense(256, activation='relu'),
Dense(1, activation='sigmoid')
])
12.8 Truck
Scores Probabilities
Note: these are ‘probability like’ numbers (do not go to vegas and bet in this ratio).
Cross entropy compares two distributions
Cross entropy loss for a batch of
examples
Each example has a label in a one-hot True prob (either 1 or 0)
format in our case!
This is a bird
Predicted probabilities
0.1 0.2 0.6 0.2 0.0 0.0 0.0 0.0 0.0 0.0
Answers: bit.ly/ijcai_1-a_answers
TensorFlow RFP
[email protected]
goo.gle/tensorflow-rfp
Convolution
60
Not a Deep Learning concept
import scipy
from skimage import color, data
import matplotlib.pyplot as plt
img = data.astronaut()
img = color.rgb2gray(img)
plt.axis('off')
plt.imshow(img, cmap=plt.cm.gray)
Convolution example
-1 -1 -1
-1 8 -1
-1 -1 -1
Notes
Edge detection intuition: dot
product of the filter with a
region of the image will be
zero if all the pixels around
the border have the same
value as the center.
-1 8 -1
-1 -1 -1
Notes
Edge detection intuition: dot
product of the filter with a
region of the image will be
zero if all the pixels around
the border have the same
value as the center.
Eileen Collins
A simple edge detector
kernel = np.array([[-1,-1,-1],
[-1,8,-1],
[-1,-1,-1]])
result = scipy.signal.convolve2d(img, kernel, 'same')
plt.axis('off')
plt.imshow(result, cmap=plt.cm.gray)
Easier to see with seismic
-1 -1 -1
-1 8 -1
-1 -1 -1
Notes
Edge detection intuition: dot
product of the filter with a
region of the image will be
zero if all the pixels around
the border have the same
value as the center.
Eileen Collins
Example
2 0 1 1
1 0 1
0 1 0 0
0 0 0
0 0 1 0
0 1 0
0 3 0 0
2 0 1 1
1 0 1
0 1 0 0 3
0 0 0
0 0 1 0
0 1 0
0 3 0 0
2 0 1 1
1 0 1
0 1 0 0 3 2
0 0 0
0 0 1 0
0 1 0
0 3 0 0
2 0 1 1
1 0 1
0 1 0 0 3 2
0 0 0
0 0 1 0 3
0 1 0
0 3 0 0
2 0 1 1
1 0 1
0 1 0 0 3 2
0 0 0
0 0 1 0 3 1
0 1 0
0 3 0 0
model.add(Conv2D(filters=4,
kernel_size=(4,4),
input_shape=(10,10,3))
A RGB image as a 3d volume.
Each color (or channel) is a
layer.
weights
4 In 3d, our filters have width,
4 height, and depth.
3
weights
4
4
3
weights
4 Applied in the same way as 2d
4 (sum of weight * pixel value as
3 they slide across the image).
...
weights
4 Applying the convolution over
4 the rest of the input image.
3
weights
4 More filters, more output channels.
4
3
Going deeper
model = Sequential()
model.add(Conv2D(filters=4,
kernel_size=(4,4),
input_shape=(10,10,3))
model.add(Conv2D(filters=8,
kernel_size=(3,3))
weights
3
3
4
Edges
???
Shapes
...
...
Textures
...
...
…
Exercise
bit.ly/ijcai_1_b
Ref: tensorflow.org/beta/tutorials/images/intro_to_cnns
Exercise
bit.ly/ijcai_1b
Answers: bit.ly/ijcai_1_b_answers
Game 1
Would you like to volunteer?
quickdraw.withgoogle.com
Example: transfer learning
bit.ly/ijcai_2
Ref: tensorflow.org/beta/tutorials/images/transfer_learning
Ref: tensorflow.org/beta/tutorials/images/hub_with_keras
Example: transfer learning
bit.ly/ijcai_2
Answers: bit.ly/ijcai_2_answers
Deep Dream
New tutorial
bit.ly/dream-wip
Image segmentation
Recent tutorial
bit.ly/im-seg
Timeseries forecasting
Recent tutorial
Game 2
Who would like to volunteer?
magenta.tensorflow.org/assets/sketch_rnn_
demo/index.html
CycleGAN
Recent tutorial
Under the hood
93
Let’s make this faster
lstm_cell = tf.keras.layers.LSTMCell(10)
# 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)
# benchmark
timeit.timeit(lambda: lstm_cell(input, state), number=10) # 0.03
timeit.timeit(lambda: fn(input, state), number=10) # 0.004
AutoGraph makes this possible
@tf.function
def f(x):
while tf.reduce_sum(x) > 1:
x = tf.tanh(x)
return x
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
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'])
Learning more
Latest tutorials and guides
● tensorflow.org/beta
Books
For details
● deeplearningbook.org
100