56
Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Manage ExperimentsCS 20SI:

TensorFlow for Deep Learning ResearchLecture 51/27/2017

1

Page 2: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

2

Page 3: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Justin JohnsonStanford Vision Lab

Wednesday (2/1) 3

Guest lectures

Jon ShlensGoogle Brain

Wednesday (2/8)

They are amazing.

Read their papers!

Page 4: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

4

TensorFlow Dev Summit

Livestream party on campus(probably) with food and drinks and TF engineers

When: Wednesday, Feb 15, 2015Location: TBD

Interested? Send me an email

Page 5: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Agenda

More word2vec

tf.train.Saver

tf.summary

Randomization

Data Readers

5

Page 6: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Where are the gradients?

6

Page 7: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Reverse mode automatic differentiation

7

Page 8: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Reverse mode automatic differentiation

8

Page 9: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.gradients(y, [xs])

9

Take derivative of y with respect to each tensor

in the list [xs]

Page 10: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.gradients(y, [xs])

10

x = tf.Variable(2.0)

y = 2.0 * (x ** 3)

z = 3.0 + y ** 2

grad_z = tf.gradients(z, [x, y])

with tf.Session() as sess:

sess.run(x.initializer)

print sess.run(grad_z) # >> [768.0, 32.0]

# 768 is the gradient of z with respect to x, 32 with respect to y

Page 11: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Should I still learn to take gradients?

11

Page 12: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Vanishing/exploding gradients

12Plot by “Understanding the exploding gradient problem”, Pascanu et al. (2012)

Page 13: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Structure our model

13

Page 14: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

We’ve dumped everything intoone giant function word2vec

(check minus for style in CS106)

14

Page 15: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Need models to be reusable

15

Page 16: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

class SkipGramModel: """ Build the graph for word2vec model """ def __init__(self, params): pass

def _create_placeholders(self): """ Step 1: define the placeholders for input and output """ pass

def _create_embedding(self): """ Step 2: define weights. In word2vec, it's actually the weights that we care about """ pass

def _create_loss(self): """ Step 3 + 4: define the inference + the loss function """ pass

def _create_optimizer(self): """ Step 5: define optimizer """ pass

16

Yay, object oriented programming!!

Page 17: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Manage experiments

17

Page 18: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.train.Saversaves graph’s variables in binary files

18

Page 19: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.train.Saver.save(sess, save_path, global_step=None...)

19

Saves sessions, not graphs!

Page 20: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.train.Saver.save(sess, save_path, global_step=None...)

20

Saves sessions, not graphs!

Page 21: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

# define model

# create a saver objectsaver = tf.train.Saver()

# launch a session to compute the graphwith tf.Session() as sess: # actual training loop

for step in range(training_steps): sess.run([optimizer])

if (step + 1) % 1000==0:saver.save(sess, 'checkpoint_directory/model_name',

global_step=model.global_step)

21

Save parameters after 1000 steps

Page 22: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

# define model

# create a saver objectsaver = tf.train.Saver()

# launch a session to compute the graphwith tf.Session() as sess: # actual training loop

for step in range(training_steps): sess.run([train_op])

if (step + 1) % 1000==0:saver.save(sess, 'checkpoint_directory/model_name',

global_step=model.global_step)

22

Each saved step is a checkpoint

Page 23: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

self.global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step')

23

Global stepVery common in TensorFlow program

Page 24: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

self.global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step')

self.optimizer = tf.train.GradientDescentOptimizer(self.lr).minimize(self.loss, global_step=self.global_step)

24

Global stepNeed to tell optimizer to increment global step

Page 25: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

25

tf.train.Saver

Only save variables, not graph

Checkpoints map variable names to tensors

Page 26: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

saver.restore(sess, 'checkpoints/name_of_the_checkpoint')

e.g. saver.restore(sess, 'checkpoints/skip-gram-99999')

26

Restore variables

Page 27: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoints/checkpoint'))

if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path)

27

Restore the latest checkpoint

1. checkpoint keeps track of the latest checkpoint

2. Safeguard to restore checkpoints only when there are checkpoints

Page 28: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.summaryWhy matplotlib when you can summarize?

28

Page 29: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Visualize our summary statistics during our trainingtf.summary.scalartf.summary.histogramtf.summary.image

29

tf.summary

Page 30: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

with tf.name_scope("summaries"): tf.summary.scalar("loss", self.loss tf.summary.scalar("accuracy", self.accuracy) tf.summary.histogram("histogram loss", self.loss) # merge them all self.summary_op = tf.summary.merge_all()

30

Step 1: create summaries

Page 31: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

loss_batch, _, summary = sess.run([model.loss, model.optimizer, model.summary_op], feed_dict=feed_dict)

31

Step 2: run them

Like everything else in TF, summaries are ops

Page 32: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

writer.add_summary(summary, global_step=step)

32

Step 3: write summaries to file

Page 33: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

See summaries on TensorBoard

33

Page 34: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

34

Scalar loss

Page 35: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

35

Histogram loss

Page 36: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

36

Toggle run to compare experiments

Page 37: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Control Randomization

37

Page 38: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

e.g.

my_var = tf.Variable(tf.truncated_normal((-1.0,1.0), stddev=0.1, seed=0))

38

Op level random seed

Page 39: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

c = tf.random_uniform([], -10, 10, seed=2)

with tf.Session() as sess:print sess.run(c) # >> 3.57493print sess.run(c) # >> -5.97319

----c = tf.random_uniform([], -10, 10, seed=2)

with tf.Session() as sess:print sess.run(c) # >> 3.57493

with tf.Session() as sess:print sess.run(c) # >> 3.57493

39

Sessions keep track of random state

Each new session restarts the random state

Page 40: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

c = tf.random_uniform([], -10, 10, seed=2)d = tf.random_uniform([], -10, 10, seed=2)

with tf.Session() as sess:print sess.run(c) # >> 3.57493print sess.run(d) # >> 3.57493

40

Op level seed: each op keeps its own seed

Page 41: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.set_random_seed(seed)(example: live coding)

41

Graph level seed

Page 42: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Data Readers

42

Page 43: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

43

Problem with feed_dict?

Storage Client Worker

Page 44: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

44

Problem with feed_dict?

Storage Client Workers

Slow when client and workers are on different machines

Page 45: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

45

Data Readers

Storage Worker

Readers allow us to load data directly into the worker process.

Page 46: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

46

Data Readers

Ops that return different values every time you call them(Think Python’s generator)

Page 47: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.TextLineReaderOutputs the lines of a file delimited by newlinesE.g. text files, CSV files

47

Different Readers for different file types

Page 48: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.TextLineReaderOutputs the lines of a file delimited by newlinesE.g. text files, CSV files

tf.FixedLengthRecordReaderOutputs the entire file when all files have same fixed lengthsE.g. each MNIST file has 28 x 28 pixels, CIFAR-10 32 x 32 x 3

48

Different Readers for different file types

Page 49: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.TextLineReaderOutputs the lines of a file delimited by newlinesE.g. text files, CSV files

tf.FixedLengthRecordReaderOutputs the entire file when all files have same fixed lengthsE.g. each MNIST file has 28 x 28 pixels, CIFAR-10 32 x 32 x 3

tf.WholeFileReaderOutputs the entire file content

49

Different Readers for different file types

Page 50: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.TextLineReaderOutputs the lines of a file delimited by newlinesE.g. text files, CSV files

tf.FixedLengthRecordReaderOutputs the entire file when all files have same fixed lengthsE.g. each MNIST file has 28 x 28 pixels, CIFAR-10 32 x 32 x 3 tf.WholeFileReaderOutputs the entire file content

tf.TFRecordReaderReads samples from TensorFlow’s own binary format (TFRecord)

50

Different Readers for different file types

Page 51: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

tf.TextLineReaderOutputs the lines of a file delimited by newlinesE.g. text files, CSV files

tf.FixedLengthRecordReaderOutputs the entire file when all files have same fixed lengthsE.g. each MNIST file has 28 x 28 pixels, CIFAR-10 32 x 32 x 3 tf.WholeFileReaderOutputs the entire file content

tf.TFRecordReaderReads samples from TensorFlow’s own binary format (TFRecord)

tf.ReaderBaseTo allow you to create your own readers

51

Different Readers for different file types

Page 52: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

filename_queue = tf.train.string_input_producer(["file0.csv", "file1.csv"])

reader = tf.TextLineReader()key, value = reader.read(filename_queue)

52

Read in files from queues

Page 53: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

53

tf.FIFOQueue

Page 54: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

54

Threads & Queues

You can use tf.Coordinator and tf.QueueRunner to manage your queues

Page 55: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

with tf.Session() as sess:# start populating the filename queue.coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(coord=coord)

55

Threads & Queues

More on this in week 8

Page 56: Manage Experiments - Stanford University · Manage Experiments CS 20SI: TensorFlow for Deep Learning Research Lecture 5 1/27/2017 1

Next class

Guest lecture by Justin Johnson

Convnet

Style Transfer

Feedback: [email protected]

Thanks!

56