Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Welcome and Logistics
GitHub: https://github.com/amygdala/tensorflow-workshop
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Agenda
- Intro
- Setup time, Introduction to Tensorflow and Cloud ML
- Warmup: XOR
- Wide and Deep
- Use the tf.learn API to jointly train a wide linear model and a deep feed-forward
neural network.
- Word2vec
- Custom Estimators, learning and using word embeddings, and the embeddings
visualizer
- Transfer learning and online prediction
- learn your own image classifications by bootstrapping the Inception v3 model,
then use the Cloud ML API for prediction
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform
5 5
TensorFlow on Cloud ML Workshop Setup
For a cloud-based setup, follow these instructions:
http://bit.ly/aifrontiers-cloudml-install If youre done, visit
playground.tensorflow.org
Or to set up on your laptop:
Clone or download this repo:
https://github.com/amygdala/tensorflow-workshop
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform
6 6
Workshop Setup addendum
If you set up a docker container yesterday, then in the
container, do:
cd
python download_git_repo.py
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform
7 7
Whats TensorFlow?
(and why is it so great for ML?)
An Alaskan Malamute (left) and a Siberian Husky (right). Images from Wikipedia.
https://research.googleblog.com/2016/08/improving-inception-and-image.html
Show and Tell
https://research.googleblog.com/2016/09/show-and-tell-image-captioning-open.html
Text Summarization
Original text
Alice and Bob took the train to visit the zoo. They saw a baby giraffe, a
lion, and a flock of colorful tropical birds.
Abstractive summary
Alice and Bob visited the zoo and saw animals and birds.
https://research.googleblog.com/2016/08/text-summarization-with-tensorflow.html
Parsey McParseface
https://research.googleblog.com/2016/05/announcing-syntaxnet-worlds-most.html
Whats TensorFlow?
A graph of operations.
A graph of
A multidimensional array.
operations.
10000 Images
Each Image has 256 Rows
Each Row has 256 Pixels
Each Pixel has 3 channels (RGB)
MatMul Xent
examples
labels
s o rs
Computation is a dataflow graph ten
with
MatMul Xent
examples
labels
t a t e
Computation is a dataflow graph
w ith s
biases
learning rate
Build a graph; then run it.
a b
...
c = tf.add(a, b) add
c
...
session = tf.Session()
value_of_c = session.run(c, {a=1, b=2})
From: http://googleresearch.blogspot.com/2016_03_01_archive.html
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 23
Common models
tf.contrib.learn.Estimator
Building Models
tf.contrib.
{layers, losses, metrics}
INNOVATION + ASSISTANCE
TensorFlow Distributed Execution Engine
Train
Inputs model Model
Proprietary + Confidential
27
As your data size increases, batching and distribution
become important
Train
Inputs model
Model
Proprietary + Confidential
28
Input necessary transformations
Proprietary + Confidential
29
Hyperparameter tuning might be nice
Hyper-parameter
tuning
Proprietary + Confidential
30
Need to autoscale prediction code
Hyper-parameter
tuning
Deploy
Proprietary + Confidential
31
Cloud machine learningrepeatable, scalable, tuned
Distributed
training,
Hyper-parameter
tuning
Same Deploy
Proprietary + Confidential
32
A first look at some code:
Creating and running a TensorFlow graph
to learn XOR
Simple
No need to do data manipulation
Cant learn with a single linear
regression
Can use TensorFlows standard
gradient descent tools to learn it.
XOR cannot be learned without
artificial non-linearity, and at least
one hidden layer
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 35
XOR: Building The Graph
1 0
def make_graph(features,
labels,
num_hidden=8):
hidden_weights = tf.Variable(
tf.truncated_normal(
[2, num_hidden],
stddev=1/math.sqrt(2)))
# Shape [4, num_hidden]
hidden_activations = tf.nn.relu(
tf.matmul(features, hidden_weights))
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 36
XOR: Building The Graph
1 0
output_weights = tf.Variable(tf.truncated_normal(
[num_hidden, 1],
stddev=1/math.sqrt(num_hidden)
))
# Shape [4, 1]
logits = tf.matmul(hidden_activations,
output_weights)
# Shape [4]
(0, 1)
predictions = tf.sigmoid(tf.squeeze(logits))
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 37
Minimize loss: optimizers
error
tf.train.GradientDescentOptimizer
function minimum
loss = tf.reduce_mean(
tf.square(predictions - tf.to_float(labels)))
gs = tf.Variable(0, trainable=False)
train_op = tf.train.GradientDescentOptimizer(
0.2
).minimize(loss, global_step=gs)
(0, )
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 39
Notebook Interlude
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 41
XOR: Running The Graph
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 42
Common TF NN pattern:
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 43
Monitoring With TensorBoard: xor_summaries
Introducing TensorBoard
tensorboard --logdir=<logdir>
(reads subdirs recursively! Great for multiple runs)
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 49
Break (10 min)
google.cloud.ml
custom NN models
Predefined estimators
LinearClassifier, DNNClassifier,
DNNRegressor, DNNLinearCombinedClassifier
Limited configuration options: feature columns, metrics.
Workshop section:
wide_n_deep
fnlwgt Continuous The number of people the census takers believe that
observation represents (sample weight). This variable will
not be used.
education Categorical The highest level of education achieved for that individual.
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
70
File Queues
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TextLineReader()
key, value = reader.read_up_to(filename_queue,
num_records=BATCH_SIZE)
record_defaults = [[0], [" "], [0], [" "], [0],
[" "], [" "], [" "], [" "], [" "],
[0], [0], [0], [" "], [" "]]
columns = tf.decode_csv(value, record_defaults=record_defaults)
m = tf.contrib.learn.DNNLinearCombinedClassifier(
model_dir=model_dir,
linear_feature_columns=wide_columns,
dnn_feature_columns=deep_columns,
dnn_hidden_units=[100, 70, 50, 25])
m.fit(input_fn=generate_input_fn(train_file),
steps=1000)
results = m.evaluate(
input_fn=generate_input_fn(test_file),
steps=1)
print('Accuracy: %s' % results['accuracy'])
m = tf.contrib.learn.DNNLinearCombinedClassifier(
model_dir=model_dir,
linear_feature_columns=wide_columns,
dnn_feature_columns=deep_columns,
dnn_hidden_units=[100, 70, 50, 25])
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 81
To the code!
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 86
Break (10 min)
Up next:
Building Word embeddings
and Custom Estimators
Building Models
tf.contrib.
{layers, losses, metrics}
INNOVATION + ASSISTANCE
TensorFlow Distributed Execution Engine
output
relu
add bias
INNOVATION + ASSISTANCE
mul weights
input
90
TensorFlow: Flexibility vs. Usability
output
relu
INNOVATION + ASSISTANCE
mul weights
input
91
TensorFlow: Flexibility vs. Usability
output
relu
INNOVATION + ASSISTANCE
mul weights
input
92
Common models
tf.contrib.learn.Estimator Integration with TFX
Building Models
tf.contrib.
{layers, losses, metrics}
INNOVATION + ASSISTANCE
TensorFlow Distributed Execution Engine
fit
Estimator
evaluate
Model Definition
(TensorFlow py)
predict
INNOVATION + ASSISTANCE
Sessions & Graphs
export
94
But, what if there is not a
pre-baked Estimator for your
model?
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
97
Word embeddings
Word data (and categorical data in general) cant be modeled as dense
data
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
98
model.nearby([b'cat'])
https://www.tensorflow.org/versions/r0.8/images/nce-nplm.png
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
101
Skip-Gram model
(predict source context-words from target words)
the quick brown fox jumped over the lazy dog ...
([the, brown], quick), ([quick, fox], brown), ([brown,
jumped], fox),
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
102
Skip-gram model
(predict source context-words from target words)
the quick brown fox jumped over the lazy dog ...
([the, brown], quick), ([quick, fox], brown), ([brown,
jumped], fox),
Input/output pairs:
(quick, the), (quick, brown), (brown, quick), (brown,
fox),
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
103
Custom Estimator:
model_fn: Constructs the graph given features, labels,
and a mode
input_fn: Constructs a graph to read input tensors
from files using a QueueRunner
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
104
Why Is Word2Vec a good example problem?
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
105
Experiment and learn_runner
Workshop section:
word2vec
range_queue = tf.train.range_input_producer(
num_windows,
shuffle=False,
capacity=windows_per_batch * 2,
num_epochs=num_epochs
)
indices = range_queue.dequeue_many(windows_per_batch)
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
113
Variable Partitioning
with tf.device(tf.train.replica_device_setter()):
with tf.variable_scope('nce',
partitioner=tf.fixed_size_partitioner(
num_partitions)):
embeddings = tf.get_variable(
'embeddings',
shape=[vocab_size, embedding_size],
dtype=tf.float32,
initializer=tf.random_uniform_initializer(-1.0, 1.0)
)
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
114
Hyperparameter Tuning
Objective
tuning
Google-developed search algorithm
efficiently finds better
hyperparameters for your
Hy
model/dataset
pe
rP
m#1
ara
One line of code: a r a
er P
Hyp
m
tf.summary.scalar('training/hptuning/
#2
metric', my_metric_tensor)
Google Cloud Platform Confidential & Proprietary 116
Break (10 min)
Up next:
Transfer Learning
and Online Prediction
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
119
Transfer Learning
We can bootstrap an existing model to reduce the
effort needed to learn something new.
we will use an Inception v3 architecture model trained
on ImageNet images:
use values generated from its penultimate "bottleneck" layer
train a new top layer that can recognize other classes of
images.
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
120
Demo
Dont Hug?
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
122
Okay, how did we do that?
Train a small model that sits on top, taking the embeds as input
Tell Cloud ML that we want to use and serve our trained model.
Use the Cloud ML API for online prediction with the model
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
124
Bootstrapping with the Inception model
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
125
Our steps:
Do image pre-processing using the Inception v3 model to get image
bottleneck embeds: these express useful high-level image features
Train a small model that sits on top, taking the embeds as input
Tell Cloud ML that we want to use and serve our trained model.
Use the Cloud ML API for online prediction with the model
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
126
Lab: Transfer Learning:
Bootstrapping the Inception v3 model to learn
whether objects are huggable or not
+ making predictions using the Cloud ML API
Workshop section:
transfer_learning/cloudml
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
128
Generating the embeddings
def build_graph(self):
"""Returns:
input_jpeg: A tensor containing raw image bytes as the input layer.
embedding: The embeddings tensor, that will be materialized later.
"""
input_jpeg = tf.placeholder(tf.string, shape=None)
add some image conversion ops...
inception_input = munged_image
# Build Inception layers, which expect a tensor of type float from [-1, 1)
# and shape [batch_size, height, width, channels].
with slim.arg_scope(inception.inception_v3_arg_scope()):
_, end_points = inception.inception_v3(inception_input, is_training=False)
embedding = end_points['PreLogits']
return input_jpeg, embedding
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
129
Generating the embeddings
def calculate_embedding(self, batch_image_bytes):
"""Get the embeddings for a given JPEG image.
Args:
batch_image_bytes: As if returned from [ff.read() for ff in file_list].
Returns:
The Inception embeddings (bottleneck layer output)
"""
return self.tf_session.run(
self.embedding, feed_dict={self.input_jpeg: batch_image_bytes})
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
130
Lab Step 1: Deploy the pre-processing
pipeline
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
132
Building our own model
Build model graph conditionals for train, evaluate, and predict
The prediction part will need to know how to generate the embeds
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
133
def add_final_training_ops(
self,embeddings, all_labels_count, bottleneck_tensor_size,
hidden_layer_size=BOTTLENECK_TENSOR_SIZE / 4, dropout_keep_prob=None):
with tf.name_scope('input'):
bottleneck_input = tf.placeholder_with_default(
embeddings, shape=[None, bottleneck_tensor_size],
name='ReshapeSqueezed')
bottleneck_with_no_gradient = tf.stop_gradient(bottleneck_input)
with tf.name_scope('Wx_plus_b'):
hidden = layers.fully_connected(bottleneck_with_no_gradient,
hidden_layer_size)
if dropout_keep_prob:
hidden = tf.nn.dropout(hidden, dropout_keep_prob)
logits = layers.fully_connected(
hidden, all_labels_count, activation_fn=None)
softmax = tf.nn.softmax(logits, name='softmax')
return softmax, logits
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
134
Lab Step 2:
Train our model on Cloud ML
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
137
Queues In TensorFlow!
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
138
Lab Step 3:
Use the trained model for prediction
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
140
Making online predictions
gcloud beta ml predict --model <model_name> \
--json-instances <request.json>
Using the Cloud ML API (and the Google API client
libraries)
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
141
Putting it all together:
back to our demo!
http://www.deeplearningbook.org/
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop
Google Cloud Platform 144
Thank you!
http://bit.ly/tf-workshop-slides bit.ly/tensorflow-workshop