Python 3.x 不允许将'tf.Tensor'用作Python'bool'。使用`if t not None:`而不是`if t:`来测试是否定义了张量,

Python 3.x 不允许将'tf.Tensor'用作Python'bool'。使用`if t not None:`而不是`if t:`来测试是否定义了张量,,python-3.x,numpy,tensorflow,keras,genetic-algorithm,Python 3.x,Numpy,Tensorflow,Keras,Genetic Algorithm,我试图定义func(x),以便在此处使用遗传algs库: 然而,当我尝试使用sga.init\u random\u population(population\u size,params,interval)时,代码抱怨我使用tf.Tensors作为python bools 然而,我在整个代码中只引用了一个bool(精英主义),所以我不知道为什么会出现这个错误。询问其他使用sga.init的人。。。我的输入/设置很好。如有任何建议,将不胜感激 完全回溯: Traceback (most recen

我试图定义func(x),以便在此处使用遗传algs库: 然而,当我尝试使用
sga.init\u random\u population(population\u size,params,interval)
时,代码抱怨我使用tf.Tensors作为python bools

然而,我在整个代码中只引用了一个bool(精英主义),所以我不知道为什么会出现这个错误。询问其他使用sga.init的人。。。我的输入/设置很好。如有任何建议,将不胜感激

完全回溯:

Traceback (most recent call last):
  File "C:\Users\Eric\eclipse-workspace\hw1\ga2.py", line 74, in <module>
    sga.init_random_population(population_size, params, interval)
  File "C:\Program Files\Python36\lib\site-packages\geneticalgs\real_ga.py", line 346, in init_random_population
    self._sort_population()
  File "C:\Program Files\Python36\lib\site-packages\geneticalgs\standard_ga.py", line 386, in _sort_population
    self.population.sort(key=lambda x: x.fitness_val, reverse=True)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 671, in __bool__
    raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.

Tensorflow生成要在Tensorflow会话中执行的操作的计算图

geneticalgs.RealGA.init_random_population
是一种使用
numpy.random.uniform
生成numpy数组的操作

生成的总体作为张量对象可能意味着:

  • geneticalgs.RealGA.init_random_population
    中调用的
    numpy.random.uniform
    被修饰为返回张量
  • numpy.random.uniform
    已添加到要在会话中执行的计算图中
我将尝试通过启用“急切执行”来急切地执行程序

你也可以在某种程度上执行你热切关注的部分

size = tf.placeholder(tf.int64)
dim = tf.placeholder(tf.int64)
interval = tf.placeholder(tf.int64, shape=(2,))

init_random_population = tf.py_func(
    sga.init_random_population, [size, dim, interval], [])

with tf.Session() as session:
    session.run(
        init_random_population,
        {size: population_size, dim: params, interval: interval})

通常当程序给你一个错误时,它会指出错误发生的地方。它甚至可能显示一个“回溯”,一长串函数调用,以there错误结束。如果您不向我们提供此类信息,我们可能会因为不清楚而关闭它。也许这将有助于我编辑包含完整的回溯@hpauljReview文档以查看
init\u random\u population
需要哪些参数?在一个槽中,我想你给了它一个张量,而不是真/假参数问题可能在
sga
tf.enable_execution()
size = tf.placeholder(tf.int64)
dim = tf.placeholder(tf.int64)
interval = tf.placeholder(tf.int64, shape=(2,))

init_random_population = tf.py_func(
    sga.init_random_population, [size, dim, interval], [])

with tf.Session() as session:
    session.run(
        init_random_population,
        {size: population_size, dim: params, interval: interval})