1

I have been trying out these codes related to tensorflow 1.12.2 install together with visual studio 15.9.6. The python version is 3.6.6.

The problem lies in the conditional statement in the log_huber function. Any advise on how to solve this is greatly appreciated. The code is appended below:

import tensorflow as tf
import numpy as np

def log_huber(x, m):
  if tf.abs(x) <= tf.abs(m):
    return x**2
  else:
    return m**2 * (1 - 2 * tf.log(m) + tf.log(x**2))

x = np.arange(10,dtype=np.float32)
m = np.arange(10,20,dtype=np.float32)

_x = tf.data.Dataset.from_tensor_slices(x).shuffle(10).repeat().batch(1)
iter_x = _x.make_one_shot_iterator()
_x_init_ops = iter_x.make_initializer(_x)
next_x = iter_x.get_next()

_m = tf.data.Dataset.from_tensor_slices(m).shuffle(10).repeat().batch(1)
iter_m = _m.make_one_shot_iterator()
_m_init_ops = iter_m.make_initializer(_x)
next_m = iter_m.get_next()

y = tf.contrib.eager.py_func(func=log_huber, inp=[next_x,next_m], Tout=tf.float32)
with tf.Session() as sess:
    sess.run([_x_init_ops,_m_init_ops])

    terminate = 1

    while terminate!="0":
        print(sess.run(y))
        terminate = input("0 for end, 1 to continue")

The error message is as follow:

...\testTensorboard\testTensorboard\dataset.py", line 5, in log_huber
    if tf.abs(x) <= tf.abs(m):

 ...\conda\conda\envs\rdkit-env\lib\site-packages\tensorflow\python\framework\ops.py", line 914, in __bool__
    "Non-scalar tensor %s cannot be converted to boolean." % repr(self))

ValueError: Non-scalar tensor <tf.Tensor: id=58, shape=(1,), dtype=bool, numpy=array([False])> cannot be converted to boolean.
1
  • .eval() function can also help. This evaluates the expression and returns a value Commented Jun 20, 2019 at 17:04

1 Answer 1

3

If you use tf.squeeze like this your dimensions are removed.

def log_huber(x, m):
  print (tf.abs(x))
  if tf.squeeze(tf.abs(x)) <= tf.squeeze(tf.abs(m)):
    return x**2
  else:
    return m**2 * (1 - 2 * tf.log(m) + tf.log(x**2))

It removes dimensions of size 1 from the shape of this tensor

tf.Tensor([2.], shape=(1,), dtype=float32)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.