TensorFlow Workshop
TensorFlow Workshop
Core TF Model
Yet another dataflow system
biases Graph of Nodes, also called Operations or ops.
MatMul Xent
examples
labels
s o rs
ith ten
Yet another dataflow systemw
MatMul Xent
examples
labels
t a t e
ith s
Yet another dataflow systemw
'Biases' is a variable Some ops compute gradients −= updates biases
biases
learning rate
ut e d
is t r i b
Yet another dataflow systemd
Device A Device B
biases
learning rate
Graph Construction
● Assemble a Graph of Operations.
Graph Execution
● Deploy and execute operations in a Graph.
Hello, world!
# Create an operation.
hello = tf.Constant("Hello, world!")
# Create a session with the "local" Tensorflow runtime.
sess = tf.Session("local")
# Execute that operation and print its result.
print sess.Run(hello)
Graph Construction
Constant Constant
import numpy as np
c = Constant(np.random.rand(2, 4, 6, 8)) # double 2x4x6x8 tensor
Value Reference
Variable Variable
State
Variables Variable
State
Some Ops modify the Variable state: InitVariable, Assign, AssignSub, AssignAdd.
Random
Parameters
Math Ops
c = Constant(...) c
w = Variable(...) MatMul
w Add
b = Variable(...)
b
y = Add(MatMul(c, w), b)
Documentation at http://go/tensorflow-ops
● Gradients
● Optimizers
● Higher-Level APIs in core TF
● Higher-Level libraries outside core TF
Gradients
many ops
var0 Op
Op loss
var1 Op
Gradients
many ops
var0 Op
Op loss
var1 Op
Op
x Transpose
MatMul gw
gy
MatMul gx
w Transpose
Optimizers
var
AssignSub
grad
Mul
learning_rate
Builtin
● SGD, Adagrad, Momentum, Adam, …
Contributed
● LazyAdam, NAdam, YellowFin, ...
Putting all together to train a Neural
Net
tutorials/mnist/mnist.py
● Shows both training and evaluation
● Also shows InputLayers: Ops that
read data from files
Distributed Execution
Graph Execution
Session API
● Stubby based API to deploy a Graph in a Tensorflow runtime
● Can run any subset of the graph
● Can add Ops to an existing Graph (for interactive use in colab for example)
Training Utilities
● Checkpoint, Recovery, Summaries, Avisu, Replicas, etc.
Tensorflow Runtimes
Python Program
Session
create graph Runtime
create session
sess.Run() CPU
GPU
Remote Runtime
Master RunSubGraph()
Python Program CPU
()
raph Worker
create graph
ateG
s])
create session
([op
Cre
sess.Run() GetTensor()
Run
an op Fetch
an op Fetch
an op Fetch
Feed
an op Fetch
Feed
Example:
learning/brain/models/mnist/mnist_replicas.py
Layers are ops that create Variables
estimator = tf.contrib.learn.Estimator(
model_fn=model_builder(model_name, hparams=hparams),
model_dir=output_dir,
config=tf.contrib.learn.RunConfig(master=...))
experiment = tf.contrib.learn.Experiment(
estimator=estimator,
train_input_fn=f1, eval_input_fn=f2,
eval_metrics=eval_metrics, train_steps=train_steps,
eval_steps=eval_steps, train_monitors=train_monitors)
model_fn
def model_fn(features, targets, mode):
"""Creates the prediction, loss, and train ops.
Args:
features: A dictionary of tensor by feature name.
targets: A tensor representing the labels (targets).
mode: The execution mode, tf.contrib.learn.ModeKeys.
Returns:
A tuple: prediction, loss, and train_op.
input_fn
def input_fn():
"""Supplies input to our model.
Returns:
A tuple consisting of 1) a dictionary of tensors whose keys are
the feature names, and 2) a tensor of target labels if the mode
is not INFER (and None, otherwise).
"""
High-Level Exteral Libraries:
Tensor2Tensor
Tensor2Tensor (github)
Define, train, and evaluate ML tasks and models (especially sequence tasks).
● Many datasets (WMT, MSCoco, LM1B, etc.) and models (Transformer,
ByteNet, NeuralGPU, LSTM) already built in - mix and match!
● Eminently extensible - add a new Problem, T2TModel, or Modality
● Easy distributed training, both sync and async (and with support for multiple
GPUs per machine)
● Easy hyperparameter tuning
Tensor2Tensor Organization
● data_generators/ : generators for datasets which subclass Problem
● models/ : layers and models, models must subclass T2TModel
● utils/ : utilities, t2t_model class, etc.
● google/ : internal stuff (organized in the same way)
● t2t_trainer.py: main binary called to train:
t2t-trainer
--data_dir=$DATA_DIR --problems=$PROBLEM --model=$MODEL \
--hparams_set=$HPARAMS --output_dir=$TRAIN_DIR
Tensor2Tensor Models
@registry.register_model
class ByteNet(t2t_model.T2TModel):
@property
def targeted_vocab_size(self):
return 2**13 # 8192