Python TensorFlow 2.0:稀疏分类交叉熵和稀疏分类交叉熵有什么区别?
阅读TensorFlow 2.0的文档,我发现: 及Python TensorFlow 2.0:稀疏分类交叉熵和稀疏分类交叉熵有什么区别?,python,tensorflow,keras,tensorflow2.0,Python,Tensorflow,Keras,Tensorflow2.0,阅读TensorFlow 2.0的文档,我发现: 及 它们在教程中的使用方式,它们的论点,它们的描述,在我看来都是平等的。两者之间有什么区别?没有区别。如果您查看链接的文档,就可以在GitHub上找到源代码。两者都指向同一对象: def sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1): """Categorical crossentropy with integer targets.
它们在教程中的使用方式,它们的论点,它们的描述,在我看来都是平等的。两者之间有什么区别?没有区别。如果您查看链接的文档,就可以在GitHub上找到源代码。两者都指向同一对象:
def sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1):
"""Categorical crossentropy with integer targets.
if not from_logits:
if (isinstance(output, (ops.EagerTensor, variables_module.Variable)) or
output.op.type != 'Softmax'):
epsilon_ = _constant_to_tensor(epsilon(), output.dtype.base_dtype)
output = clip_ops.clip_by_value(output, epsilon_, 1 - epsilon_)
output = math_ops.log(output)
# ... blablabla
这是在:
tensorflow/python/keras/backend.py
例如,前者(tf.keras.loss.sparse\u categorical\u crossentropy
)的名称如下:
def sparse_categorical_crossentropy(y_true, y_pred, from_logits=False, axis=-1):
return K.sparse_categorical_crossentropy(
y_true, y_pred, from_logits=from_logits, axis=axis)
因此它指向
tensorflow/python/keras/backend.py中的另一个,一个是函数,一个是类
第一个是功能版本,当你评估它时,它只会吐出损失的值
第二个是类版本。您需要评估类本身的实例,以获得损失值
我相信你是对的,没有什么区别,如果使用KerasAPI,唯一的区别将是编译模型时
e、 g
请注意类版本上的额外括号,您需要传入一个类的实例。如果我错了,请纠正我。我想可以定义一个对象,例如:loss=SparseCategoricalCrossentropy()
,但另一方面,你必须按原样使用sparse\u category\u crossentropy,对吗?这是正确的。我认为更确切地说,compile方法有一些接口,因此它期望“loss”是一个可调用的对象,它有两个参数(logit和label)。
model.compile(loss=tf.keras.losses.sparse_categorical_crossentropy)
vs
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy())