tf.keras two losses, with intermediate layers as input to of one of them error:Inputs to eager execution function cannot be Keras symbolic tensors

tf.keras two losses, with intermediate layers as input to of one of them error:Inputs to eager execution function cannot be Keras symbolic tensors

我想在我的 tensorflow keras 模型中有两个损失,其中一个以中间层作为输入。 当我使用 keras 时,这段代码有效,但是当涉及到 tensorflow.keras 时,我遇到以下错误。

def loss_VAE(input_shape, z_mean, z_var, weight_L2=0.1, weight_KL=0.1):

  def loss_VAE_(y_true, y_pred):
      c, H, W, D = input_shape
      n = c * H * W * D

      loss_L2 = K.mean(K.square(y_true - y_pred), axis=(1, 2, 3, 4)) # original axis value is (1,2,3,4).

      loss_KL = (1 / n) * K.sum(
          K.exp(z_var) + K.square(z_mean) - 1. - z_var,
          axis=-1
      )

      return weight_L2 * loss_L2 + weight_KL * loss_KL

  return loss_VAE_

def loss_gt(e=1e-8):

  def loss_gt_(y_true, y_pred):
      intersection = K.sum(K.abs(y_true * y_pred), axis=[-3,-2,-1])
      dn = K.sum(K.square(y_true) + K.square(y_pred), axis=[-3,-2,-1]) + e

      return - K.mean(2 * intersection / dn, axis=[0,1])

  return loss_gt_


model.compile(
    adam(lr=1e-4),
    [loss_gt(dice_e), loss_VAE(input_shape, z_mean, z_var, weight_L2=weight_L2, weight_KL=weight_KL)],
    # metrics=[dice_coefficient]
)

错误:

_SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'Dec_VAE_VDraw_Var/Identity:0' shape=(None, 128) dtype=float32>, <tf.Tensor 'Dec_VAE_VDraw_Mean/Identity:0' shape=(None, 128) dtype=float32>]

是bug吗? 请在 NOTEBOOK.

中找到完整的代码

THIS是数据的link。

如果 运行 在 eager 模式下,tensorflow op 将检查输入的类型是否为 "tensorflow.python.framework.ops.EagerTensor" 并且 Keras ops 是否作为 DAG 实现。因此,急切模式的输入将是 tensorflow.python.framework.ops.Tensor,这会引发错误

您可以通过在 Keras 的 eager 模式下明确告诉 tensorflow 为 运行 来将输入类型更改为 EagerTensor。

tf.config.experimental_run_functions_eagerly(真)

添加此语句应该可以解决您的问题。尽管请注意,由于您 运行 现在处于急切模式并且建议仅用于调试、分析等,因此性能会受到显着影响。

K.mean 替换为 tf.reduce_mean 并相应地将所有 keras 后端函数替换为 tensorflow 函数解决了问题。