RuntimeError:当启用急切执行时,传递给 Optimizer.compute_gradients 的 `loss` 应该是一个函数。使用 Tensorflow 1.15

ash*_*edi 6 python optimization training-data deep-learning tensorflow1.15

我正在尝试使用 tensorflow 1.15 训练模型并启用急切执行。对于火车损失,我正在使用

train loss = mse_loss*args.lmbda + bits_per_pixel_loss
Run Code Online (Sandbox Code Playgroud)

我已经定义了优化器如下

main_optimiser = tf.train.AdamOptimiser(learning_rate=1e-3)
Run Code Online (Sandbox Code Playgroud)

到这里代码工作正常错误出现在优化的最小部分

main_step = main_optimiser.minimise(train_loss, global_step=step)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

RuntimeError: `loss` passed to Optimizer.compute_gradients should be a function when eager execution is enabled.
Run Code Online (Sandbox Code Playgroud)

因此,当我在函数内部计算 train_loss 时说,loss_computer在 train 函数内部构建然后使用

main_step =main_optimiser.minimise(loss_computer, global_step=step)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

AttributeError: 'NoneType' object has no attribute 'dtype'.
Run Code Online (Sandbox Code Playgroud)

如何在启用 Eager Execution 的情况下训练模型?如果我需要澄清任何事情,请告诉我。非常感谢任何帮助,谢谢!