python - Keras 自定义损失函数返回值错误
问题描述
我在谷歌 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.
解决方案
您的损失函数中不能有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
推荐阅读
- c# - .net 标准库在 .net 核心中失败,但在框架中失败
- javascript - React json 渲染猫图片
- mercurial - 为什么 PhpStorm 看不到 /usr/bin 中的文件?
- python - 在python中完成前一个线程后启动一个新的基于计时器的线程
- google-apps-script - UrlFetchApp 超时
- r - R Copula 包。定义具有不同 copula 系列的 mvdc 时出错
- amazon-web-services - 如何在 aws cdk 中导入现有 VPC?
- entropy - 将生病标签提取为 Numpy 数组?
- javascript - 如何在 SVG 中计算法国曲线?
- sql - Presto - 使用唯一标识符连接多个表