【发布时间】:2021-08-30 15:58:42
【问题描述】:
我在下面写了损失函数:
def custom_loss(q_k):
def loss(y_true,y_pred):
loss= y_true * y_true /np.log(y_pred + q_k)
return loss
我给出了错误:
Cannot convert a symbolic Tensor (2nd_target:0) to a NumPy array
然后根据@Dr.snoopy 和@Kaveh,我编辑了我的损失函数但我得到了一个新错误,我的新损失函数:
def custom_loss(q_k):
def loss(y_true,y_pred):
return y_true * y_true /tf.math.log(y_pred + q_k)
# Return a function
return loss
我应该说y_true 和q_k 是张量。
但我得到了错误:
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:853 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:842 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:835 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:789 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py:201 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/keras/losses.py:143 __call__
losses, sample_weight, reduction=self._get_reduction())
/usr/local/lib/python3.7/dist-packages/keras/utils/losses_utils.py:308 compute_weighted_loss
losses = tf.convert_to_tensor(losses)
/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/framework/ops.py:1431 convert_to_tensor_v2_with_dispatch
value, dtype=dtype, dtype_hint=dtype_hint, name=name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:1441 convert_to_tensor_v2
as_ref=False)
/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:346 _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py:272 constant
allow_broadcast=True)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py:290 _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.
代码
更新:
感谢@Matthias Fripp,我将损失编辑为:
def custom_loss(q_k):
def loss_func(y_true,y_pred):
return y_true * tf.math.log(y_pred + q_k)
return loss_func
但我收到新错误: InvalidArgumentError:需要可广播的形状 [[node gradient_tape/loss/mul/Mul(定义于:1)]] [Op:__inference_train_function_2026]
函数调用栈: train_function
【问题讨论】:
-
你不能在 numpy 中实现损失,它必须使用 keras.backend 函数。
-
谢谢,是的,我编辑了一个新错误,不支持任何值!
-
用你自己的话来说,从函数返回值是什么意思?用你自己的话来说,当函数
def loss(y_true,y_pred):被调用时,你期望returned 是什么?为什么?用您自己的话说,该函数内部是否有return语句?loss= y_true * tf.math.log(y_pred + q_k)行是否用于计算返回值?如果是这样,您为什么希望它被退回? -
使用 Keras 后端和 tf.math 日志我都得到 None values not supported 错误!
-
在您的
loss函数中,您需要返回值,而不仅仅是计算它。当函数没有return语句时,它会自动返回None。在loss函数中添加return loss应该可以修复它。您可能还想在loss函数中使用loss以外的名称,因为在两件事上使用相同的名称会有点混乱。
标签: python tensorflow keras