AttributeError:“Tensor”对象在自定义损失函数中没有属性“numpy”(Tensorflow 2.1.0)

Fra*_*ala 4 python numpy loss tensorflow loss-function

我想使用自定义损失函数训练模型,为此,我需要在以下方法中将张量转换为 numpy 数组:

def median_loss_estimation(y_true, y_predicted):
    a = y_predicted.numpy()
Run Code Online (Sandbox Code Playgroud)

但我有这个错误:

AttributeError: 'Tensor' object has no attribute 'numpy'
Run Code Online (Sandbox Code Playgroud)

为什么?如何将张量转换为 numpy 数组?

Fra*_*ala 5

答案是:放进run_eagerly=Truemodel.compile