使用`tf.张量`作为Python,keras中的自定义丢失函数不允许使用“bool”

2024-10-16 20:47:00 发布

您现在位置:Python中文网/ 问答频道 /正文

操作系统:Ubuntu 16.04

Python 3.5

我试图在增强策略梯度算法程序上实现自定义损失函数,但遇到以下错误:

2018-03-28 23:32:37.766405: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA Traceback (most recent call last):   File "reinforce.py", line 199, in <module>
    main(sys.argv)   File "reinforce.py", line 191, in main
    reinforce.train(env)   File "reinforce.py", line 57, in train
    self.model.compile(loss=custom_loss, optimizer='adam')   File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 824, in compile
    **kwargs)   File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 603, in compile
    loss = loss or {}   File "/home/yuyangw/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 614, 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. Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x7f2a3346d748>>

错误在于模拟类的训练功能。这里我想实现一个定制的日志函数来执行梯度策略。但我不能使用模型.编译优化定制损失函数。在

代码如下:

^{pr2}$

谢谢!!在


Tags: to函数inpyislibpackageslocal