jpp1
jpp1

Reputation: 2119

Accessing intermediate results from a tensorflow graph

If I have a complex calculation of the form

tmp1 = tf.fun1(placeholder1,placeholder2)
tmp2 = tf.fun2(tmp1,placeholder3)
tmp3 = tf.fun3(tmp2)
ret  = tf.fun4(tmp3)

and I calculate

ret_vals = sess.run(ret,feed_dict={placeholder1: vals1, placeholder2: vals2, placeholder3: vals3}) 

fun1, fun2 etc are possibly costly operations on a lot of data.

If I run to get ret_vals as above, is it possible to later or at the same time access the intermediate values as well without re-running everything up to that value? For example, to get tmp2, I could re-run everything using

tmp2_vals = sess.run(tmp2,feed_dict={placeholder1: vals1, placeholder2: vals2, placeholder3: vals3})

But this looks like a complete waste of computation? Is there a way to access several of the intermediate results in a graph after performing one run?

The reason why I want to do this is for debugging or testing or logging of progress when ret_vals gets calculated e.g. in an optimization loop. Every step where I run the ret_vals calculations is costly but I want to see some of the intermediate results that were calculated.

If I do something like

tmp2_vals, ret_vals = sess.run([tmp2, ret], ...)

does this guarantee that the graph will only get run once (instead of one time for tmp2 and one time for ret) like I want it?

Upvotes: 2

Views: 1189

Answers (1)

童学智
童学智

Reputation: 41

Have you looked at tf.Print? This is an identity op with printing funciton. You can insert it in your graph right after tmp2 to get the value of it. Note that the default setting only allows you to print the first n values of the tensor, you can modify the value n by giving attribute first_n to the op.

Upvotes: 1

Related Questions