Python 3.x Tensorflow python。ValueError:无法将非标量张量转换为布尔值

Python 3.x Tensorflow python。ValueError:无法将非标量张量转换为布尔值,python-3.x,tensorflow,tensorflow-datasets,Python 3.x,Tensorflow,Tensorflow Datasets,我一直在尝试这些与tensorflow 1.12.2和visual studio 15.9.6一起安装相关的代码。python版本是3.6.6 问题在于log_huber函数中的条件语句。任何关于如何解决这一问题的建议都将不胜感激。代码附在下面: import tensorflow as tf import numpy as np def log_huber(x, m): if tf.abs(x) <= tf.abs(m): return x**2 else: r

我一直在尝试这些与tensorflow 1.12.2和visual studio 15.9.6一起安装相关的代码。python版本是3.6.6

问题在于log_huber函数中的条件语句。任何关于如何解决这一问题的建议都将不胜感激。代码附在下面:

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")
将tensorflow导入为tf
将numpy作为np导入
def log_huber(x,m):
如果tf.abs(x)如果你这样使用,你的尺寸会被移除

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))
def log_huber(x,m):
打印(tf.abs(x))
如果tf.squence(tf.abs(x)).eval()函数也有帮助。这将计算表达式并返回一个值
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))