Skip to content

Latest commit

 

History

History
371 lines (260 loc) · 6.3 KB

presentation.md

File metadata and controls

371 lines (260 loc) · 6.3 KB

Introduction to Deep Learning with TensorFlow

DePaul University, Chicago
March 9, 2017

Garrett Smith / @gar1t


http://playground.tensorflow.org


Source: RSIP


Inception v3

Source: Google


Code considered instructive


Follow along

https://git.io/vyVa0


TensorFlow = graphs


Graph operations

> import tensorflow as tf
>
> one = tf.constant(1)
> two = tf.constant(2)
> three = one + two
>
> sess = tf.Session()
> sess.run(three)
3

Graph operations

> sess.graph == tf.get_default_graph()
True
> sess.graph.get_operations()
[<tf.Operation 'Const' type=Const>,
 <tf.Operation 'Const_1' type=Const>,
 <tf.Operation 'add' type=Add>]

Graph operations (TensorBoard)


Placeholders

> tf.reset_default_graph()
>
> one = tf.constant(1)
> x = tf.placeholder(tf.int32)
> y = x + one
>
> sess = tf.Session()
> sess.run(y, {x:2})
3

Placeholders

> sess.graph.get_operations()
[<tf.Operation 'Const' type=Const>,
 <tf.Operation 'Placeholder' type=Placeholder>,
 <tf.Operation 'add' type=Add>]

Placeholders (TensorBoard)


Variables

> tf.reset_default_graph()
>
> one = tf.constant(1)
> x = tf.Variable(2)
> y = x + one
>
> sess = tf.Session()
> sess.run(tf.global_variables_initializer())
> sess.run(x)
2
> sess.run(y)
3

Variables

> sess.graph.get_operations()
[<tf.Operation 'Const' type=Const>,
 <tf.Operation 'Variable/initial_value' type=Const>,
 <tf.Operation 'Variable' type=VariableV2>,
 <tf.Operation 'Variable/Assign' type=Assign>,
 <tf.Operation 'Variable/read' type=Identity>,
 <tf.Operation 'add' type=Add>,
 <tf.Operation 'init' type=NoOp>]

Variables (TensorBoard)


Advanced Operations (matrix mult)

> tf.reset_default_graph()
>
> x = tf.Variable([[2,1]])
> y = tf.Variable([[1],[2]])
> z = tf.matmul(x, y)
>
> sess = tf.Session()
> sess.run(tf.global_variables_initializer())
> sess.run(z)
array([[4]], dtype=int32)

Advanced Operations (TensorBoard)


Graphs = models and their operations


Machine learning


Training script

def train():
    for step in range(training_steps):
        batch = data.next_batch()
        sess.run(train_op, batch)
        maybe_log_status(step, batch)
        maybe_save_model()

if __name__ == "__main__":
    init_data()
    init_train()
    train()

Train

def init_train():
    init_model()
    init_train_op()
    init_accuracy_op()
    init_summaries()
    init_session()

Model (softmax regression)

def init_model():
    global x, y
    x = tf.placeholder(tf.float32, [None, 784])
    W = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    y = tf.nn.softmax(tf.matmul(x, W) + b)

Train op (gradient descent)

def init_train_op():
    global y_, loss, train_op
    y_ = tf.placeholder(tf.float32, [None, 10])
    loss = tf.reduce_mean(
             -tf.reduce_sum(
               y_ * tf.log(y),
               reduction_indices=[1]))
    train_op = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

Accuracy op

def init_accuracy_op():
    global accuracy
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

Summaries

def init_summaries():
    global summaries, writer
    tf.summary.scalar("loss", loss)
    tf.summary.scalar("accuracy", accuracy)
    summaries = tf.summary.merge_all()
    writer = tf.summary.FileWriter(FLAGS.rundir + "/train")

Session

def init_session():
    global sess
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

Logging status

def log_status(step, batch):
    accuracy_val, summary = sess.run([accuracy, summaries], batch)
    writer.add_summary(summary, step)
    print "Step %i: accuracy=%f" % accuracy_val

Saving a model

def save_model():
    saver = tf.train.Saver()
    saver.save(sess, FLAGS.rundir + "/model/export")

Full graph (MNIST regression)


Full graph (MNIST CNN)


Training script (review)

def train():
    for step in range(training_steps):
        batch = data.next_batch()
        sess.run(train_op, batch)
        maybe_log_status(step, batch)
        maybe_save_model()

if __name__ == "__main__":
    init_data()
    init_train()
    train()

In practice

https://guild.ai


Full TensorFlow Lifecycle

  • Get code
  • Prepare model for training
  • Train model
  • Compare results
  • Serve model

Deep learning with TensorFlow

  • TF is a low level library for machine learners
  • Frameworks growing in popularity and support (High level API/TFLearn, Keras, TFSlim)
  • Graphs are central (define model & ops, run, export, serve)
  • Good conventions (patterns) essential for success

Links