0

I am trying to print the value of "kl_term" in this code

x = tf.placeholder('float', [None, num_l1])
y = tf.placeholder('float')

l1, prediction = nn_model(x,num_l1,num_l2)
ro = float(sparcity_parm)
ro_h = tf.reduce_mean(l1+1e-15)

kl_term = tf.reduce_sum(beta * ( ( ro * tf.log(ro/ro_h) ) + ( (1 - ro) * tf.log((1-ro)/(1-ro_h)) ) ))

a3_yy = tf.reduce_mean((prediction - x)**2 ,axis=0)

cost = tf.reduce_mean(0.5 * a3_yy) + kl_term

optimizer = tf.train.AdamOptimizer().minimize(cost)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for epoch in range(hm_epochs):
    epoch_loss = 0
    for i in range(int(data.shape[0]/batch_size)):
        epoch_x = data[i * batch_size : i * batch_size + batch_size,:]
        outt, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_x})
        epoch_loss += c
        print("kl_term:", tf.Print(kl_term,[kl_term]))

All it is printing out is this:

'kl_term:', <tf.Tensor 'Print_3:0' shape=() dtype=float32>

How can I get the actual value for kl_term?

A.Razavi
  • 479
  • 2
  • 8
  • 19
  • Possible duplicate of [How to print the value of a Tensor object in TensorFlow?](https://stackoverflow.com/questions/33633370/how-to-print-the-value-of-a-tensor-object-in-tensorflow) – c2huc2hu Feb 12 '18 at 16:15
  • I tried both "sess.run(kl_term)" and "kl_term.eval()", they both give errors. – A.Razavi Feb 12 '18 at 16:20
  • You need to supply values for your placeholders (x, y). See https://www.tensorflow.org/versions/r0.12/get_started/basic_usage and search for `feed_dict` – c2huc2hu Feb 12 '18 at 16:22
  • When I use feed_dict, it tells me value of input cannot be a Tensor: "The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles." – A.Razavi Feb 12 '18 at 16:51
  • Tensorflow is internally calculate the value for "kl_term", and it tells me its shape and type, why can't simply print the values too?! – A.Razavi Feb 12 '18 at 16:53
  • try adding `kl_term = ....; kl_term = tf.Print(kl_term, ['kl_term', kl_term])` as you need to run `tf.Print` in the graph when running the `cost` tensor. `cost = tf.Print(cost, ['kl_term', kl_term])` shoudl work as well. – Patwie Feb 12 '18 at 17:27
  • You're not feeding the correct values to x and y. You need to feed numpy arrays to them. You've told Tensorflow the shape of the arrays, but you haven't given them any values. That's why it can't print them – c2huc2hu Feb 12 '18 at 17:45

0 Answers0