小编dig*_*ger的帖子

如何通过 Tensorflow2.x 中的子类 tf.keras.losses.Loss 类自定义损失

当我阅读Tensorflow 网站上的指南时,我发现了两种自定义损失的方法。第一个是定义损失函数,就像:

def basic_loss_function(y_true, y_pred):
    return tf.math.reduce_mean(tf.abs(y_true - y_pred))
Run Code Online (Sandbox Code Playgroud)

并且为了简单起见,我们假设批量大小也为1,因此y_true和的形状y_pred都是(1, c),其中c是类的数量。所以在这个方法中,我们给出两个向量y_truey_pred,并返回一个值(scala)。

然后,第二种方法是子类化tf.keras.losses.Lossclass,guide中的代码是:

class WeightedBinaryCrossEntropy(keras.losses.Loss):
    """
    Args:
      pos_weight: Scalar to affect the positive labels of the loss function.
      weight: Scalar to affect the entirety of the loss function.
      from_logits: Whether to compute loss from logits or the probability.
      reduction: Type of tf.keras.losses.Reduction to apply to loss.
      name: Name of the loss function.
    """
    def __init__(self, pos_weight, weight, from_logits=False,
                 reduction=keras.losses.Reduction.AUTO, …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow tensorflow2.0

6
推荐指数
1
解决办法
5508
查看次数

为什么使用 cv2.calcHist 总是有一个错误“返回 NULL 而不设置错误”

我在 python 中使用 Opencv,遇到了一个问题。
当我运行以下代码时:

img = cv2.imread('test.jpg',0)
hist = cv2.calcHist([img],[0],None,256,[0,256])
Run Code Online (Sandbox Code Playgroud)

发生错误:SystemError: <built-in function calcHist> returned NULL without setting an error
我很困惑,在网络中找不到相同的错误,所以出了什么问题?
谢谢。

PS:我在 Windows 和 Ubuntu 中运行相同的代码,并得到相同的错误,所以这可能不是系统的原因?

python opencv computer-vision

5
推荐指数
2
解决办法
5009
查看次数