Keras自定义损失函数返回值错误

2024-06-26 04:43:42 发布

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

我在google TFT模型中使用一个自定义损失函数

def custom_loss(y_actual,y_pred):
     tupl = np.shape(y_actual)
     flag = tf.compat.v1.math.is_nan(y_actual)
     y_actual = y_actual[tf.compat.v1.math.logical_not(flag)]
     y_pred = y_pred[tf.compat.v1.math.logical_not(flag)]
     tensordiff = tf.compat.v1.math.reduce_sum(
                             tf.compat.v1.math.square(y_actual-y_pred))

     if len(tupl) >= 2:      
          tensordiff /= tupl[0]       
     if len(tupl) >= 3: 
          tensordiff /= tupl[1]      
     if len(tupl) >= 4:    
          tensordiff /= tupl[2]
        
     return tensordiff

我能够使用标准损失函数运行代码和训练模型,但当我使用自定义损失函数时,我得到:

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_util.py:445
          make_tensor_proto raise ValueError("None values not supported.")
ValueError: None values not supported.

有没有办法解决这个问题

更新:

使用下面的循环代码重新运行

def custom_lossGCF1(y_actual,y_pred):
        tupl = np.shape(y_actual)
        tensordiff = tf.compat.v1.math.reduce_sum(tf.compat.v1.math.square(y_actual-y_pred))

        for x in range(min(len(tupl),4)-1):
          tensordiff = tf.compat.v1.math.divide_no_nan(tensordiff,tupl[x])

        return tensordiff

但仍然遇到以下错误:

有什么建议吗

alueError: in user code:

    <ipython-input-99-3fb23687b2d6>:1076 custom_lossGCF1  *
        tensordiff = tf.compat.v1.math.divide_no_nan(tensordiff, tupl[x])
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper  **
        return target(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py:1463 div_no_nan
        y = ops.convert_to_tensor(y, name="y", dtype=x.dtype.base_dtype)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/profiler/trace.py:163 wrapped
        return func(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:1566 convert_to_tensor
        ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py:339 _constant_tensor_conversion_function
        return constant(v, dtype=dtype, name=name)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py:265 constant
        allow_broadcast=True)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py:283 _constant_impl
        allow_broadcast=allow_broadcast))
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_util.py:445 make_tensor_proto
        raise ValueError("None values not supported.")

    ValueError: None values not supported.

Tags: pylibpackagesusrlocaldisttftensorflow
2条回答

损失函数中不能有if语句,因为它没有梯度。
尝试用此代码替换它。此循环的功能与if语句相同

def custom_loss(y_actual,y_pred):
   
        tupl = np.shape(y_actual)
        flag = tf.compat.v1.math.is_nan(y_actual)
        y_actual = y_actual[tf.compat.v1.math.logical_not(flag)]
        y_pred = y_pred[tf.compat.v1.math.logical_not(flag)]
        tensordiff = tf.compat.v1.math.reduce_sum(tf.compat.v1.math.square(y_actual-y_pred))
        
        for x in range(min(len(tupl),4)-1):
                tensordiff /= tupl[x]
        
        return tensordiff

在损失函数中不能有if语句,if没有梯度,只要想想if的导数是什么

相关问题 更多 >