【问题标题】:for my loss function i give the errer: ValueError: None values not supported对于我的损失函数,我给出了错误: ValueError: None values not supported
【发布时间】: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_trueq_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


【解决方案1】:

您不能在损失函数中使用 numpy 数组,因为它将在图形模式下执行。

改为使用这样的 tensorflow 方法:

def custom_loss(q_k):

  def loss(y_true,y_pred):
    return y_true * tf.math.log(y_pred + q_k) #tf.math.log instead of np.log
 
  # Return a function
  return loss

【讨论】:

  • 谢谢。但我没有!我更新了我的问题,请看一下并给我你的想法。
  • 请永远不要完全更改问题,因为这会使答案变得毫无价值,并将新错误添加为新问题或将其作为更新部分添加到您当前的问题中。
  • @شیواپاساراد 正如@MatthiasFripp 所指出的,您应该在loss 函数中返回计算值。
  • 这就是问题所在。如果您不指定batch_size,它将认为它是 32。然后它将首先计算 (32,19) 个样本。所以,y_truey_pred 的形状是 (32,19)。但是你通过了q_k,形状为(17105,19),不能添加形状为(32,19)(17105,19)的张量。
  • 那么您应该根据您正在运行的步骤和时期以某种方式将q_k 拆分为损失函数。
猜你喜欢
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
  • 2020-08-24
  • 1970-01-01
相关资源
最近更新 更多