Tensorflow 在图形执行中不允许将`tf.Tensor`用作Python`bool`

Tensorflow 在图形执行中不允许将`tf.Tensor`用作Python`bool`,tensorflow,Tensorflow,我正在编写一个神经网络,我有一个“使用tf.Tensor作为Pythonbool在图形执行中是不允许的”错误,当我调用compile时无法修复。我已经写了我自己的自定义损失,我认为这是抛出错误的原因 def compute_loss(Z5, Z6, C, YC, YI, YL): cost1 = np.add(np.matmul(-(Z5), YI), np.log(np.add(1, np.exp(Z5)))) cost1 = np.add(Z6, np.add(-np.dot(Z6,

我正在编写一个神经网络,我有一个“使用
tf.Tensor
作为Python
bool
在图形执行中是不允许的”错误,当我调用compile时无法修复。我已经写了我自己的自定义损失,我认为这是抛出错误的原因

def compute_loss(Z5, Z6, C, YC, YI, YL):
  cost1 = np.add(np.matmul(-(Z5), YI), np.log(np.add(1, np.exp(Z5))))
  cost1 = np.add(Z6, np.add(-np.dot(Z6, YL), np.log(np.add(1, exp(-Z6)))))
  cost2 = np.add(Z5, np.add(-np.dot(Z5, YI), np.log(np.add(1, exp(-Z5)))))
  cost1 = np.add(C, np.add(-np.dot(C, YC), np.log(np.add(1, exp(-YC)))))

  return cost

d2, D2I, D2L, C = d2(images) #call function to build network
custom_loss = compute_loss(D2I, D2L, C, y_c, y_i, y_l)
my_metric = tf.keras.metrics.Accuracy(name="accuracy", dtype=None)

d2.compile(optimizer=opt, loss=custom_loss, metrics=[tf.keras.metrics.Accuracy()], run_eagerly=True)
我尝试过使用numpy()或nd_array()将D2I、D2L、C转换为数组,但它们都会抛出tensorflow错误。任何帮助都将不胜感激 还尝试将损失更改为

def compute_loss(Z5, Z6, C, YC, YI, YL):
  cost1 = tf.nn.softmax_cross_entropy_with_logits(logits=Z5, labels=YI)
  cost2 = tf.nn.softmax_cross_entropy_with_logits(logits=Z6, labels=YL)
  cost3 = tf.nn.softmax_cross_entropy_with_logits(logits=C, labels=YC)
  cost = tf.reduce_mean(tf.add(cost1, tf.add(cost2, cost3)))
  return cost

但这引发了相同的错误

哪一行代码产生了此错误,或者您能否提供更多错误日志?当我使用丢失的nn.softmax版本时,该错误在d2.compile处引发。我试图修复它是为了在compile中添加'run_agreely=True(我已经检查过了,我正在agree中执行),但它没有改变任何东西。因此,我的第二次尝试是使用numpy add和mamtul(您在我的代码中看到的loss的第一个版本)编写我自己的softmax交叉熵,byt抛出一个“array()接受一个位置参数,但给出两个wre”错误