Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching Computations in TensorFlow

Tags:

tensorflow

Is there a canonical way to reuse computations from a previously-supplied placeholder in TensorFlow? My specific use case:

  • supply many inputs (using one placeholder) simultaneously, all of which are fed through a network to obtain smaller representations
  • define a loss based on various combinations of these smaller representations
  • train on one batch at a time, where each batch uses some subset of the inputs, without recomputing the smaller representations

Here is the goal in code, but which is defective because the same computations are carried out again and again:

X_in = some_fixed_data
combinations_in = large_set_of_combination_indices
for combination_batch_in in batches(combinations_in, batch_size=128):
    session.run(train_op, feed_dict={X: X_in, combinations: combination_batch_in})

Thanks.

like image 568
rd11 Avatar asked Mar 13 '23 20:03

rd11


1 Answers

The canonical way to share computed values across sess.Run() calls is to use a Variable. In this case, you could set up your graph so that when the Placeholders are fed, they compute a new value of the representation that is saved into a Variable. A separate portion of the graph reads those Variables to compute the loss. This will not work if you need to compute gradients through the part of the graph that computes the representation. Computing those gradients will require recomputing every Op in the encoder.

like image 110
Ian Goodfellow Avatar answered Mar 24 '23 18:03

Ian Goodfellow