I was going through some Tensorflow code that runs a tf.while_loop()
and I had a question. The code calculates the roots of a 4th degree polynomial as part of a lab for learning tensorflow.
My question is why a specific print statement will not output all of the intermediate values.
The code is below:
import tensorflow as tf
def f(x, w):
return (w[0] + w[1] * x + w[2] * tf.pow(x,2) + w[3] * tf.pow(x,3) + w[4] * tf.pow(x,4) )
def f1(x, w):
return (w[1] + 2. * w[2] * x + 3. * w[3] * tf.pow(x,2) + 4. * w[4] * tf.pow(x,3) )
def f2(x, w):
return (2. * w[2] + 6. * w[3] * x + 12. * w[4] * tf.pow(x,2) )
def fxn_plus_1(xn, w):
return (xn - (2. * f(xn, w) * f1(xn, w) / (2. * tf.square(f1(xn, w)) - f(xn, w) * f2(xn, w))))
def c(x, weights):
return tf.abs(x - fxn_plus_1(x, weights)) > 0.001
def b(x, weights):
x = fxn_plus_1(x, weights)
return x, weights
weights = tf.constant( [-1000., 1., 1. , 5. , 0.1])
x = fxn_plus_1(-10., weights)
out = tf.while_loop(c, b, [x, weights])
with tf.Session() as sess:
x, weights = sess.run(out)
print(x)
The output is correct with a values of 5.575055
. Now, I wanted to see what the intermediate values were of the loop body b()
as the algorithm proceeded. I changed the function b()
to the following:
def b(x, weights):
x = fxn_plus_1(x, weights)
print(x) ## ADDED PRINT STATEMENT
print(weights)
return x, weights
What I get in return is:
Tensor("while_4/sub_4:0", shape=(), dtype=float32)
Tensor("while_4/Identity_1:0", shape=(5,), dtype=float32)
5.575055
this seems to give the debug output or the graph information for the values of x,weights
instead of the actual values. I was not sure how to get the loop to actually print the values at each step.
Any suggestions? Thanks.
UPDATE on things attempted and suggested:
One of the commentors @user49593, suggested I try using tf.Print()
.
Here is the code, and the output. I am still just getting the graph info instead of the actual vector of values.
def b(x, weights):
x = fxn_plus_1(x, weights)
x = tf.Print(x, [x], message="here: ") #CHANGED TO tf.Print STATEMENT
return x, weights
The output is still just 5.575055
. No vector of intermediate values.